1. Define the class Date which has the data attributes (fields) month, day, and
ID: 3914506 • Letter: 1
Question
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.
PLEASE INCLUDE ALL PARTS ASKED
Explanation / Answer
Hi,
Since you had not mentioned any specific language, I have C# to write the code. I have pasted the classes in order Date, Employee, PTEmployee and finally the program. I have included all the calls explained in part 4. I have refactored the code very well and have commented on error scenarios with reason.
Still if you face any difficulty running or understanding the code please contact.
If it answers your question please give me thumbs up. Thanks.
I wish you all the best in your study :)
Date Class ->
using System;
namespace Chegg_Employee_Date
{
enum Month
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
class Date
{
Month month;
internal Month Month
{
get { return month; }
set
{
Month result;
if(Enum.TryParse<Month>(value.ToString(), true, out result))
{
month = result;
}
else
{
}
}
}
int day;
int year;
public Date(String m, int d, int y)
{
if(!Validate(m, d, y))
{
Console.WriteLine("Invalid date!");
Console.WriteLine("Please reenter correct date");
}
}
private bool Validate(String m, int d, int y)
{
if (y < 2050)
{
year = y;
}
else
{
return false;
}
Month result;
if (Enum.TryParse<Month>(m.ToString(), true, out result))
{
month = result;
}
else
{
return false;
}
if (month == Month.January || month == Month.March || month == Month.May || month == Month.July || month == Month.August || month == Month.October || month == Month.December)
{
if (d >= 1 && d <= 31)
{
day = d;
}
else
{
return false;
}
}
else if (month == Month.April || month == Month.June || month == Month.September || month == Month.November)
{
if (d >= 1 && d <= 30)
{
day = d;
}
else
{
return false;
}
}
else if (month == Month.February)
{
if (checkLeapYear(year) && d >= 1 && d <= 29)
{
day = d;
}
else if (d >= 1 && d <= 28)
{
day = d;
}
else
{
return false;
}
}
return true;
}
public string toString()
{
return month.ToString() + ", " + day + ", " + year;
}
bool checkLeapYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is muliplt of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is muliplt of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
}
}
Employee Class ->
namespace Chegg_Employee_Date
{
class Employee
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string ssn;
public string SSN
{
get { return ssn; }
set { ssn = value; }
}
string position;
public string Position
{
get { return position; }
set { position = value; }
}
Date dateofBirth;
internal Date DateofBirth
{
get { return dateofBirth; }
set { dateofBirth = value; }
}
float pay;
public float Pay
{
get { return pay; }
set { pay = value; }
}
public Employee(string name, string ssn, string position, Date dateOfBirth, float pay)
{
Name = name;
SSN = ssn;
Position = position;
DateofBirth = dateofBirth;
Pay = pay;
}
public string toString()
{
return Name + " :" + SSN + " :" + Position + " :" + DateofBirth + " :" + Pay;
}
public void raise(float per)
{
pay = pay + (pay * (per / 100));
}
}
}
PTEmployee class ->
namespace Chegg_Employee_Date
{
class PTEmployee : Employee
{
int nHours;
public int NHours
{
get { return nHours; }
set { nHours = value; }
}
float wages;
public float Wages
{
get { return wages; }
set { wages = value; }
}
public PTEmployee(string name, string ssn, string position, Date dateOfBirth, int nHours, int wages)
: base(name, ssn, position, dateOfBirth, 0)
{
NHours = nHours;
Wages = wages;
Pay = nHours * wages;
}
public string toString()
{
return base.toString() + " :" + NHours + " :" + Wages;
}
public void raise(float amount)
{
Wages = Wages + amount;
Pay = nHours * wages;
}
}
}
EmployeeDemo program ->
using System;
namespace EmployeeDemo
{
class Program
{
static void Main(string[] args)
{
Employee E = CreateEmployee();
Console.WriteLine(E.Name);
Console.WriteLine(E.SSN);
Console.WriteLine(E.Position);
Console.WriteLine(E.DateofBirth);
Console.WriteLine(E.Pay);
Console.WriteLine(E.toString());
PTEmployee PE = CreatePTEmployee();
Console.WriteLine(PE.Name);
Console.WriteLine(PE.SSN);
Console.WriteLine(PE.Position);
Console.WriteLine(PE.DateofBirth);
Console.WriteLine(PE.NHours);
Console.WriteLine(PE.Wages);
Console.WriteLine(PE.Pay);
Console.WriteLine(PE.toString());
Employee EV;
PTEmployee PEV;
EV = E;
EV.Pay = 100;
Console.WriteLine(EV.Pay);
//EV.Wages = 15; (*This call is invalid since EV is of type Employee*)
//Console.WriteLine(EV.Wages);(*This call is invalid since EV is of type Employee*)
EV.raise(4.0);
Console.WriteLine(EV.DateofBirth);
Console.WriteLine(EV.toString());
EV = PE;
EV.Pay = 100;
Console.WriteLine(EV.Pay);
//EV.Wages = 15; (*This call is invalid since EV is of type Employee*)
//Console.WriteLine(EV.Wages);(*This call is invalid since EV is of type Employee*)
EV.raise(4.0);
Console.WriteLine(EV.DateofBirth);
Console.WriteLine(EV.toString());
PEV = E;
PEV.Pay = 100;
Console.WriteLine(PEV.Pay);
PEV.Wages = 15;
Console.WriteLine(PEV.Wages);
PEV.raise(4.0);
Console.WriteLine(PEV.DateofBirth);
Console.WriteLine(PEV.toString());
PEV = PE;
PEV.Pay = 100;
Console.WriteLine(PEV.Pay);
PEV.Wages = 15;
Console.WriteLine(PEV.Wages);
PEV.raise(4.0);
Console.WriteLine(PEV.DateofBirth);
Console.WriteLine(PEV.toString());
}
private static Employee CreateEmployee()
{
Console.WriteLine("Enter name");
string name = Console.ReadLine();
Console.WriteLine("Enter ssn");
string ssn = Console.ReadLine();
Console.WriteLine("Enter position");
string position = Console.ReadLine();
Date dateOfBirth = CreateDateOfBirth();
Console.WriteLine("Enter pay");
float pay = float.Parse(Console.ReadLine());
return new Employee(name, ssn, position, dateOfBirth, pay);
}
private static PTEmployee CreatePTEmployee()
{
Console.WriteLine("Enter name");
string name = Console.ReadLine();
Console.WriteLine("Enter ssn");
string ssn = Console.ReadLine();
Console.WriteLine("Enter position");
string position = Console.ReadLine();
Date dateOfBirth = CreateDateOfBirth();
Console.WriteLine("Enter nHours");
int nHours = int.Parse(Console.ReadLine());
Console.WriteLine("Enter wages");
int wages = int.Parse(Console.ReadLine());
return new PTEmployee(name, ssn, position, dateOfBirth, nHours, wages);
}
private static Date CreateDateOfBirth()
{
Console.WriteLine("Enter dateofBirth");
Console.WriteLine(" Enter day");
int day = int.Parse(Console.ReadLine());
Console.WriteLine(" Enter month");
string month = Console.ReadLine();
Console.WriteLine(" Enter year");
int year = int.Parse(Console.ReadLine());
return new Date(month, day, year);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.