Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Asst 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

American University of Beirut Assignment 1

Department of Computer Science Inheritance and


CMPS 202 – Intermediate Programming with Polymorphism
Data Structures Deadline: Sunday
Fall 2022/2023 Sept. 11 at midnight

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

+Employee ( firstName: String, lastName: String, socialSecurityNumber: String )


//…add get and set methods that manipulate Employee's instance variables…
+toString(): String // String representation of Employee object(follow the format shown in Table 1)
+earnings(): double; // an abstract method overridden by subclasses. no implementation here

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, weeklySalary: double)


+setWeeklySalary // assigns a new non-negative value to instance variable weeklySalaryotherwise it is set to 0
+getWeeklySalary // get weeklySalary
+earnings(): double // a concrete method thatreturns the weekly salary
+toString(): String // String representation of SalariedEmployee object (followthe format shownin Table 1)
The second subclass of Employee is HourlyEmployee. This subclass also overrides the earnings method of Employee,
thus making HourlyEmployee a concrete class. Every hourly employee is paid the amount of hours worked at
the hourly wage up to 40 hours. Beyond these 40 hours, every extra hour is paid at 1.5 the hourly wage. Class
HourlyEmployee consists of the following attributes and methods.
HourlyEmployee
-wage: double
-hours: 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

Hourly- Hourly employee: firstName lastName


Employee Socialsecurity number: SSN
Hourly wage: wage; hours worked: hours
Invoice invoice:
part number: partNumber(partDescription) quantity: quantity
price per item: pricePerItem

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:

public class PayrollSystemTest


{
public static void main ( String[] args )
{
// For each of the Employee subclasses, create 2 objects
...

// 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.
...

// create a four-element Employee array


...

// initialize array with Employees


...

// 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
...
}
}

Your run of PayrollSystemTest shouldlooklike this:

Employees processed individually:


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

hourly employee: Bob Lewis


social security number: 444 -44-4444 hourly wage: $10; hours worked: 43.00 earned:
$475.00
Employees processed polymorphically:

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

hourly employee: Bob Lewis


social security number: 444-44-4444 hourly wage: $10; hours worked: 43.00 earned:
$475.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( partNumber: String, partDescription: String, quantity: int, pricePerItem: double )


//…get and set methods thatmanipulate Invoice’s instance variables…
+toString(): String // String representation of Invoice object (requiredformat shown in Table 1)
+getPaymentAmount: double // The method multiplies thevalues of quantityand pricePerItem
(obtainedthroughthe appropriate getmethods) andreturns theresult.
Now do the necessary changes to class Employee such that it also implements interface Payable. Keep in mind the
Payable’s method getPaymentAmountwould not be implemented in class Employee as it will be implemented in its
subclass SalariedEmployee.
Finally, update Employee subclass SalariedEmployee to "fit" into the Payable hierarchy (i.e., rename SalariedEmployee
method earnings as getPaymentAmount).
You will now implement PayableInterfaceTest to illustrate how interface Payable can be used to process a set of
Invoices and Employees polymorphically in a single application. The structure for the PayableInterfaceTest class will
be:
public class PayableInterfaceTest
{
public static void main(String args[] )
{
// create a four-element Payable array
...
// populate array with objects that implement Payable (the first 2 objects are instances of class Invoice, and the
remaining two objects are instances of SalariedEmployee
...
// use for statement to polymorphically process each Payable object in the array, printing the object as a String,
along with the payment amount due.
}
}

Your run of PayrollSystemTest should look like this:

Invoices and Employees processed polymorphically: invoice:


part number: 01234 (seat) quantity: 2
price per item: $375.00 payment due: $750.00

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.

You might also like