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...
Write a Java code to represent a 1. Date class. As date class is composed of...
Write a Java code to represent a 1. Date class. As date class is composed of three attributes, namely month, year and day; so the class contains three Data Members, and one method called displayDate() which will print these data members. Test the Date class using main class named DateDemo. Create two objects of date class. Initialize the data fields in Date class using the objects, invoke the method displyaDate(). Date month : String year: int day : int displayDate():...
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...
For this exercise, you are going to write your code in the FormFill class instead of...
For this exercise, you are going to write your code in the FormFill class instead of the main method. The code is the same as if you were writing in the main method, but now you will be helping to write the class. It has a few instance variables that stores personal information that you often need to fill in various forms, such as online shopping forms. Read the method comments for more information. As you implement these methods, notice...
Background For this exercise, you will write an Appointment class that tracks information about an appointment...
Background For this exercise, you will write an Appointment class that tracks information about an appointment such as the start and end times and a name for the appointment. Your Appointment class will also have an overlaps() method that takes another Appointment object as an argument and determines whether the two appointments overlap in python. Instructions Write a class called Appointment with methods as described below: __init__() method Define an __init__() method with four parameters: self name, a string indicating...
Please write this program in C++ Write a program that           - a user-defined class Cuboid...
Please write this program in C++ Write a program that           - a user-defined class Cuboid which has the following elements:                    - default constructor (has no parameters)                    - constructor that has 3 parameters (height, length, width)                    - data members                              - height, length, width                    - member functions                              - to set the value of each of the data members - 3 functions                              - to get the value of each of the data members - 3...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT