Asst 1
Asst 1
Asst 1
Problem 1
A firm that sells car parts in Lebanon pays its employees on a weekly basis. There are two types of employees: Salaried
employees are paid a fixed weekly salary regardless of the number of hours worked, and hourly employees are
paid by the hour and receive overtime pay for all hours worked in excess of 40 hours. The company wants to
implement a Java application that performs its payroll calculations polymorphically.
We use abstract class Employee to represent the general concept of an employee. Classes SalariedEmployee and
HourlyEmployee are subclasses of class Employee. Each employee, regardless of the way his or her earnings are
calculated, has a first name, a last name and a socialsecuritynumber. So begin by defining the abstract class Employee
consisting of the following attributes and methods. The plus (+) sign indicates public and the (-) sign indicates
private.
Employee
-firstName: String
-lastName: String
-socialSecurityNumber: String
The first subclass of Employee is SalariedEmployee. This subclass overrides thee arnings method of Employee, thus
making SalariedEmployee a concrete class. Class SalariedEmployee consists of the following attributes and methods:
SalariedEmployee
-weeklySalary: double
+SalariedEmployee ( firstName: String, lastName: String, socialSecurityNumber: String, wage: double, hours: double
)
+setWage // assigns a new non-negative value to instance variable Wage otherwise it is set to 0
+getWage
+setHours // should ensure that the hours is between 0 and 168 otherwise it is set to 0
+getHours
+earnings(): double // a concrete method tocalculate earnings.
+toString(): String // String representation of HourlyEmployeeobject(required format shownin Table 1)
Table 1 - Formats of the various toString accordingto theclasses
toString
Employee firstName lastName
socialsecurity number: SSN
Salaried- Salaried employee: firstName lastName
Employee socialsecurity number: SSN weekly salary: weeklysalary
To test our Employee hierarchy, a main program will be created called PayrollSystemTest. This program creates
objects of each of the two concrete classes SalariedEmployee, HourlyEmployee. The program manipulates these
objects, first via variables of each object's own type, then polymorphically, using an array of Employee variables.
While processing the objects polymorphically, the program determines and outputs the type of each object in
the Employee array. The structure for the PayrollSystemTest class will be:
// output the string representation and earnings of each of these objects. Note that
each object's toString method is called implicitly by printf when the object is output
as a String with the %s format specifier.
...
// use the enhanced for statement to generically process each element in array employees
printing out each employee (using toString) followed by a printout of his earnings
...
}
}
salaried employee: Sue Jones social security number: 333 -33-3333 weekly salary:
$600.00
earned: $600.00
salaried employee: John Smith social security number: 111 -11-1111 weekly salary:
$800.00
earned: $800.00
hourly employee: Karen Price social security number: 222 -22-2222
hourly wage: $16.75; hours worked: 40.00 earned: $670.00
salaried employee: Sue Jones social security number: 333 -33-3333 weekly salary:
$600.00
earned: $600.00
After you finish testing the classes you implementedtillnow, place themin a folder called Problem2. Make a
copy of folder Problem2 and call it Problem3. You will be using in Problem3.
Problem 3
The same company of Problem 1 decides that it needs to implement a class called Invoice. This class contains billing
information for only one kind of car part. The company notices that it needs to build an application that can
determine payments for employees and invoices alike, and thus the need for an interface called Payable.
Interface Payable contains method getPaymentAmount that returns a doublea mount that must be paid for an
object of any class that implements the interface. Method getPaymentAmount is a general-purpose version of method
earnings of the Employee hierarchy method. earnings calculates a payment amount specifically for an Employee, while
getPaymentAmount can be applied to a broad range of unrelated objects.
After declaring interface Payable in a separate file, you are required to create the class Invoice, which implements
interface Payable. Class Invoice consists of the following attributes and methods. Note that quantity and pricePer
Item instance variables should be assigned non-negative values otherwise their values are set to 0.
Invoice
-partNumber: String
-partDescription: String
-quantity: int
-pricePerItem: double
invoice:
part number: 56789 (tire) quantity: 4
price per item: $79.95 payment due: $319.80
salaried employee: John Smith social security number: 111-11-1111 weekly salary: $800.00
payment due: $800.00
salaried employee: Lisa Barnes social security number: 888 -88-8888 weekly salary: $1,200.00
payment due: $1,200.00
Problem 3
Create the following classes in Java:
A HabbitMakhlouta is characterized by its Name (String) and the price of 1 Kg of it (Integer). It is also characterized
by the way it is consumed. This function should return how the user consumes a HabbitMakhlouta. Add to this class a
default and non-default constructors. Add the necessary setters and getters and the toString function.
ElBezer is a type of HabbitMakhlouta characterized by its Source: “Laktin”, “MayyelElShams”, or “Battikh”. Add to
your class a default and a non-default constructor (4 points). The function Consume returns a string reading “Tef2ayeh”.
Add to this class the required setters and getters and a toString function.
ElFesto2 is another type of HabbitMakhlouta characterized by whether it is Salted or not. Add to your class a default
and non-default constructor. The function Consume returns a string reading “Ma T2asherne”. Add to this class the
required setters and getters and a toString function.
Your classes are going to be used by Al Rifai Roastery. Therefore, your driver program should:
1. You went to buy an array of HabbitMakhlouta whose size is to be determined and filled by the user.
2. Print how each nut & kernel in your array is consumed.
3. Sort your array in ascending order of price.
4. All Festo2 entries received a discount of 30%. Update your array accordingly and print the updated price.
5. Because you were in a hurry, you forgot whether you have bought Bezer Laktin or not. Print how many entries
in your array has this specific type.
6. Print the total price of all Nuts & Kernels.
Follow general stylistic guidelines, such as indentation and whitespace, meaningful identifier names, and localization
of variables. Submit via Moodle a zipped folder called hw1_netid. It should contain three subfolders called
problem1, problem2, and problem3.