Could use some help with this program, much appreciated! 1. Define the class Dat
ID: 3912978 • Letter: C
Question
Could use some help with this program, much appreciated!
1. Define the class Date which has the data attributes (fields) month, day, and year, and the methods constructor Date( String m, int d, int y), toString() and validate(). The toString converts an object of type Date into a string of the following format “July, 16, 2018”. The validate() is called by the constructor to validate the date before initializing the month, day, and year( you may use the enum type for the months See chapter 8. The year should be five digits <2050 and the day must be between 1 and 31 or 30 or 28 or 29 depending on the month ). If the date is invalid, the constructor prints an error message and asks you to renter the date.
2. Define the class Employee which the data attributes: name, ssn, position, dateOfBirth, and pay. The first three attributes of type string, dataofBirth of type Date, and pay of type float. The class Employee has the following methods:
The method toString(): returns the employee info as a string of the following format:
Name:
SSN:
Position:
Date of Birth:
Pay:
The method raise ( float per) which increases the pay by the given percentage, per.
The methods set and get for each attributes including the dateOfBirth.
3. Define the class PTEmployee as an extended class of Employee and include two additional attributes: nHours and wages. The constructor of the extended class is defined by calling the constructor of the superclass Employee (assuming the pay =0)and then initializing the nHours , wages , and pay. The pay is computed as nHours*wages. The method raise( float amount) for this subclass increases the wages by the given amount( it is not percentage) and re compute the pay . Also, this class has get and set methods for nHours and wages. The method toString for the subclass PTEmployee uses the toString of the super class Employee and adds to it nHours and wages.
4. Write the program EmployeeDemo which inputs employee info from the keyboard and creates two objects E of type Employee and the second one PE of type PTEmployee. Test your program for the following:
Entering invalid date;
Call every method above for each of E and PE and print the return value( if it returns a value);
Declare two variables EV of type Employee and PEV of type PTEmployee and the following four assignments:
EV=E
Call every one of the following methods: EV.setPay, EV.getPay, EV.setWages EV.getWages,
EV.raise( 4.0), getDateOfBirth() and then print the info of EV.
EV=PE
Call every one of the following methods: EV.setPay, EV.getPay, EV.setWages EV.getWages,
EV.raise( 4.0), getDateOfBirth() and then print the info of EV.
PEV=E
Call every one of the following methods: EV.setPay, EV.getPay, EV.setWages EV.getWages, EV.raise( 4.0), getDateOfBirth() and then print the info of PEV.
PEV=E
Call every one of the following methods: EV.setPay, EV.getPay, EV.setWages EV.getWages, EV.raise( 4.0), getDateOfBirth() and then print the info of PEV.
You may get error messages when you run some of the above methods. Explain the reason and comment out those methods, one at time, and rerun the program.
Explanation / Answer
Java code:
Date class:
import java.util.LinkedHashMap;
import java.util.Map;
//Date class-STARTS
public class Date
{
//data attributes (fields)
private String month;
private int day,year;
//to add months and corresponding days
Map<String,Integer> month_Days=new LinkedHashMap<String,Integer>();
//constructor
Date(String m, int d, int y)
{
//calling validate() method
if(validate(m,d,y))
{
month=m;
day=d;
year=y;
}
else
{
System.err.println("Entered date is wrong.");
}
}
private boolean validate(String m,int d,int y)
{
boolean monthValid=false;
boolean dayValid=false;
boolean yearValid=false;
//converting month to Upper case letters
m=m.toUpperCase();
//adding months and days
month_Days.put("JANUARY",31);
//for leap year, 29days
if(y%4==0)
month_Days.put("FEBRUARY",29);
else
month_Days.put("FEBRUARY",28);
month_Days.put("MARCH",31);
month_Days.put("APRIL",30);
month_Days.put("MAY",31);
month_Days.put("JUNE",30);
month_Days.put("JULY",31);
month_Days.put("AUGUST",31);
month_Days.put("SEPTEMBER",30);
month_Days.put("OCTOBER",31);
month_Days.put("NOVEMBER",30);
month_Days.put("DECEMBER",31);
//if map contains month,then month is valid, else invalid
if(month_Days.containsKey(m))
monthValid=true;
else
monthValid=false;
//for year validity
if(y<2050 && y>0)
{
yearValid=true;
//for leap-year
if(y%4==0)
{
//number of days in February must be between 1 to 29
if(month_Days.get("FEBRUARY")>1 && month_Days.get("FEBRUARY")<=29)
{
dayValid=true;
}else
{
//else invalid
dayValid=false;
}
}else //for non-leap year
{
//number of days in February must be between 1 to 28
if(month_Days.get("FEBRUARY")>1 && month_Days.get("FEBRUARY")<=28)
{
dayValid=true;
}else
{
dayValid=false;
}
}
}else{
yearValid=false;
}
if(monthValid & dayValid & yearValid)
return true;
else
return false;
}
//toString() method
public String toString()
{
return month+","+day+","+year;
}
}
//Date class-ENDS
Employee class:
//Employee class-STARTS
public class Employee
{
//data attributes (fields)
String name, ssn, position;
Date dateOfBirth;
float pay;
public Employee() {}
//constructor
public Employee(String name, String ssn, String position, Date dateOfBirth, float pay)
{
super();
this.name = name;
this.ssn = ssn;
this.position = position;
this.dateOfBirth = dateOfBirth;
this.pay = pay;
}
// method toString()
public String toString()
{
return "Name:"+name+" SSN:"+ssn+" Position:"+position+" "
+"Date of Birth:" +dateOfBirth+" Pay:"+pay;
}
//increases the pay by the given percentage, per.
public void raise(float per)
{
pay=pay+per;
}
//setter and getter methods for each attributes
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public float getPay() {
return pay;
}
public void setPay(float pay) {
this.pay = pay;
}
}
//Employee class-ENDS
PTEmployee class:
//PTEmployee class which extends Employee class
public class PTEmployee extends Employee
{
//two additional attributes: nHours and wages.
int nHours;
float wages;
//constructor
public PTEmployee(String name,String ssn,String position,Date dateOfBirth,float pay,
int nHours, float wages)
{
super(name,ssn,position,dateOfBirth,pay);
this.nHours = nHours;
this.wages = wages;
}
public PTEmployee() {
// TODO Auto-generated constructor stub
}
//method to raise wage by given amount
public void raise(float amount)
{
wages=wages+amount;
//calling computePay() method after wage raised by given amount
this.computePay();
}
//computing pay
public void computePay()
{
pay=nHours*wages;
}
//getter and setter methods
public int getnHours() {
return nHours;
}
public void setnHours(int nHours) {
this.nHours = nHours;
}
public float getWages() {
return wages;
}
public void setWages(float wages) {
this.wages = wages;
}
//toString() method
public String toString()
{
//using toString() method of the super class Employee
return super.toString()+" nHours:"+nHours+" wages:"+wages+" ";
}
}
EmployeeDemo class:
public class EmployeeDemo {
public static void main(String[] args) {
//employee info from the keyboard
Date dob = new Date("MARCH",20,1989);
System.out.println(dob.toString());
Date dob1 = new Date("AUGUST",8,1992);
System.out.println(dob1.toString());
//creates two objects E of type Employee and second one PE of type PTEmployee.
Employee E = new Employee("Joe","12910ID","Manager",dob,45000);
PTEmployee PE = new PTEmployee("Mathew","10223ID","Assistant Manager",dob1,45000,8,3400);
System.out.println(" Employee Details:");
//calling toString() method of Employee class
System.out.println(E.toString());
System.out.println(" PTEmployee Details:");
//calling toString() method of PTEmployee class
System.out.println(PE.toString());
//Declare two variables EV of type Employee and PEV of type PTEmployee
Employee EV = new Employee();
PTEmployee PEV = new PTEmployee();
//Assignment
EV=E;
EV.setPay(1200);
System.out.println(EV.getPay());
//EV.setWages(100);
//EV.getWages();
EV.raise(4.0f);
System.out.println(EV.getDateOfBirth());
//print the info of EV.
System.out.println(EV.toString());
//Assignment
EV=PE;
EV.setPay(900);
System.out.println(EV.getPay());
//EV.setWages();
//EV.getWages();
EV.raise(4.0f);
System.out.println(EV.getDateOfBirth());
System.out.println(EV.toString());
//Assignment
//PEV=E;
EV.setPay(123);
EV.getPay();
//EV.setWages();
//EV.getWages();
EV.raise( 4.0F);
System.out.println(EV.getDateOfBirth());
}
}
Output:
ARCH,20,1989
AUGUST,8,1992
Employee Details:
Name:Joe
SSN:12910ID
Position:Manager
Date of Birth:MARCH,20,1989
Pay:45000.0
PTEmployee Details:
Name:Mathew
SSN:10223ID
Position:Assistant Manager
Date of Birth:AUGUST,8,1992
Pay:45000.0
nHours:8
wages:3400.0
1200.0
MARCH,20,1989
Name:Joe
SSN:12910ID
Position:Manager
Date of Birth:MARCH,20,1989
Pay:1204.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.