Question

In: Computer Science

Using the class Date that you defined in Exercise O3, write the definition of the class...

  1. Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables:
  • first name               -           class string
  • last name                -           class string
  • ID number             -           integer (the default ID number is 999999)
  • birth day                -           class Date
  • date hired               -           class Date
  • base pay                 -           double precision (the default base pay is $0.00)

The default constructor initializes the first name to “john”, the last name to “Doe”, and the birth day and hired day to the default date (1/1/1960).

In addition to the constructors, the class has the following public instance methods:

  • void readPInfo(Scanner scan ) that uses the Scanner object parameter to read the values for the instance variables first name, last name, ID number, birth day, and date of hire.
  • void readPayInfo(Scanner scan ) that uses the Scanner object parameter to read the value for the base pay instance variable.
  • String getPInfoString( ) that returns a string in the following format:

                  NAME: <lastName + “, “ +   firstName>

                  ID NUMBER: <Id-Number>

                BIRTH DAY: <string-birth-day>

DATE HIRED: < string-date-hired >

  • void setBpay( double newBpay ) that sets the value of the base pay to the new value, newBpay.
  • double getBpay( ) that returns the value of the base pay instance variable.
  • double getGpay( ) that returns the value of the gross pay (which is the base pay).
  • double computeTax(   ) that computes the tax deduction on the gross pay and returns it as follows:

If gross pay is greater than or equal to 1000, 20% of the gross pay;

If 800 <= gross pay < 1000, 18% of gross pay

If 600 <= gross pay < 800, 15% of gross pay

Otherwise, 10 % of the gross pay.

  • String getPayInfoString( ) that returns a string in the following format:

                  GROSS PAY: <gross-pay>

                  TAX DEDUCTION: <tax-deduction>

                NET PAY: <Net-pay>

  1. Define another class named ExerciseO4 that contains the method main that does the following:
  1. Define an object and instantiate it with the default constructor, then output its personal information (by calling the instance methods getPInfoString( ).
  2. Define an object and initialize its instance variables as follows:

John   Doe   111111   10/25/1990   11/15/2010 750.00

And then output its personal and pay information (by calling the instance methods getPInfoString( ) and getPayInfoString( )).

  1. Define an object, read its personal and pay information (by calling the methods readPInfo(Scanner scan ) and readPayInfo(Scanner scan )) , and then output its personal and pay information (by calling the instance methods getPInfoString( ) and getPayInfoString( )).
  1. Define an object and instantiate it (or read the values for its instance variables) with an invalid date (date of birth or date of hire).

excercise 03 is the code written below

import java.util.Scanner;

class Date {

   private int month; // to hold the month (1 – 12)

   private int day; // to hold the day (1 – 31)

   private int year; // to hold the year 1960 - 2019

   static final String[] monthList = { " ", "January", "February", "March", "April", "May", "June", "July", "August",

           "September", "October", "November", "December" };

   public Date() // default constructor

   {

       month = 1;

       day = 1;

       year = 1960;

   }

   public Date(int newMonth, int newDay, int newYear) // constructor

   {

       month = newMonth;

       day = newDay;

       year = newYear;

       checkDate();

   }

   public void inputDate(Scanner scan) // use the Scanner object to read the month and the day

   {

       month = scan.nextInt(); // read the month

       day = scan.nextInt(); // read the day

       year = scan.nextInt(); // read the year

       checkDate();

   }

   private void checkDate() // to validate the month and the day

   {

       final int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

       if ((month < 1) || (month > 12) || (day < 1) || (day > daysPerMonth[month]) || year < 1960 || year > 2016) {

           System.out.println("Invalid date");

           System.exit(0);

       }

   }

   String getStringDate() // returns the string consisting of the month followed by the day

   {

       return (monthList[month] + " " + day+" "+year);

   }

   public int getMonth() // to return the month

   {

       return (month);

   }

   public int getDay() // to return the day

   {

       return (day);

   }

   public int getYear() // to return year

   {

       return (year);

   }

   public boolean equalTo(Date obj) {

       return (month == obj.month && day == obj.day && year == obj.year);

   }

   //which Date object is greater

   public static boolean isGreaterThan(Date obj1, Date obj2) {

       if(obj1.year > obj2.year)

           return true;

       else if(obj1.year == obj2.year) {

           if(obj1.month > obj2.month)

               return true;

           else if(obj1.month == obj2.month) {

               if(obj1.day > obj2.day)

                   return true;

           }

       }

     

       return false;

   }

}

public class Test {//driver class

   /*------- Program is executed with a month and a day as command line arguments -----*/

   public static void main(String[] args) {

       Date defaultDate = new Date(); // to hold today’s month and day

       System.out.println(defaultDate.getStringDate());

     

       /*------------------------------------ read today’s month and day ---------------------*/

       Date today = new Date(3, 25, 2016); // set Today's date

       System.out.println("Todays day is:\t"+today.getStringDate());

     

       /*------------------------------------ read dueDate month and day ---------------------*/

       Date dueDate = new Date();

       Scanner input = new Scanner(System.in);

       System.out.println("Enter today’s month, day and year:");

       dueDate.inputDate(input);

       /*------------------------------------ compare today and dueDate ---------------------*/

       if (today.equalTo(dueDate))

           System.out.println("Your project is on time");

       else {

           if(Date.isGreaterThan(today, dueDate))

             System.out.println("Your project is late");

           else

               System.out.println("Your project is early");

       }

     

       /*------------------------------------ inavlid date ---------------------*/

       Date invalidDate = new Date(25, 4, 2000);

   }

}

Solutions

Expert Solution

Date.java

import java.util.Scanner;

class Date {

private int month; // to hold the month (1 – 12)
private int day; // to hold the day (1 – 31)
private int year; // to hold the year 1960 - 2019
static final String[] monthList = {" ", "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

public Date() // default constructor
{
month = 1;
day = 1;
year = 1960;
}

public Date(int newMonth, int newDay, int newYear) // constructor
{
month = newMonth;
day = newDay;
year = newYear;
checkDate();
}

public void inputDate(Scanner scan) // use the Scanner object to read the month and the day
{
month = scan.nextInt(); // read the month
day = scan.nextInt(); // read the day
year = scan.nextInt(); // read the year
scan.nextLine();
checkDate();
}

private void checkDate() // to validate the month and the day
{
final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((month < 1) || (month > 12) || (day < 1) || (day > daysPerMonth[month]) || year < 1960 || year > 2016) {
System.out.println("Invalid date");
System.exit(0);
}
}

String getStringDate() // returns the string consisting of the month followed by the day
{
return (monthList[month] + " " + day + " " + year);
}

public int getMonth() // to return the month
{
return (month);
}

public int getDay() // to return the day
{
return (day);
}

public int getYear() // to return year
{
return (year);
}

public boolean equalTo(Date obj) {
return (month == obj.month && day == obj.day && year == obj.year);
}

//which Date object is greater
public static boolean isGreaterThan(Date obj1, Date obj2) {
if (obj1.year > obj2.year) {
return true;
} else if (obj1.year == obj2.year) {
if (obj1.month > obj2.month) {
return true;
} else if (obj1.month == obj2.month) {
if (obj1.day > obj2.day) {
return true;
}
}
}
return false;
}
}

Employee.java

import java.util.Scanner;

public class Employee {
private String firstName, lastName;
private int idNumber;
private Date birthDay, dateHired;
private double basePay;
  
public Employee()
{
setFirstName("john");
setLastName("doe");
setIdNumber(999999);
setBirthDay(new Date());
setDateHired(new Date());
setBPay(0.0);
}

public Employee(String firstName, String lastName, int idNumber, Date birthDay, Date dateHired, double basePay) {
setFirstName(firstName);
setLastName(lastName);
setIdNumber(idNumber);
setBirthDay(birthDay);
setDateHired(dateHired);
setBPay(basePay);
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getIdNumber() {
return idNumber;
}

public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

public Date getDateHired() {
return dateHired;
}

public void setDateHired(Date dateHired) {
this.dateHired = dateHired;
}

public double getBPay() {
return basePay;
}
  
public double getGPay() {
return basePay;
}

public void setBPay(double newBpay) {
this.basePay = newBpay;
}
  
public void readPInfo(Scanner scan)
{
System.out.print("Enter first name: ");
String fName = scan.nextLine().trim();
System.out.print("Enter last name: ");
String lName = scan.nextLine().trim();
System.out.print("Enter id number: ");
int idNum = Integer.parseInt(scan.nextLine().trim());
Date bDay = new Date();
Date hireDate = new Date();
System.out.print("Enter birth day (mm dd yyyy): ");
bDay.inputDate(scan);
System.out.print("Enter date hired (mm dd yyyy): ");
hireDate.inputDate(scan);
setFirstName(fName);
setLastName(lName);
setIdNumber(idNum);
setBirthDay(bDay);
setDateHired(hireDate);
}
  
public void readPayInfo(Scanner scan)
{
System.out.print("Enter base pay: $");
double bPay = Double.parseDouble(scan.nextLine().trim());
setBPay(bPay);
}
  
public String getPInfoString()
{
return("NAME: " + getLastName() + ", " + getFirstName() + "\n"
+ "ID NUMBER: " + getIdNumber() + "\n"
+ "BIRTH DAY: " + getBirthDay().getStringDate() + "\n"
+ "DATE HIRED: " + getDateHired().getStringDate());
}
  
public double computeTax()
{
double tax;
if(getGPay() >= 1000)
tax = (0.2 * getGPay());
else if(getGPay() >= 800 && getGPay() < 1000)
tax = (0.18 * getGPay());
else if(getGPay() >= 600 && getGPay() < 800)
tax = (0.15 * getGPay());
else
tax = (0.1 * getGPay());
return tax;
}
  
public String getPayInfoString()
{
return("GROSS PAY: $" + String.format("%,.2f", getGPay()) + "\n"
+ "TAX DEDUCTION: $" + String.format("%,.2f", computeTax()) + "\n"
+ "NET PAY: $" + String.format("%,.2f", (getGPay() - computeTax())));
}
}

Exercise04.java (Main class)

import java.util.Scanner;

public class ExerciseO4 {
  
public static void main(String[] args) {
Employee emp1 = new Employee();
System.out.println("Employee object with default constructor..\n" + emp1.getPInfoString());
Employee emp2 = new Employee("John", "Doe", 111111, new Date(10, 25, 1990), new Date(11, 15, 2010), 750.00);
System.out.println("\nEmployee object with parameterized constructor..");
System.out.println("Personal Info:\n--------------\n" + emp2.getPInfoString());
System.out.println("Pay Info:\n---------\n" + emp2.getPayInfoString());
System.out.println("\nEmployee object with info read from user..");
Scanner scan = new Scanner(System.in);
Employee emp3 = new Employee();
System.out.println("Reading personal info..");
emp3.readPInfo(scan);
System.out.println("Reading pay info..");
emp3.readPayInfo(scan);
System.out.println("Displaying user defined employee object..");
System.out.println("Personal Info:\n--------------\n" + emp3.getPInfoString());
System.out.println("Pay Info:\n---------\n" + emp3.getPayInfoString());
System.out.println("\nEmployee object with invalid date..");
Employee emp4 = new Employee("John", "Smith", 333333, new Date(02, 30, 1990), new Date(07, 18, 2000), 560.77);
System.out.println(emp4.getPInfoString());
}
}

******************************************************* SCREENSHOT *********************************************************


Related Solutions

(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
CODE must using C++ language. Write the definition of the class dayTye that implements the day...
CODE must using C++ language. Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType 1. set the day 2. display the day as a string - Sunday, ... Saturday 3. return the day as an integer 4. return the next as an integer 5. return the previous...
The Date Class This class will function similarly in spirit to the Date class in the...
The Date Class This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
. Write a function in JAVA called shorten that is defined in the class P8 and...
. Write a function in JAVA called shorten that is defined in the class P8 and shortens each element of an array of strings. Every string with more than two characters is cut down to its first two characters. For example, a program that uses the function shorten follows. public class P8 { public static void main(String args[]) { String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"}; shorten(x); for (int i = 0; i < 6; i++) System.out.print(x[i] +...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
House Class The first class you should develop is a House class, as defined in the...
House Class The first class you should develop is a House class, as defined in the assignment specification. As we have not yet covered inheritance or association, we will not be integrating Banner this week — you will still be writing it for later usage though. Hence, the complete class should be implemented as defined in the UML below (please see the note on interpreting UML). You should maintain the order of the class fields in field declarations, and parameter...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT