Escolar Documentos
Profissional Documentos
Cultura Documentos
Lab #4
L4.1
Write a Java class, Date, that represents a calendar day, including the year, month and day. It should also
define a number of static constants for default field initialization (for the default constructor), an array
containing the number of days per month for each month, and the month names for the String
representation of the object. It should also have the necessary accessor and mutator methods, and the
mutator methods should check for valid year/month/day combinations before allowing to change the state
of the object. The class should define a number of private helper methods to check for the validity of the
input values. If the user supplies an invalid year/month/day combination, the input value checking
methods should throw an IllegalArgumentException exception with an appropriate input string as an
error message. Hint: a leap year is a year that either divisible by 4 but not by 100, or it is divisible by 400.
(For example, 1700 and 1800 were not leap years, but 1600 and 2000 were.) The UML diagram for the
class is provided below.
In addition, write another class, DateTest, that tests the functionality of the class Date, and place both
classes in a common package, e.g. datePackege. The tester class should have a main( ) method, and it
should create objects using both constructors, print out the content (state) of all created objects, and
exercise the mutator and accessor methods.
Solution
package datePackege;
// static attributes
private static final int[] DAYS_PER_MONTH = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
// instance attributes
} // checkYear()
} // checkMonth()
} // checkDay()
} // class Date
package datePackege;
} // main()
} // class DateTest
L4.2
Write two Java classes, Employee and Manager, that form and inheritance hierarchy, representing a
employees and managers. They have an “is-a” relationship: each manager is also an employee. The
Employee class should have three fields, representing the employee’s name social security number and
salary. The class should also have the methods shown in the UML diagram below. The toString()
method should return the String representation of the object. The raiseSalary() method should increase
the employee’s salary by the specified non-negative amount and throw an IllegalArgumentException
exception with the message "The salary raise amount is negative." if the supplied amount parameter
is negative.
The Manager class should be a subclass of of Employee, and have one additional field representing
the bonus managers receive in addition to their salaries. It should define two new methods, getBonus()
and setBonus(), and override the methods toString() and getEarnings() already existing in the
superclass.
In addition, write another class, EmployeeTest , that tests the functionality of these, and place
both classes in a common package, e.g. employeePackage. The tester class should have a main( ) method,
and it should create objects using all constructors, print out the content (state) of all created objects, and
exercise the mutator and accessor methods. Then it should create the following employees:
Finally, the program should calculate the total earnings of all employees by calling the getEarnings()
method on each employee.
Solution
package employeePackage;
// static attributes
// instance attributes
private String name;
private String socialSecurityNumber;
private double salary = 50000; // initializer expression - executed before the
// constructors
} // class Employee
package employeePackage;
// static fields
// instance fields
} // class Manager
package employeePackage;
} // main()
} // class EmployeeTest