1. Exercises #1
Print(or photocopy) the whole document and write your answers on the succeeding pages. (Handwritten only)
Name: _____________________________Schedule:_______________Points:___________
1.A Reference Class
Create a class called Invoice that a hardware store might use to represent an invoice
for an item sold at the store. An Invoice should include four pieces of information as instance
variables – a part number(String), a part description(String), a quantity of the item being
purchased(int) and a price per item(double). Your class should have a constructor that
initializes the four instance variables. Provide mutator and accessor methods for each instance
variable. In addition, provide a method named getInvoiceAmount that calculates the invoice
amount(multiply the quantity by the price per item), then returns the amount as a double
value. If the quantity is not positive, it should be set to 0. If the price per item is not positive,
it should be set to 0.0. Draw the UML diagram for the class.
1.B Main Class
Write a test application named InvoiceTest that demonstrates class Invoice’s
capabilities.
2.A Reference Class
Create a class named Employee that includes three pieces of information as instance
variables – a first name(String), a last name(String) and a monthly salary(double). Your class
should have a constructor that initializes the three instance variables. Provide a set and get
method for each instance variable. Draw the UML diagram for the class.
2.B Main Class
Write a test application named EmployeeTest that demonstrates class Employee’s
capabilities. Create two Employee objects and display each object’s salary. Then give each
Employee a 10% raise and display each Employee’s yearly salary again.
3.A
(The Rectangle class) Design a class named Rectangle to represent a rectangle. The class
contains:
Two double data fields named width and height that specify the width and height of
the rectangle. The default values are 1 for both width and height.
A string data field named color that specifies the color of a rectangle.
Hypothetically, assume that all rectangles have the same color. The default color is
white.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and height.
The accessor and mutator methods for all three data fields.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
3.B
Draw the UML diagram for the class. Write a test program that creates two Rectangle
objects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the
second object. Assign color red to all Rectangle objects. Display the properties of both
objects and find their areas and perimeters.
2. 4.A
(The Fan class) Design a class named Fan to represent a fan. The class contains:
Three constants named SLOW, MEDIUM, and FAST with values 1, 2, and 3 to denote the
fan speed.
An int data field named speed that specifies the speed of the fan (default SLOW).
A boolean data field named on that specifies whether the fan is on (default false).
A double data field named radius that specifies the radius of the fan (default 5).
A string data field named color that specifies the color of the fan (default blue).
A no-arg constructor that creates a default fan.
The accessor and mutator methods for all four data fields.
A method named toString() that returns a string description for the fan. If the fan is
on, the method returns the fan speed, color, and radius in one combined string. If the
fan is not on, the method returns fan color and radius along with the string "fan is
off" in one combined string.
4.B
Draw the UML diagram for the class. Implement the class. Write a test program that creates
two Fan objects. Assign maximum speed, radius 10, color yellow, and turn it on to the first
object. Assign medium speed, radius 5, color blue, and turn it off to the second object.
Display the objects by invoking their toString method.
5.A
Create a class called BirthDay that includes three pieces of information as data members –
month (type int), day (type month), and a year (type int). Your class should have a constructor
that initializes the three data members. Provide mutators and accessors for each data member.
Provide a method displayBirthDay that displays the month, day and years separated by
slashes (Example: 12/12/85). Provide another method named toString that will display the
date in words. (Example: twelfth day of December, year one thousand nine hundred
eighty five). Draw the UML diagram for the class.
5.B
Write a test application named BirthDayTest that demonstrates class BirthDay’s Application.
Note: This is worth 250 points. (To be passed on Wednesday)