In: Computer Science
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:
NAME: <lastName + “, “ + firstName>
ID NUMBER: <Id-Number>
BIRTH DAY: <string-birth-day>
DATE HIRED: < string-date-hired >
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.
GROSS PAY: <gross-pay>
TAX DEDUCTION: <tax-deduction>
NET PAY: <Net-pay>
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( )).
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);
}
}
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 *********************************************************