Question: Question 1, 2 & 3 deal with the given code below: public class Human { private int age; private String name; public Human(int age, String
Question 1, 2 & 3 deal with the given code below:
public class Human {
private int age;
private String name;
public Human(int age, String name) {
this.age = age;
this.name = name;
}
public String toString() { return " Age: "+ age +" Name: "+name ; }
public int getAge() { return age; }
public String getName() { return name; }
public String talkToMe() { return " Human "; }
}
public class Professor extends Human {
private double salary;
public Professor(double salary, String name) {
super(45, name);
this.salary = salary;
}
public String toString() { return super.toString() + " Salary: "+ salary; }
public double getSalary() { return salary; }
public String talkToMe() { return " Professor "; }
}
1. What is the output,
public class Q1 {
public static void main(String[] args) {
Human h = new Human(72 , "Mac");
Professor p = new Professor(25.7, "Poof");
check(h);
check(p);
}
public static void check(Human h2) {
System.out.println( h2.toString() + " , " + h2.talkToMe());
}
}
Answer:
2. what is the output
public class Q2 {
public static void main(String[] args) {
Professor p = new Professor(7600,"prof");
System.out.println(p.getName()+ " , "+p.getAge()+ " , "+p.getSalary());
}
}
Answer:
3. For class Professor, fill in the body of method equals below. A given Professor object is equal to another Professor object if their salary, name and age are all the same. [1,1,,=6]
public boolean equals(Professor f) {
return (getName().equals(f.getName() ) &&
getAge() == f.getAge() &&
salary == f.salary();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
