X461: Polymorphism Practice 1

For this question, assume the following implementation of the Person and Student classes:

Person Class Implementation

x
 
1
public class Person{
2
    private String name;
3
    private int age;
4
    private double heightInCM;
5
6
    public Person (String na, int ag, double he) {
7
        name  = na;
8
        age = ag;
9
        heightInCM = he;
10
    }
11
12
    public String getName() {
13
        return name;
14
    }
15
    public int getAge() {
16
        return age;
17
    }
18
    public double getHeightInCM() {
19
        return heightInCM;
20
    }
21
}

Student Class Implementation

20
 
1
public class Student extends Person {
2
    private int studentID;
3
    private String school;
4
5
    public Student(String na, int ag, double he, int sId, String sch) {
6
        super(na, ag, he);
7
        studentID = sId;
8
        school = sch;
9
10
    }
11
12
    public int getStudentID() {
13
        return studentID;
14
    }
15
16
17
    public String getSchool() {
18
        return school;
19
    }
20
}

Write a method that takes in two variables: one of type Person, one of type Student. This code should assign the object where age is larger to the variable max defined below, then return max.

Your Answer:

xxxxxxxxxx
6
 
1
public Person getOldest(Person per, Student stu){
2
    Person max = per;
3
    
4
    return max;
5
}
6
Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.