Create a class called SchoolKid that is the base class for children at a school.
ID: 3804510 • Letter: C
Question
Create a class called SchoolKid that is the base class for children at a school. It should have attributes for the child’s name and age, the name of the child’s teacher, and a greeting (String type). It should have appropriate accessor and mutator methods for each of the attributes.
Derive a class ExaggeratingKid from SchoolKid. The new class should override the accessor method for the age, reporting actual age plus 2. It also should override the accessor method for the greeting, returning the child’s greeting concatenated with the words “I am the best”.
Explanation / Answer
SchoolKid.java
public class SchoolKid {
//Declaring instance variables
private String childName;
private int age;
private String teacherName;
private String greeting;
//parameterized constructor
public SchoolKid(String childName, int age, String teacherName,
String greeting) {
this.childName = childName;
setAge(age);
this.teacherName = teacherName;
setGreeting(greeting);
}
//Setters and getters
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Child Name=" + childName + ", Age=" + age
+ ", Teacher Name=" + teacherName + ", greeting=" + greeting;
}
}
________________
ExaggeratingKid.java
public class ExaggeratingKid extends SchoolKid {
public ExaggeratingKid(String childName, int age, String teacherName,
String greeting) {
super(childName, age, teacherName, greeting);
}
@Override
public void setAge(int age) {
super.setAge(age+2);
}
@Override
public void setGreeting(String greeting) {
super.setGreeting(greeting+"I am the best ");
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString();
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an SchoolKid class object by passing arguments
SchoolKid sk=new SchoolKid("Williams",21,"James","Hello!.");
//Displaying the contents of school object
System.out.println("SchoolKid Info: "+sk.toString());
//Creating an ExaggeratingKid class object by passing arguments
ExaggeratingKid esk=new ExaggeratingKid("Johnson", 22, "Sachin", "Hi!.");
//Displaying the contents of school object
System.out.println("ExaggeratingKid Info: "+esk.toString());
}
}
_________________
Output:
SchoolKid Info:
Child Name=Williams, Age=21, Teacher Name=James, greeting=Hello!.
ExaggeratingKid Info:
Child Name=Johnson, Age=24, Teacher Name=Sachin, greeting=Hi!.I am the best
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.