The Person, Student, Employee classes. Design a class named Person and its two s
ID: 3774209 • Letter: T
Question
The Person, Student, Employee classes. Design a class named Person and its two subclasses, Student and Employee.
A has instance variables called name, phone number and email, all of which are Strings. A , which is a subclass of Person , has status (freshman, junior etc), also declared as a String.
An has , office number (declared as int), salary (declared as int)
Implement the , and classes. Have a full-arg constructor for each class. Test all these classes using a class. In your test , you will test the full arg constructors of each class and for each object of that class, you will obtain the values of the instance variables. You will also use all the setters of a class for the object.
// you will be using Scanner class to obtain user inputs. You need to import this class first
import java.util.Scanner;
public class TestPersonPratibhaMenon {
public static void main(String args[]){
//Create a new scanner object to read inputs
Scanner scanner = new Scanner(System.in);
//Select the user choice for Student or Employee objects.
System.out.println("Enter 1)To create a student 2)To create an employee:");
int choice = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter name:");
String name = scanner.nextLine();
System.out.print("Enter address:");
String address = scanner.nextLine();
System.out.print("Enter phone number:");
String phone = scanner.nextLine();
System.out.print("Enter email:");
String email = scanner.nextLine();
if(choice == 1){
System.out.print("Enter student's status:");
String status = scanner.nextLine();
Student student1 = new Student(name, address, phone, email, status);
System.out.print(student1.toString());
}
else{
System.out.print("Enter office number:");
int office = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter salary:");
int salary = scanner.nextInt();
scanner.nextLine();
Employee employee1 = new Employee(name, address, phone, email, office, salary);
System.out.print(employee1.toString());
}
}
}
Explanation / Answer
TestPersonPratibhaMenon.java
import java.util.Scanner;
public class TestPersonPratibhaMenon {
public static void main(String args[]){
//Create a new scanner object to read inputs
Scanner scanner = new Scanner(System.in);
//Select the user choice for Student or Employee objects.
System.out.println("Enter 1)To create a student 2)To create an employee:");
int choice = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter name:");
String name = scanner.nextLine();
System.out.print("Enter address:");
String address = scanner.nextLine();
System.out.print("Enter phone number:");
String phone = scanner.nextLine();
System.out.print("Enter email:");
String email = scanner.nextLine();
if(choice == 1){
System.out.print("Enter student's status:");
String status = scanner.nextLine();
Student student1 = new Student(name, address, phone, email, status);
System.out.print(student1.toString());
}
else{
System.out.print("Enter office number:");
int office = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter salary:");
int salary = scanner.nextInt();
scanner.nextLine();
Employee employee1 = new Employee(name, address, phone, email, office, salary);
System.out.print(employee1.toString());
}
}
}
Employee.java
public class Employee extends Person{
private int office;
private int salary;
public Employee(String name,String address,String phone,String email,int office, int salary){
super(name, address, phone, email);
this.office = office;
this.salary = salary;
}
public int getOffice() {
return office;
}
public void setOffice(int office) {
this.office = office;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return super.toString()+" Office: "+office+" Salary: "+salary;
}
}
Student.java
public class Student extends Person {
private String status;
public Student(String name,String address,String phone,String email, String status){
super(name, address, phone, email);
this.status= status;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String toString(){
return super.toString()+" Status: "+status;
}
}
Person.java
public class Person {
private String name, address, phone, email;
public Person(String name,String address,String phone,String email){
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return "Name: "+name+" Address: "+address+" Phone: "+phone+" Email: "+email;
}
}
Output:
Enter
1)To create a student
2)To create an employee:
1
Enter name:Suresh
Enter address:Address
Enter phone number:1234
Enter email:ssssss
Enter student's status:status
Name: Suresh Address: Address Phone: 1234 Email: ssssss Status: status
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.