Java Project # 8 Concepts Tested in this Program: ? Class Design ? Constructors
ID: 3738382 • Letter: J
Question
Java Project # 8
Concepts Tested in this Program:
? Class Design
? Constructors
? Objects
? Inheritance
Program:
Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee.
A Person object has a name, address, phone number, and email address (all Strings).
A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable.
An Employee Object has an office number, salary (both ints ), and a date hired. Use the MyDate class defined below to create an object for date hired:
public class MyDate {
private String date;
public MyDate(String date){
this.date = date;
}
public String getDate() {
return date;
}
}
A Faculty object has office hours and a rank (both Strings), while a Staff object has a title (as a String).
For the Student, Faculty, and Staff classes, create toString methods that store information about the object (in the format shown in the examples below).
Test your classes in a Driver class (within the same file) that asks the user what type of object they would like to create as well as what information they would like it to have.
The program should then use the object's toString method to print information about that object.
Sample Run #1
Sample Run #2
1) to create a student 2) to create an Employee Choice: 1 Enter a name: Walter D. Graham Enter Walter D. Graham's adress: 4737 Stadium Drive, Orangville, MA, 01588 Enter Walter D. Graham's email: valtergrahameuniversity.edu Enter Walter D. Graham s class Status Junior Student: Walter D. Graham Status: Junior Address: 4737 Stadium Drive, Orangville, MA, 01588 Phone number: 508-266-2108 Email Address: waltergraham@university.eduExplanation / Answer
Java Code:
Person.java
package mainprog;
public class Person {
String nam;
String add;
String phn;
String em;
public Person(String nam, String add, String phn, String em) {
this.nam = nam;
this.add = add;
this.phn = phn;
this.em = em;
}
}
Student.java
package mainprog;
public class Student extends Person {
public final String classStatus;
public Student(String nam, String add, String phn, String em, String classStatus) {
super(nam, add, phn, em);
this.classStatus = classStatus;
}
@Override
public String toString() {
return this.getClass().getName() + " : " + nam+" Status : "+classStatus+" Address : "+add+" Phone number : "+phn+"Email Address : "+em;
}
}
Employee.java
package mainprog;
public class Employee extends Person {
public int officeNumber;
public int sal;
public MyDate date;
public Employee(String nam, String add, String phn, String em,int officeNumber,int sal,MyDate date) {
super(nam, add, phn, em);
this.officeNumber=officeNumber;
this.sal=sal;
this.date=date;
}
}
Faculty.java
package mainprog;
public class Faculty extends Employee {
public String offHrs;
public String rnk;
public Faculty(String nam, String add, String phn, String em,MyDate date, int officeNumber, int sal, String offHrs, String rnk) {
super(nam, add, phn, em,officeNumber,sal,date);
this.offHrs=offHrs;
this.rnk=rnk;
}
@Override
public String toString() {
return this.getClass().getName() + " : " + nam+" Office Hours : "+offHrs+" Rank "+rnk+" Office Number : "+officeNumber
+" Salary : "+sal+" Date hired: "+date.getDate()+" Address : "+add+" Phone number : "+phn+" Email Address : "+em;
}
}
Staff.java
package mainprog;
public class Staff extends Employee {
public String title;
public Staff(String nam, String add, String phn, String em,MyDate date,int officeNumber, int sal, String title) {
super(nam, add, phn, em,officeNumber,sal,date);
this.title=title;
}
@Override
public String toString() {
return this.getClass().getName() + " : " + nam+" Title : "+title+" Office Number : "+officeNumber+" Salary : "+sal+" Date hired: "+date.getDate()+" Address : "+add+" Phone number : "+phn+" Email Address : "+em;
}
}
Output:
run:
1) To create a student
1) To create an Employee
Choice: 1
Enter a name : dd
Enter dd address : ff
Enter dd phone number : 396
Enter dd email : sfh
Enter dd class Status : ff
mainprog.Student : dd
Status : ff
Address : ff
Phone number : 396Email Address : sfh
BUILD SUCCESSFUL (total time: 26 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.