Answer to Question #342599 in Java | JSP | JSF for Kirti

Question #342599

Let a class Person contains data members name and age. A constructor with two arguments

is used to assign name and age. Person is of two types a) Student and b) Teacher. class

Student contains data members i)course ii) Roll Number and iii)Marks and method

display() to display data related to student. Similarly, class Teacher contains data members

i) subject_assigned (May take this as a String) ii) contact_hour and method display () to

display data related to teacher. Implement this program using base class constructor in

derived class


1
Expert's answer
2022-05-19T16:43:40-0400
class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

class Student extends Person {
    private final int course;
    private final int roll_number;
    private final int[] marks;

    public Student(String name, int age, int course, int roll_number, int[] marks) {
        super(name, age);
        this.course = course;
        this.roll_number = roll_number;
        this.marks = marks;
    }

    public void display() {
        super.display();
        System.out.println("Course: " + course);
        System.out.println("Roll number: " + roll_number);
        System.out.print("Marks: ");
        for (int mark : marks) {
            System.out.print(mark);
            System.out.print(' ');
        }
        System.out.println();
    }
}

class Teacher extends Person {
    private final String subject_assigned;
    private final int contact_hour;

    public Teacher(String name, int age, String subject_assigned, int contact_hour) {
        super(name, age);
        this.subject_assigned = subject_assigned;
        this.contact_hour = contact_hour;
    }

    public void display() {
        super.display();
        System.out.println("Subject assigned: " + subject_assigned);
        System.out.println("Contact hour: " + contact_hour);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS