Question

In: Computer Science

This is not an actual question, but figure examples for another assignment. // Fig. 8.7: Date.java...

This is not an actual question, but figure examples for another assignment.

// Fig. 8.7: Date.java
// Date class declaration.

public class Date {
   private int month; // 1-12
   private int day; // 1-31 based on month
   private int year; // any year

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

   // constructor: confirm proper value for month and day given the year
   public Date(int month, int day, int year) {
      // check if month in range
      if (month <= 0 || month > 12) {
         throw new IllegalArgumentException(
            "month (" + month + ") must be 1-12");
      }

      // check if day in range for month
      if (day <= 0 ||
         (day > daysPerMonth[month] && !(month == 2 && day == 29))) {
         throw new IllegalArgumentException("day (" + day +
            ") out-of-range for the specified month and year");
      }

      // check for leap year if month is 2 and day is 29
      if (month == 2 && day == 29 && !(year % 400 == 0 ||
           (year % 4 == 0 && year % 100 != 0))) {
         throw new IllegalArgumentException("day (" + day +
            ") out-of-range for the specified month and year");
      }

      this.month = month;
      this.day = day;
      this.year = year;

      System.out.printf("Date object constructor for date %s%n", this);
   }

   // return a String of the form month/day/year
   public String toString() {
      return String.format("%d/%d/%d", month, day, year);
   }
}

// Fig. 8.8: Employee.java
// Employee class with references to other objects.

public class Employee {
   private String firstName;
   private String lastName;
   private Date birthDate;
   private Date hireDate;

   // constructor to initialize name, birth date and hire date
   public Employee(String firstName, String lastName, Date birthDate,
      Date hireDate) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.birthDate = birthDate;
      this.hireDate = hireDate;
   }

   // convert Employee to String format
   public String toString() {
      return String.format("%s, %s Hired: %s Birthday: %s",
         lastName, firstName, hireDate, birthDate);
   }
}

// Fig. 8.9: EmployeeTest.java
// Composition demonstration.

public class EmployeeTest {
   public static void main(String[] args) {
      Date birth = new Date(7, 24, 1949);
      Date hire = new Date(3, 12, 1988);
      Employee employee = new Employee("Bob", "Blue", birth, hire);

      System.out.println(employee);
   }
}

// Fig. 8.12: Employee.java
// static variable used to maintain a count of the number of
// Employee objects in memory.

public class Employee {
   private static int count = 0; // number of Employees created
   private String firstName;
   private String lastName;

   // initialize Employee, add 1 to static count and
   // output String indicating that constructor was called
   public Employee(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;

      ++count; // increment static count of employees
      System.out.printf("Employee constructor: %s %s; count = %d%n",
         firstName, lastName, count);
   }

   // get first name
   public String getFirstName() {
      return firstName;
   }

   // get last name
   public String getLastName() {
      return lastName;
   }

   // static method to get static count value
   public static int getCount() {          
      return count;                        
   }
}

Solutions

Expert Solution

Date.java

public class Date {
  
private int month;
private int day;
private int year;
  
private static final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  
public Date(int month, int day, int year)
{
// check if month in range
if (month <= 0 || month > 12)
{
throw new IllegalArgumentException("month (" + month + ") must be 1-12");
}
  
if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
{
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}
  
if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}
this.month = month;
this.day = day;
this.year = year;
}
  
@Override
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
}
}

Employee.java

public class Employee {

private static int count = 0; // number of Employees created
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;
private EmployeeStatus employeeStatus;
private double hoursWorked;
  
private enum EmployeeStatus
{
FullTime, PartTime;
}

public Employee(String firstName, String lastName, Date birthDate, Date hireDate, double hoursWorked) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
setHoursWorked(hoursWorked);
if(this.hoursWorked >= 40)
this.employeeStatus = EmployeeStatus.FullTime;
else if(this.hoursWorked < 40)
this.employeeStatus = EmployeeStatus.PartTime;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public static int getCount() {
return count;
}

public Date getBirthDate() {
return birthDate;
}

public Date getHireDate() {
return hireDate;
}

public String getEmployeeStatus() {
return employeeStatus.toString();
}

public double getHoursWorked() {
return hoursWorked;
}
  
public void setHoursWorked(double hoursWorked)
{
if (hoursWorked < 1 || hoursWorked > 80)
{
throw new IllegalArgumentException("Data entered was out of range.");
}   
this.hoursWorked = hoursWorked;
}

@Override
public String toString() {
return ("Name: " + getFirstName() + " " + getLastName() + ", Birth Date: " + getBirthDate()
+ ", Hire Date: " + getHireDate() + ", Hours Worked: " + getHoursWorked()
+ ", Employee Status: " + getEmployeeStatus());
}
}

Hourly.java

public class Hourly {
private Employee employee;
private double payRate;
private double weeklyPay;
private static int numWeeklyPayChecks = 0;
public static final int MAXREGULAR = 40;
  
public Hourly()
{
this.employee = null;
this.payRate = 0;
this.weeklyPay = 0;
}

public Hourly(Employee employee, double payRate) {
this.employee = employee;
setPayRate(payRate);
numWeeklyPayChecks++;
}

public Employee getEmployee() {
return employee;
}

public double getPayRate() {
return payRate;
}
  
public void setPayRate(double payRate)
{
if (payRate < 15 || payRate > 30)
{
throw new IllegalArgumentException("Data entered was out of range.");   
}
this.payRate = payRate;   
}

public double getWeeklyPay() {
return weeklyPay;
}
  
public double getPaycheck()
{
this.weeklyPay = this.employee.getHoursWorked() * this.payRate;
  
if (this.employee.getHoursWorked() > MAXREGULAR) // if here was overtime. . .
{
double otHours = this.employee.getHoursWorked() - MAXREGULAR; // calculate overtime hours
double otPay = otHours * (payRate * 0.5); // pay "half" the rate for overtime hours worked
weeklyPay = weeklyPay + otPay; // add overtime pay to regular pay
}
return weeklyPay;
}
  
@Override
public String toString()
{
return(this.employee.toString() + "\nWeekly PayCheck: $" + String.format("%.2f", getPaycheck())
+ "\nNumber of Weekly PayChecks: " + numWeeklyPayChecks);
}
}

HourlyTest.java (Main class)

import java.util.Scanner;

public class HourlyTest {
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
  
System.out.print("Enter first name: ");
String firstName = sc.nextLine().trim();
System.out.print("Enter last name: ");
String lastName = sc.nextLine().trim();
  
System.out.print("Date of birth:\n--------------\nEnter day: ");
int birthDay = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter month: ");
int birthMonth = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter year: ");
int birthYear = Integer.parseInt(sc.nextLine().trim());
Date birthDate = new Date(birthMonth, birthDay, birthYear);
  
System.out.print("\nDate of hire:\n--------------\nEnter day: ");
int hireDay = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter month: ");
int hireMonth = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter year: ");
int hireYear = Integer.parseInt(sc.nextLine().trim());
Date hireDate = new Date(hireMonth, hireDay, hireYear);
  
System.out.print("\nEnter the number of hours worked: ");
double hoursWorked = Double.parseDouble(sc.nextLine().trim());
  
System.out.print("Enter hourly rate: $");
double payRate = Double.parseDouble(sc.nextLine());
  
Hourly hourly = new Hourly(new Employee(firstName, lastName, birthDate, hireDate, hoursWorked), payRate);
  
double weeklyPay = hourly.getPaycheck();
  
if (weeklyPay == 0.0)
{
System.out.print("\nEnter the number of hours worked: ");
hoursWorked = Double.parseDouble(sc.nextLine().trim());

System.out.print("Enter hourly rate: $");
payRate = Double.parseDouble(sc.nextLine().trim());
  
hourly.getEmployee().setHoursWorked(hoursWorked);
hourly.setPayRate(payRate);
weeklyPay = hourly.getPaycheck();
}
System.out.println("\n*** DETAILS ***\n---------------\n" + hourly);
sc.close();
}
}

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


Related Solutions

Go-Figure, Inc.* The actual company name and product has been disguised. Go-Figure, a newly formed corporation,...
Go-Figure, Inc.* The actual company name and product has been disguised. Go-Figure, a newly formed corporation, has developed a proprietary circuit board inspection system that offers a high-speed way to inspect boards for defects. Data indicate that 10 to 25 percent of circuit boards that leave a manufacturing line have defects such as wrong polarity, missing components, wrong components, etc. Quality control is a serious problem for manufacturers as, in aggregate, they place 2 to 5 million components on circuit...
Go-Figure, Inc.* The actual company name and product has been disguised. Go-Figure, a newly formed corporation,...
Go-Figure, Inc.* The actual company name and product has been disguised. Go-Figure, a newly formed corporation, has developed a proprietary circuit board inspection system that offers a high-speed way to inspect boards for defects. Data indicate that 10 to 25 percent of circuit boards that leave a manufacturing line have defects such as wrong polarity, missing components, wrong components, etc. Quality control is a serious problem for manufacturers as, in aggregate, they place 2 to 5 million components on circuit...
What are the three examples of commodity chain organization? Give actual examples of these relationships and...
What are the three examples of commodity chain organization? Give actual examples of these relationships and explain how they represent globalization. (8-12 sentences)
hi chapter 12 Q3: here is the question: modify the spreadsheet shown in Fig 12.9 to...
hi chapter 12 Q3: here is the question: modify the spreadsheet shown in Fig 12.9 to include the cash flows in this account. If the company deposits $3m in this account every month, what is the probability that the account will have insufficient funds to pay claims at some point during the year? how do i calculate beginning balance from month two knowing month one is $2,500,000
The actual case study assignment should be uploaded to the Week 6 Assignment Dropbox by 11:59...
The actual case study assignment should be uploaded to the Week 6 Assignment Dropbox by 11:59 p.m. mountain time on Sunday at the end of Week 6. You are encouraged to use the Excel template file provided in Doc Sharing. The Cambridge Company has budgeted sales revenues as follows.                                                                                       Jan                    Feb                  Mar__ Credit sales                                                            $45,000             $36,000             $27,000 Cash sales                                                              27,000             76,500             58,500 Total sales                                                              $72,000          $112,500             $85,500 Past experience indicates that 60% of the credit...
I CANNOT FIGURE THIS OUT FOR THE LIFE OF ME. *(It's not 570 actual labor hours;...
I CANNOT FIGURE THIS OUT FOR THE LIFE OF ME. *(It's not 570 actual labor hours; I keep getting that)* The auto repair shop of Quality Motor Company uses standards to control the labor time and labor cost in the shop. The standard labor cost for a motor tune-up is given below: Standard Hours Standard Rate Standard Cost Motor tune-up 2.60 $4.00 $10.40 The record showing the time spent in the shop last week on motor tune-ups has been misplaced....
Assignment 2 ACCT101 Assignment Question(s):                                   &
Assignment 2 ACCT101 Assignment Question(s):                                                       Q1- A company wants to implement good internal control. What are the policies and procedures you can suggest to minimize human frauds and errors? (1Mark) Q2- Assume that you have a company. And the management team estimates that 3% of sales will be uncollectible. Give any amount of sales and prepare the journal entry using the percent of sales method. (1Mark) Q3- A company that uses a perpetual inventory system made the following cash purchases...
a. Find real life examples of IFRS 9 used by an actual company
a. Find real life examples of IFRS 9 used by an actual company
This research assignment is an actual simulation directly taken from the audit section of a CPA...
This research assignment is an actual simulation directly taken from the audit section of a CPA exam. Please research the questions and provide the correct answer along with a short explanation as to why you chose that answer. Each question is worth ten points. Gloria CPA, an auditor for Smart Move Inc., observed changes in certain Year 2 financial ratios or amounts from the Year 1 ratios or amount. For each observed change, answer the following questions regarding possible explanations....
Suppose a friend is completing another physics assignment in their chair at their desk and you...
Suppose a friend is completing another physics assignment in their chair at their desk and you take the opportunity to turn this scenario into the physics question you now must complete. Your friend and chair have a combined constant moment of inertia of 5.5 kg m2, and the chair can freely rotate. Now your friend picks up two solid bricks (each of mass 1.7 kg) they accidentally collected from one of the campus buildings, and experiment with rotational kinematics. Supposing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT