Você está na página 1de 8

ENTS 640 Networks and Protocols I

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;

class Date // package scope


{

// ------------- attributes ------------- //

// static attributes

// default date values


private static final int DEFAULT_YEAR = 1900;
private static final int DEFAULT_MONTH = 1;
private static final int DEFAULT_DAY = 1;

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

private static final String[] MONTH_NAMES = { "", "January", "February", "March",


"April", "May", "June", "July", "August", "September", "October", "November",
"December" };

// instance attributes

// storing the date information


private int year;
private int month;
private int day;

// ------------- public methods ------------- //

// static methods: accessors

// static methods: mutators

// instance methods: constructors

public Date(int aYear, int aMonth, int aDay)


{
setDate(aYear, aMonth, aDay);
} // Date()

public Date() // default constructor + overloaded constructor


{
this(DEFAULT_YEAR,DEFAULT_MONTH,DEFAULT_DAY); // calling another constructor
} // Date()

// instance methods: accessors

public int getYear()


{
return year;
} // getYear()

public int getMonth()


{
return month;
} // getYear()
public int getDay()
{
return day;
} // getYear()

// conversion to String for printing Date objects


public String toString()
{
return "Date object: Year: " + year + ", Month: " + MONTH_NAMES[month] +
", Day: " + day + ".";
} // toString()

// instance methods: mutators

public void setDate(int aYear, int aMonth, int aDay)


{
year = checkYear(aYear);
month = checkMonth(aMonth);
day = checkDay(aYear, aMonth, aDay);
} // setDate()

// ------------- private methods ------------- //

// validate the year value


private int checkYear(int aYear)
{
if(aYear >= 0) // only A.D. dates are accepted
return aYear;

throw new IllegalArgumentException("The year value must be non-negative.");

} // checkYear()

// validate the month value


private int checkMonth(int aMonth)
{
if( aMonth > 0 && aMonth < 13 )
return aMonth;

throw new IllegalArgumentException("The month value must be between 1 and 12.");

} // checkMonth()

// validate day value


private int checkDay(int aYear, int aMonth, int aDay)
{
if(aDay > 0 && aDay <= DAYS_PER_MONTH[month])
return aDay;

// special case: allow for February 29 for leap years


if( aDay == 29 && aMonth == 2 &&
(aYear % 400 == 0 || (aYear % 4 == 0 && aYear % 100 != 0) ) )
return aDay;

throw new IllegalArgumentException("The day value is out of range for the


specified year and month.");

} // checkDay()

} // class Date
package datePackege;

public class DateTest


{
public static void main(String[] args)
{

Date myBirthDate = new Date(1977,1,15);

System.out.print("The date stored in myBirthDate is:\n");


System.out.print(myBirthDate); // implicit invocation of toString()

Date otherDate = new Date();

System.out.print("\nThe date stored in otherDate is:\n");


System.out.print(otherDate.toString()); // explicit invocation of toString()

// incrementing the day in the otherDate object


otherDate.setDate(otherDate.getYear(), otherDate.getMonth(),
otherDate.getDay()+1);
System.out.print("\nThe new date stored in otherDate is now:\n");
System.out.print(otherDate);

} // main()

} // class DateTest

------------------------------------- OOOOO -------------------------------------

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:

Name Type Salary Bonus


Joe Apple Employee 40k -
Jack Orange Employee 50k -
Jill Pear Manager 100k 10k

Finally, the program should calculate the total earnings of all employees by calling the getEarnings()
method on each employee.

Solution

package employeePackage;

public class Employee


{
// ------------- attributes ------------- //

// static attributes

// instance attributes
private String name;
private String socialSecurityNumber;
private double salary = 50000; // initializer expression - executed before the
// constructors

// ------------- public methods ------------- //


// static methods: accessors

// static methods: mutators

// instance methods: constructors

public Employee(String aName, String aSSN, double aSalary)


{
name = aName;
socialSecurityNumber = aSSN;
salary = aSalary;
} // Employee()

public Employee(String aName, String aSSN) // salary is set in the initializer


// expression
{
name = aName;
socialSecurityNumber = aSSN;
} // Employee()

// instance methods: accessors

public String getName()


{
return name;
} // getName()

public String getSSN()


{
return socialSecurityNumber;
} // getSSN()

public double getEarnings()


{
return salary;
} // getSalary()

public String toString()


{
return "Employee object: name = " + name +", SSN = " + socialSecurityNumber +
", earnings = " + salary + ".";
} // toString()

// instance methods: mutators

public void raiseSalary(double amount)


{
if(amount >= 0)
salary += amount;
else
throw new IllegalArgumentException("The salary raise amount is negative.");
} // raiseSalary(

// ------------- package-scope methods ------------- //

// ------------- private methods ------------- //

} // class Employee
package employeePackage;

public class Manager extends Employee


{
// ------------- fields ------------- //

// static fields

// instance fields

private double bonus;

// ------------- public methods ------------- //

// static methods: accessors

// static methods: mutators

// instance methods: constructors

public Manager(String aName, String aSSN, double aSalary, double aBonus)


{
super(aName, aSSN, aSalary); // calling Employee's constructor Employee(String
// aName, String aSSN, double aSalary)
bonus = aBonus;
} // Manager()

// instance methods: accessors

public double getBonus()


{
return bonus;
}

public double getEarnings() // overriding Employee's getEarnings()


{
return super.getEarnings() + bonus;
}

public String toString() // / overriding Employee's toString()


{
return "Manager object: name = " + super.getName() +", SSN = " + super.getSSN()
+ ", earnings = " + getEarnings() + ".";
} // toString()

// instance methods: mutators

public void setBonus(double aBonus)


{
bonus = aBonus;
}

// ------------- package-scope methods ------------- //

// ------------- private methods ------------- //

} // class Manager
package employeePackage;

public class EmployeeTest


{

public static void main(String[] args)


{
// creating objects
Employee joe = new Employee("Joe Apple", "123-45-6789",35000);
System.out.print("Employee joe: " + joe);
Employee jack = new Employee("Jack Orange", "111-11-1111");
System.out.print("\nEmployee jack: " + jack);
Manager jill = new Manager("Jill Pear", "222-22-2222", 100000, 10000);
System.out.print("\nEmployee jill: " + jill);

// raising Joe's salary


Employee otherJoe = joe; // both otherJoe and joe refer to the same object
double amount = 5000;
otherJoe.raiseSalary(amount); // Joe Apple's salary is increased
System.out.print("\nEmployee otherJoe: " + otherJoe);

double totalEarnings = joe.getEarnings() + jack.getEarnings() +


jill.getEarnings();

System.out.print("\nThe total earnings payable: " + totalEarnings);

} // main()

} // class EmployeeTest

------------------------------------- OOOOO -------------------------------------

Você também pode gostar