Class person, student and teacher
Monday, November 13, 2023
⁃
Difficulty:
Intermediate
Objective
Develop a Java project that consists of three fundamental classes: Person, Student, and Teacher. The inheritance relationship establishes that the Student and Teacher classes inherit from the base Person class.
The Student class will include a public method called Study()
, which will display the message "I am studying" on the screen.
Meanwhile, the Person class must contain two essential public methods: Greet()
and SetAge(int age)
, the latter responsible for assigning the person's age.
Additionally, the Teacher class will have a public method called Explain()
, which will print "I am explaining" on the screen.
As part of the logic, a public method will be included in the Student class called ViewAge()
, which will display "My age is: x years" on the screen.
The challenge is complemented by the creation of a test class, StudentTeacherTest
, equipped with a Main
method that will perform the following actions:
- Create a new instance of Person and generate a greeting.
- Create a new instance of Student, assign an arbitrary age, generate a greeting, display its age on the screen, and start studying.
- Create a new instance of Teacher, assign an arbitrary age, generate a greeting, and start explaining.
Input
No input required
Output
Hello!
Hello!
My age is 21 years old
I'm studying
Hello!
I'm explaining
Solution
public class StudentTeacherTest {
public static void main(String[] args) {
Person myPerson = new Person();
myPerson.greet();
Student myStudent = new Student();
myStudent.setAge(21);
myStudent.greet();
myStudent.showAge();
myStudent.study();
Teacher myTeacher = new Teacher();
myTeacher.setAge(30);
myTeacher.greet();
myTeacher.explain();
}
}
class Person {
protected int age;
public void greet() {
System.out.println("Hello!");
}
public void setAge(int n) {
age = n;
}
}
class Student extends Person {
public void study() {
System.out.println("I'm studying");
}
public void showAge() {
System.out.println("My age is " + age + " years");
}
}
class Teacher extends Person {
public void explain() {
System.out.println("I'm explaining");
}
}
public class StudentTeacherTest {
public static void main(String[] args) {
Person myPerson = new Person();
myPerson.greet();
Student myStudent = new Student();
myStudent.setAge(21);
myStudent.greet();
myStudent.showAge();
myStudent.study();
Teacher myTeacher = new Teacher();
myTeacher.setAge(30);
myTeacher.greet();
myTeacher.explain();
}
}
class Person {
protected int age;
public void greet() {
System.out.println("Hello!");
}
public void setAge(int n) {
age = n;
}
}
class Student extends Person {
public void study() {
System.out.println("I'm studying");
}
public void showAge() {
System.out.println("My age is " + age + " years");
}
}
class Teacher extends Person {
public void explain() {
System.out.println("I'm explaining");
}
}
Click here to view the exercise solution
Share it
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
Copied
The code has been successfully copied to the clipboard.