This exercise depends on these two classes: The Person class has a print method
ID: 3810327 • Letter: T
Question
This exercise depends on these two classes:
The Person class has a print method that writes a person's name and age to the console. Thus, if we create a Person object with these values:
Person p = new Person("Buster", 22);
then the call:
p.print();
will write the following output to the console:
In the same manner, we want to print information about objects in the subclass Oldie. Remember that Oldie objects contain the additional boolean datum perryComoFan. If we create an Oldie object with these values:
Oldie k = new Oldie("Cornelius", 93, true);
then the call: k.print(); should result in the following output to the console:
For this exercise, enter code in the box below that will allow the print method in the Oldie class to write the above specified output to the console. Note that your output must have the same format as that given in the example above.
/**
* This class contains data and methods pertaining
* to an oldie -- i.e. someone over the age of, oh say 30.
* The datum contained in this class is:
* whether or not this person is a Perry Como fan.
* This class derives from Person.
*/
public class Oldie extends Person
{
private boolean perryComoFan;
public boolean isPerryComoFan ()
{
return perryComoFan;
}
public void setPerryComoFan (boolean f)
{
perryComoFan = f;
}
public void print ()
{
Explanation / Answer
Person.java
public class Person {
//Declaring instance variables
private String name;
private int age;
//Parameterized constructor
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
//Setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//Displaying the person class info
public void print( )
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
________________
Oldie.java
public class Oldie extends Person {
//Declaring instance variables
private boolean perryComoFan;
//Parameterized constructor
public Oldie(String name, int age, boolean perryComoFan) {
super(name, age);
this.perryComoFan = perryComoFan;
}
//getters and setters
public boolean isPerryComoFan() {
return perryComoFan;
}
public void setPerryComoFan(boolean perryComoFan) {
this.perryComoFan = perryComoFan;
}
@Override
public void print( )
{
super.print();
System.out.println("Perry Fan:"+ perryComoFan);
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating the Person class object by passing values as arguments
Person p = new Person("Buster", 22);
//Displaying the Person class info
System.out.println("Displaying the Person info :");
p.print();
Oldie k = new Oldie("Cornelius", 93, true);
//Displaying the Oldie class info
System.out.println(" Displaying the Oldie info :");
k.print();
}
}
_________________
Output:
Displaying the Person info :
Name: Buster
Age: 22
Displaying the Oldie info :
Name: Cornelius
Age: 93
Perry Fan:true
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.