In: Computer Science
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;
}
}
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 ********************************************************