I have already written a Date class and a Person class that compiles, my employe
ID: 3625318 • Letter: I
Question
I have already written a Date class and a Person class that compiles, my employee class needs major help and I would hazzard to say the I need help with the Student class and the Driver class as well. If someone could correct the employee class, and give me an example student and driver class based on my work it would help me understand where I am stuck.
Here is the exact assignment:
This program has 5 classes. The program determines the cost for classes taken by an employee or a student. The employee and student program will extend Person, because every student or employee taking classes is a Person, that has a first name, a last name and date of birth.
Date
Person
First Name Last Name Date of Birth Check date to ensure it is not null.
Employee extends Person
getClassCost() which should include the number of classes and return 50 for 1 class, 100 for 2 classes, 150 for 3 classes, and 200 for 4 classes. An error message is to display for classes over 4 and less than 1, that employee discount does not apply to classes over 4 for 1 semester. The getClassCost() should use a switch and return a value of cost for classes and print out number of classes taken. setEnrollDate () should check for null value and return an error.
Student extends Person
getClassCost() which should include the number of class as int and return 150 for 1 class, 300 for 2 classes, 450 for 3 classes, 600 for 4 classes, 750 for 5 classes. An error message is to display for classes over 5, telling them that they have exceeded the number of classes allowed. Also include a case for 0 classes that will display a message saying that the student is not enrolled. The getClassCost() should use a switch and return a value of cost for classes and print out number of class taken.
setEnrollDate () should check for null value and return an error.
toString() should print out the cost for classes enrolled, for employee or student, Date of Birth, Enrollment date.
A test class that will test 2 employees and 2 students
Here is what I have:
public class Date
{
private String month;
private int day;
private int year;
public Date()
{
month = "Jan";
day = 1;
year = 2005;
}
public Date(int monthInt, int day, int year)
{
setDate(monthInt, day, year);
}
public Date(String monthString, int day, int year)
{
setDate(monthString, day, year);
}
public Date(Date aDate)
{
if (aDate == null)
{
System.out.println("Fatal error");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}
public void setDate(int monthInt, int day, int year)
{
if (dateOK(monthInt, day, year))
{
this.month = monthString(monthInt);
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal error");
System.exit(0);
}
}
public void setDate(String monthString, int day, int year)
{
if (dateOK(monthString, day, year))
{
this.month = monthString;
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal error");
System.exit(0);
}
}
public void setYear(int year)
{
if ((year<1000) || (year>9999))
{
System.out.println("Fatal Error");
System.exit(0);
}
this.year = year;
}
public void setMonth(int monthNumber)
{
if ((monthNumber <=0) || (monthNumber>12))
{
System.out.println("Fatal Error");
System.exit(0);
}
month = monthString(monthNumber);
}
public void setDay(int day)
{
if ((day <=0) || (day>31))
{
System.out.println("Fatal Error");
System.exit(0);
}
this.day = day;
}
public int getMonth()
{
if (month.equals("Jan")) return 1;
else if (month.equals("Feb")) return 2;
else if (month.equals("Mar")) return 3;
else if (month.equals("Apr")) return 4;
else if (month.equals("May")) return 5;
else if (month.equals("Jun")) return 6;
else if (month.equals("Jul")) return 7;
else if (month.equals("Aug")) return 8;
else if (month.equals("Sep")) return 9;
else if (month.equals("Oct")) return 10;
else if (month.equals("Nov")) return 11;
else if (month.equals("Dec")) return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
public String toString()
{
return (month + " " + day + ", " + year);
}
public boolean equals(Date otherDate)
{
return ((month.equals(otherDate.month)) && (day==otherDate.day)
&& (year == otherDate.year));
}
private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ((monthInt>=1) && (monthInt <=12) &&
(dayInt>=1) && (dayInt<=31) &&
(yearInt>=1000) && (yearInt<=9999));
}
private boolean dateOK(String monthString, int dayInt, int yearInt)
{
return (monthOK(monthString) &&
(dayInt>=1) && (dayInt<=31) &&
(yearInt>=1000) && (yearInt<=9999));
}
private boolean monthOK(String month)
{
return (month.equals("Jan") || month.equals("Feb") ||
month.equals("Mar") || month.equals("Apr") ||
month.equals("May") || month.equals("Jun") ||
month.equals("Jul") || month.equals("Aug") ||
month.equals("Sep") || month.equals("Oct") ||
month.equals("Nov") || month.equals("Dec"));
}
private String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1: return "Jan";
case 2: return "Feb";
case 3: return "Mar";
case 4: return "Apr";
case 5: return "May";
case 6: return "Jun";
case 7: return "Jul";
case 8: return "Aug";
case 9: return "Sep";
case 10: return "Oct";
case 11: return "Nov";
case 12: return "Dec";
default: System.out.println("Fatal error.");
System.exit(0);
return "Error";
}
}
} // Date
public class Person
{
private String name;
public Person()
{
name = "";
}
public Person(String theName)
{
name = theName;
}
public Person(Person theObject)
{
name = theObject.name;
}
public String getName()
{
return name;
}
public void setName(String theName)
{
name = theName;
}
public boolean equals(Object other)
{
if (other == null)
{
return false;
}
else if (this.getClass() != other.getClass())
{
return false;
}
else
{
Person person = (Person)other;
return(this.getName().equals(person.getName()));
}
}
public String toString()
{
return name;
}
}//Person
public class Employee
{
private String name;
private Date hireDate;
public Employee()
{
name = "No name";
hireDate = new Date("Jan", 1, 1000);
}
public Employee(String theName, Date theDate)
{
if (theName == null || theDate == null)
{
System.out.println("Fatal Error creating employee.");
System.exit(0);
}
name = theName;
hireDate = new Date(theDate);
}
public Employee(Employee originalObject)
{
name = originalObject.name;
hireDate = new Date(originalObject.hireDate);
}
public String getName()
{
return name;
}
public Date getHireDate()
{
return new Date(hireDate);
}
public void setName(String newName)
{
if (newName == null)
{
System.out.println("Fatal Error setting employee name.");
System.exit(0);
}
else
name = newName;
}
public void setHireDate(Date newDate)
{
if (newDate == null)
{
System.out.println("Fatal Error setting employee hire date.");
System.exit(0);
}
else
hireDate = new Date(newDate);
}
public String toString()
{
return (name + " " + hireDate.toString());
}
public boolean equals(Employee otherEmployee)
{
return (name.equals(otherEmployee.name) && hireDate.equals(otherEmployee.hireDate));
}
}//Employee
Explanation / Answer
Your Employee class should extend your Person class, since an employee is a person. public class Employee extends Person Doing it this way, you only need to define the attributes and methods that Person does not have, saying it a different way, Employee will inherit everything that Person has, and whatever you define within Employee will add to it. public class Employee extends Person { // Will inherit name attribute from person private Date hireDate; public Employee() { // can call parent constructor to offload some coding super(); // calls the Person constructor, the one with no params hireDate = new Date( "Jan", 1, 1000); } } Now go through the rest of your methods with this idea
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.