In: Computer Science
Draw a UML diagram for the classes.
Code for UML:
// Date.java
public class Date {
public int month;
public int day;
public int year;
public Date(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public Date() {
this.month = 0;
this.day = 0;
this.year = 0;
}
}
//end of Date.java
// Name.java
public class Name {
public String fname;
public String lname;
public Name(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
public Name() {
this.fname = "";
this.lname = "";
}
}
//end of Name.java
// Address.java
public class Address {
public String street ;
public String state ;
public String city;
public int zipcode;
public Address(String street, String state, String
city, int zipcode) {
this.street = street;
this.state = state;
this.city = city;
this.zipcode = zipcode;
}
public Address() {
this.street = "";
this.state = "";
this.city = "";
this.zipcode = 0;
}
}
//end of Address.java
// Employee.java
public class Employee {
public int number;
public Date mydate;
public Address myadress;
public Name myname;
public Employee(int number, Name myname, Date
mydate, Address myadress) {
this.number = number;
this.mydate = mydate;
this.myadress = myadress;
this.myname = myname;
}
public Employee() {
this.number = 0;
this.mydate = new Date();
this.myadress = new Address();
this.myname = new Name();
}
// method to display the details of the Employee
public void display()
{
System.out.println("Number:
"+number);
System.out.println("Name:
"+myname.fname+" "+myname.lname);
System.out.println("Data:
"+mydate.month+"/"+mydate.day+"/"+mydate.year);
System.out.println("Address:
"+myadress.street+" "+myadress.city+", "+myadress.state+",
"+myadress.zipcode);
}
}
// end of Employee.java
// SalariedEmployee.java
public class SalariedEmployee extends Employee
{
public double salary;
// parameterized constructor
public SalariedEmployee(int number, Name myname, Date
mydate, Address myadress, double salary)
{
super(number, myname, mydate,
myadress); // call Employee's constructor
this.salary = salary;
}
// default constructor
public SalariedEmployee()
{
super();
this.salary = 0;
}
// override Employee's display method to display the
additional details
public void display()
{
super.display();
System.out.printf("Salary:
$%,.2f\n",salary);
}
}
//end of SalariedEmployee.java
// HourlyEmployee.java
public class HourlyEmployee extends Employee
{
public double pay_rate;
public int hours_worked;
public double earnings;
// parameterized constructor
public HourlyEmployee(int number, Name myname, Date
mydate, Address myadress, double pay_rate, int hours_worked)
{
super(number, myname, mydate,
myadress);
this.pay_rate = pay_rate;
this.hours_worked =
hours_worked;
// calculate earnings
if(hours_worked <= 40) // no
overtime
earnings =
this.pay_rate*this.hours_worked;
else // overtime
earnings =
this.pay_rate*40 + (this.hours_worked-40)*1.5*this.pay_rate;
}
// default constructor
public HourlyEmployee()
{
super();
pay_rate = 0;
hours_worked = 0;
earnings = 0;
}
// override display method
public void display()
{
super.display();
System.out.printf("Pay rate:
$%,.2f\n",pay_rate);
System.out.println("Hours Worked:
"+hours_worked);
System.out.printf("Earnings:
$%,.2f\n",earnings);
}
}
//end of HourlyEmployee.java
// Employeeinfo.java
public class Employeeinfo {
public static void main(String[] args)
{
// create SalariedEmployee
SalariedEmployee s = new
SalariedEmployee(12, new Name("Shaun","Marsh"), new Date(11, 7,
1995), new Address("Street1","State1","City1",70081), 75000);
// create HourlyEmployee without
any overtime
HourlyEmployee h1 = new
HourlyEmployee(15, new Name("Harry","Doe"), new Date(7, 16, 2000),
new Address("Street2","State2","City2",60181), 45.75, 35);
// create HourlyEmployee with
overtime
HourlyEmployee h2 = new
HourlyEmployee(25, new Name("Jerry","Hope"), new Date(10, 16,
2007), new Address("Street3","State3","City3",80111), 45.75,
45);
// display the details
s.display();
System.out.println();
h1.display();
System.out.println();
h2.display();
}
}
//end of Employeeinfo.java
Step 1:Date, Address, and Name are used as attributes in Employee, we introduce a dependency relationship
Step 2:Since there is a parent /Child relationship between Employee(Parent) and SalariedEmployee(Child) and HoulrlyEmployee(Child) we introduce the Generalization relationship.
Step 3:Since we are using SalariedEmployee and HourlyEmployee in Employeeinfo we introduce a dependency relationship.
The UML Class Diagram is :