Sunteți pe pagina 1din 3

ADVANCED JAVA PROGRAMMING LAB

CLASS & OBJECTS

1. (Employee Class) Create a class called Employee that includes three instance variables—a first name
(type String), a last name (type String) and a monthly salary (double). Provide a constructor that
initializes the three instance variables. Provide a set and a get method for each instance variable. If the
monthly salary is not positive, do not set its value. Write a test application named EmployeeTest that
demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s
yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

2. (Date Class) Create a class called Date that includes three instance variables—a month (type int), a day
(type int) and a year (type int). Provide a constructor that initializes the three instance variables and
assumes that the values provided are correct. Provide a set and a get method for each instance variable.
Provide a method displayDate that displays the month, day and year separated by forward slashes (/).
Write a test application named DateTest that demonstrates class Date’s capabilities.

INHERITANCE

3. Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and
Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the
points in each shape. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance
variables and methods for each class. The private instance variables of Quadrilateral should be the x-y
coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of
your classes and outputs each object’s area (except Quadrilateral).

POLYMORPHISM

4. (Project: ShapeHierarchy) Implement the Shape hierarchy shown in Fig. Each TwoDimensionalShape
should contain method getArea to calculate the area of the two-dimensional shape. Each
ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and
volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape
references to objects of each concrete class in the hierarchy. The program should print a text description
of the object to which each array element refers. Also, in the loop that processes all the shapes in the
array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it’s a
TwoDimensionalShape, display its area. If it’s a ThreeDimensionalShape, display its area and volume.

5. (Payroll System Modification) Modify the payroll system of Figs. to include an additional Employee
subclass PieceWorker that represents an employee whose pay is based on the number of pieces of
merchandise produced. Class PieceWorker should contain private instance variables wage (to store the
employee’s wage per piece) and pieces (to store the number of pieces produced). Provide a concrete
implementation of method earnings in class PieceWorker that calculates the employee’s earnings by
multiplying the number of pieces produced by the wage per piece. Create an array of Employee
variables to store references to objects of each concrete class in the new Employee hierarchy. For each
Employee, display its String representation and earnings.

6. Figure illustrates that class Employee is an abstract class. Concrete class SalariedEmployee extends
Employee and inherits its superclass’s realization relationship with interface Payable. Interface Payable
contains public abstract method getPaymentAmount. We now create class Invoice to represent a simple invoice
that contains billing information for only one kind of part. The class declares private instance variables
partNumber, partDescription, quantity and pricePerItem that indicate the part number, a description of the part,
the quantity of the part ordered and the price per item. Class Invoice also contains a constructor, get and set
methods that manipulate the class’s instance variables and a toString method that returns a String representation
of an Invoice object. Methods setQuantity and setPricePerItem ensure that quantity and pricePerItem obtain
only nonnegative values. Figure indicates that class Employee implements interface Payable. A
SalariedEmployee class that extends Employee and fulfills superclass Employee’s contract to implement
Payable method getPaymentAmount. Use interface Payable to process Invoices and Employees
Polymorphically.

7. Write a Java program to implement the concept of importing classes from user defined package and creating
packages.

S-ar putea să vă placă și