My assignemnt is this 1. Define the class Date which has the data attributes (fi
ID: 3914499 • Letter: M
Question
My assignemnt is this
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.
THIS IS WHAT I HAVE SO FAR BUT I GET THESE ERROR CODES PLEASE HELP FIX
EmployeeDemo.java:80: error: incompatible types: Employee cannot be converted to PTEmployee
PEV = E;
^
EmployeeDemo.java:83: error: cannot find symbol
PEV.setWage(84);
^
symbol: method setWage(int)
location: variable PEV of type PTEmployee
2 errors
import java.util.Scanner;
public class EmployeeDemo
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int day, year, payAsInt, hours, day2, year2, payAsInt2, wagesAsInt;
String month, name, ssn,pos, ssn2, pos2, month2;
float pay, pay2, wages;
System.out.println("Please enter the name");
name = kb.nextLine();
System.out.println("Please enter the ssn");
ssn = kb.nextLine();
System.out.println("Please enter the position");
pos = kb.nextLine();
System.out.println("Please enter the DOB beginning with the month");
month = kb.nextLine();
System.out.println("Next, please enter the day");
day = kb.nextInt();
System.out.println("Lastly, please enter the year");
year = kb.nextInt();
System.out.println("Please enter the pay");
payAsInt = kb.nextInt();
pay = payAsInt;
Date dob = new Date(month,day,year);
System.out.println(dob.toString());
System.out.println("The next following instructions will be for the next employee");
System.out.println("Please enter the name");
String name2 = kb.nextLine();
System.out.println("Please enter the ssn");
ssn2 = kb.nextLine();
System.out.println("Please enter the position");
pos2 = kb.nextLine();
System.out.println("Please enter the DOB beginning with the month");
month2 = kb.nextLine();
System.out.println("Next, please enter the day");
day2 = kb.nextInt();
System.out.println("Lastly, please enter the year");
year2 = kb.nextInt();
System.out.println("Please enter the pay");
payAsInt2 = kb.nextInt();
pay2 = payAsInt2;
System.out.println("Please enter the hours");
hours = kb.nextInt();
System.out.println("Please enter the wage");
wagesAsInt = kb.nextInt();
wages = wagesAsInt;
Date dob1 = new Date(month2,day2,year2);
System.out.println(dob1.toString());
Employee E = new Employee(name,ssn,pos,dob,pay);
PTEmployee PE = new PTEmployee(name2,ssn2,pos2,dob1,pay2,hours,wages);
System.out.println(" Employee Details:");
System.out.println(E.toString());
System.out.println(" PTEmployee Details:");
System.out.println(PE.toString());
Employee EV = new Employee();
PTEmployee PEV = new PTEmployee();
//p1
EV=E;
EV.setPay(1200);
System.out.println(EV.getPay());
EV.raise(4.0f);
System.out.println(EV.getDateOfBirth());
System.out.println(EV.toString());
// raise method needs to be a float
//ev cant use setwage or getwage since it is not extended to PTE
//p2
EV=PE;
EV.setPay(900);
//ev cant use wage since it is not extended
System.out.println(EV.getPay());
EV.raise(4.0f);
System.out.println(EV.getDateOfBirth());
System.out.println(EV.toString());
EV.setPay(123);
EV.getPay();
EV.raise(4.0F);
System.out.println(EV.getDateOfBirth());
//p3 PEV = E
PEV = E;
PEV.setPay(123);
System.out.println("PEV.getPay()");
PEV.setWage(84);
System.out.println("Pev.getWage()");
PEV.raise(4.0F);
System.out.println(EV.getDateOfBirth());
}
}
public class Employee
{
String name, ssn, position;
Date dateOfBirth;
float pay;
public Employee(){}
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;
}
public String toString()
{
return "Name:"+name+" SSN:"+ssn+" Position:"+position+" "
+"Date of Birth:" +dateOfBirth+" Pay:"+pay;
}
public void raise(float per)
{
pay = pay + ((per/100)*pay);
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setSsn(String ssn)
{
this.ssn = ssn;
}
public String getSsn()
{
return ssn;
}
public void setPosition(String position)
{
this.position = position;
}
public String getPosition()
{
return position;
}
public void setDateOfBirth(Date dateOfBirth)
{
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfBirth()
{
return dateOfBirth;
}
public void setPay(float pay)
{
this.pay = pay;
}
public float getPay()
{
return pay;
}
}
public class PTEmployee extends Employee
{
private int nHours;
private float wages;
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(){
//constructor stub
}
public void raise(float amount)
{
wages=wages+amount;
//calling computePay() method after wage raised by given amount
this.computePay();
}
public void computePay()
{
pay = nHours*wages;
}
public int getnHours()
{
return nHours;
}
public void setnHours(int nHours)
{
this.nHours = nHours;
}
public void setWages(float wages)
{
this.wages = wages;
}
public String toString()
{
return super.toString()+" nHours:"+nHours+" wages:"+wages+" ";
//used toString emth from super clas
}
}
import java.util.LinkedHashMap;
import java.util.Map;
public class Date
{
private int day, year;
private String month;
Map month_Days=new LinkedHashMap();
Date(String m,int d, int y)
{
if(validate(m,d,y))
{
month = m;
day = d;
y = year;
}
else
System.out.println("Date inputed is wrong");
}
private boolean validate(String m, int d, int y)
{
boolean monthValid = false;
boolean dayValid=false;
boolean yearValid=false;
m = m.toUpperCase();
//adding the d/m here
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;
if(y<2050)
{
yearValid = true;
if(y%4 == 0)
{
if(month_Days.get("FEBRUARY")>1 && month_Days.get("FEBRUARY")<=29)
{
dayValid=true;
}
else
dayValid = false;
}
else //4 non leap
{
//num of days in feb 1-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;
}
public String toString()
{
return month+","+day+","+year;
}
}
Explanation / Answer
package inheritance;
import java.util.LinkedHashMap;
import java.util.Map;
// Defines a class Date
public class Date
{
// Instance variable to store day, month and year
private int day, year;
private String month;
// Declares a Map object to store month name and maximum day
Map month_Days = new LinkedHashMap();
// Parameterized constructor to initialize parameter date data to instance variables
Date(String m,int d, int y)
{
// Calls the method to validate date
if(validate(m, d, y))
{
month = m;
day = d;
year = y;
}// End of if condition
// Otherwise, invalid date displays error message
else
System.out.println("ERROR: Date inputed is wrong");
}// End of parameterized constructor
// Method to return true if date is valid otherwise returns false
private boolean validate(String m, int d, int y)
{
// Initializes all the status of day, month and year to false
boolean monthValid = false;
boolean dayValid=false;
boolean yearValid=false;
// Converts the months to upper case
m = m.toUpperCase();
// Adding the month and date to map
month_Days.put("JANUARY", 31);
// Checks for leap year, 29days
if(y % 4 == 0)
month_Days.put("FEBRUARY", 29);
// Otherwise, not a leap year
else
month_Days.put("FEBRUARY", 28);
// Adding the month and date to map
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);
// Checks if map contains month, then month is valid, else invalid
if(month_Days.containsKey(m))
monthValid = true;
else
monthValid = false;
// Checks if year is less than 2050
if(y < 2050)
{
yearValid = true;
// Checks for leap year
if(y % 4 == 0)
{
// Checks if the month is February and day is greater than one and less than equals to 29 set valid day
if((int)month_Days.get("FEBRUARY") > 1 && (int)month_Days.get("FEBRUARY") <= 29)
dayValid = true;
// Otherwise set invalid
else
dayValid = false;
}// End of if condition
// Otherwise not a leap year
else
{
// Checks if the month is February and day is greater than one and less than equals to 28 set valid day
if((int)month_Days.get("FEBRUARY") > 1 && (int)month_Days.get("FEBRUARY") <= 28)
dayValid = true;
// Otherwise set invalid
else
dayValid = false;
}// End of else
}// End of outer if condition
// Otherwise invalid year set the status to false
else
yearValid = false;
// Checks if the month, day and year status is true
if(monthValid & dayValid & yearValid)
return true;
// Otherwise status is false
else
return false;
}// End of method
// Method to return date
public String toString()
{
return month + "," + day + "," + year;
}// End of method
}// End of class
------------------------------------------------------------------------------------
package inheritance;
// Defines class Employee
public class Employee
{
// Instance variable to store employee data
String name, ssn, position;
Date dateOfBirth;
float pay;
// Default constructor definition
public Employee()
{
name = ssn = position = "";
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variable
public Employee(String name, String ssn, String position, Date dateOfBirth, float pay)
{
// Calls the default constructor
this();
this.name = name;
this.ssn = ssn;
this.position = position;
this.dateOfBirth = dateOfBirth;
this.pay = pay;
}// End of parameterized constructor
// Method to return employee information
public String toString()
{
return "Name: " + name + " SSN: " + ssn + " Position :" + position + " "
+ "Date of Birth: " + dateOfBirth + " Pay: " + pay;
}// End of method
// Method to raise the pay
public void raise(float per)
{
pay = pay + ((per/100)*pay);
}// End of method
// Method to set name
public void setName(String name)
{
this.name = name;
}// End of method
// Method to return name
public String getName()
{
return name;
}// End of method
// Method to set ssn
public void setSsn(String ssn)
{
this.ssn = ssn;
}// End of method
// Method to return ssn
public String getSsn()
{
return ssn;
}// End of method
// Method to set position
public void setPosition(String position)
{
this.position = position;
}// End of method
// Method to return position
public String getPosition()
{
return position;
}// End of method
// Method to set dob
public void setDateOfBirth(Date dateOfBirth)
{
this.dateOfBirth = dateOfBirth;
}// End of method
// Method to return dob
public Date getDateOfBirth()
{
return dateOfBirth;
}// End of method
// Method to set pay
public void setPay(float pay)
{
this.pay = pay;
}// End of method
// Method to return pay
public float getPay()
{
return pay;
}// End of method
}// End of class
--------------------------------------------------------------------------------------------
package inheritance;
// Class PTEmployee derived from class Employee
public class PTEmployee extends Employee
{
// Instance variable to store employee data
private int nHours;
private float wages;
// Parameterized constructor to assign parameter values to instance variable
public PTEmployee(String name,String ssn,String position,Date dateOfBirth,float pay,int nHours, float wages)
{
// Calls the base class constructor
super(name, ssn, position, dateOfBirth, pay);
this.nHours = nHours;
this.wages = wages;
}// End of parameterized constructor
// Default constructor definition
public PTEmployee()
{
nHours = 0;
wages = 0.0f;
}// End of default constructor
// Overrides the base class method to raise the pay
public void raise(float amount)
{
wages = wages + amount;
// Calls computePay() method after wage raised by given amount
this.computePay();
}// End of method
// Method to compute pay
public void computePay()
{
pay = nHours*wages;
}// End of method
// Method to return hours
public int getnHours()
{
return nHours;
}// End of method
// Method to set hours
public void setnHours(int nHours)
{
this.nHours = nHours;
}// End of method
// Method to set wage
public void setWages(float wages)
{
this.wages = wages;
}// End of method
// Method to return wage
public float getWages()
{
return wages;
}// End of method
// Method to return PTEmployee
public String toString()
{
return super.toString() + " nHours: " + nHours + " wages: " + wages + " ";
// used toString emth from super clas
}// End of method
}// End of class
----------------------------------------------------------------------------------------
package inheritance;
import java.util.Scanner;
// Driver class EmployeeDemo definition
public class EmployeeDemo
{
// main method definition
public static void main(String[] args)
{
// Scanner class object created
Scanner kb = new Scanner(System.in);
// Local variable declaration to store data entered by the user
int day, year, payAsInt, hours, day2, year2, payAsInt2, wagesAsInt;
String month, name, ssn,pos, ssn2, pos2, month2;
float pay, pay2, wages;
// Accepts data from user
System.out.print(" Please enter the name: ");
name = kb.nextLine();
System.out.print(" Please enter the ssn: ");
ssn = kb.nextLine();
System.out.print(" Please enter the position: ");
pos = kb.nextLine();
System.out.print(" Please enter the DOB beginning with the month: ");
month = kb.nextLine();
System.out.print(" Next, please enter the day: ");
day = kb.nextInt();
System.out.print(" Lastly, please enter the year: ");
year = kb.nextInt();
System.out.print(" Please enter the pay: ");
payAsInt = kb.nextInt();
pay = payAsInt;
// Creates an object of the class Date using parameterized constructor
Date dob = new Date(month,day,year);
// Displays the date
System.out.println(dob.toString());
// Accepts next employee data
System.out.print(" The next following instructions will be for the next employee: ");
kb.nextLine();
System.out.print(" Please enter the name: ");
String name2 = kb.nextLine();
System.out.print(" Please enter the ssn: ");
ssn2 = kb.nextLine();
System.out.print(" Please enter the position: ");
pos2 = kb.nextLine();
System.out.print(" Please enter the DOB beginning with the month: ");
month2 = kb.nextLine();
System.out.print(" Next, please enter the day: ");
day2 = kb.nextInt();
System.out.print(" Lastly, please enter the year: ");
year2 = kb.nextInt();
System.out.print(" Please enter the pay: ");
payAsInt2 = kb.nextInt();
pay2 = payAsInt2;
System.out.print(" Please enter the hours: ");
hours = kb.nextInt();
System.out.print(" Please enter the wage: ");
wagesAsInt = kb.nextInt();
wages = wagesAsInt;
// Creates an object of the class Date using parameterized constructor
Date dob1 = new Date(month2,day2,year2);
// Displays the date
System.out.println(dob1.toString());
// Creates an Employee class object using parameterized constructor
Employee E = new Employee(name, ssn, pos, dob, pay);
// Creates an PTEmployee class object using parameterized constructor
PTEmployee PE = new PTEmployee(name2, ssn2, pos2, dob1, pay2, hours, wages);
// Displays the employee data
System.out.println(" Employee Details:");
System.out.println(E.toString());
// Displays the pt employee data
System.out.println(" PTEmployee Details:");
System.out.println(PE.toString());
// Creates Employee and PTEmployee class object using default constructor
Employee EV = new Employee();
PTEmployee PEV = new PTEmployee();
//p1
EV = E;
// Calls the method to set pay
EV.setPay(1200);
// Displays the modified pay
System.out.println(EV.getPay());
// Calls the method to raise the pay
EV.raise(4.0f);
// Displays date of birth
System.out.println(EV.getDateOfBirth());
// Displays employee data
System.out.println(EV.toString());
//p2
EV = PE;
// Calls the method to set pay
EV.setPay(900);
// Displays modified pay
System.out.println(EV.getPay());
// Calls the method to raise the pay
EV.raise(4.0f);
// Displays date of birth
System.out.println(EV.getDateOfBirth());
// Displays employee data
System.out.println(EV.toString());
EV.setPay(123);
EV.getPay();
EV.raise(4.0F);
System.out.println(EV.getDateOfBirth());
//p3 PEV = E
PEV = PE;
PEV.setPay(123);
System.out.println("PEV.getPay()");
PEV.setWages(84);
System.out.println("Pev.getWage()");
PEV.raise(4.0F);
System.out.println(EV.getDateOfBirth());
}// End of main method
}// End of class
Sample Output:
Please enter the name: Suresh
Please enter the ssn: 111
Please enter the position: Manager
Please enter the DOB beginning with the month: March
Next, please enter the day: 11
Lastly, please enter the year: 2009
Please enter the pay: 800
March,11,2009
The next following instructions will be for the next employee:
Please enter the name: Amit
Please enter the ssn: 222
Please enter the position: Accountant
Please enter the DOB beginning with the month: May
Next, please enter the day: 3
Lastly, please enter the year: 2005
Please enter the pay: 400
Please enter the hours: 12
Please enter the wage: 30
May,3,2005
Employee Details:
Name: Suresh
SSN: 111
Position :Manager
Date of Birth: March,11,2009
Pay: 800.0
PTEmployee Details:
Name: Amit
SSN: 222
Position :Accountant
Date of Birth: May,3,2005
Pay: 400.0
nHours: 12
wages: 30.0
1200.0
March,11,2009
Name: Suresh
SSN: 111
Position :Manager
Date of Birth: March,11,2009
Pay: 1248.0
900.0
May,3,2005
Name: Amit
SSN: 222
Position :Accountant
Date of Birth: May,3,2005
Pay: 408.0
nHours: 12
wages: 34.0
May,3,2005
PEV.getPay()
Pev.getWage()
May,3,2005
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.