You are on page 1of 7

CSYM025 Week 6

Lab Exercises:

1. Design a class named Employee. The class should keep the following
information in fields:
 Employee Name
 Employee number In the format XXXX-L, where each X Is
a digit within the range 0-9 and the L is a letter within the
range A-M
 Hire date
Write one or more constructors and the appropriate accessor and
mutator methods for the class.
Next, write a class named ProductionWorker that extends the
Employee class. The ProductionWorker class should have fields to
hold the following information:
Shift (an Integer)
Hourly pay rate (a double)
The workday is divided into two shifts:day and night. The shift field
will be an integer value representing the shift that the employee
works. The day shift is shift 1 and the night shift is shift 2. Write one
or more constructors and the appropriate accessor and mutator
methods for the class. Design class diagrams and demonstrate the
classes by writing a program that uses a ProductionWorker object.

2. In a particular factory, a team leader Is an hourly paid production


worker that leads a small team. In addition to hourly pay, team leaders
earn a fixed monthly bonus. Team leaders are required to attend a
minimum number of hours of training per year. Design a TeamLeader
class that extends the ProductionWorker class you designed in Lab
Exercise 1. The TeamLeader class should have fields for the monthly
bonus amount, the required number of training hours, and the number
of training hours that the team leader has attended. Write one or more
constructors and the appropriate accessors and mutators for the class.
Demonstrate the class by writing a program that uses a TeamLeader
object. Include class diagrams.

3. In a particular factory, a shift supervisor is a salaried employee who


supervises a shift. In addition to a salary, the shift supervisor earns a
yearly bonus when his or her shift meets production goals. Design a
ShiftSupervisor class that extends the Employee class you created in
Lab Exercise 1. The ShiftSupervisor class should have a field that
holds the annual salary and a field that holds the annual production
bonus that a shift supervisor has earned. Write one or more
constructors and the appropriate accessor and mutator methods for the
class. Demonstrate the class by writing a program that uses a
ShiftSupervisor object. Include class diagrams.

4. Design an Essay class that extends the GradedActivity class presented


in the lecture. The Essay class should determine the grade a student
receives for an essay. The student's essay score can be up to 100 and
is determined In the following manner:
Grammar: 30 points
Spelling: 20 points
Correct Length: 20 points
Content: 30 points
Demonstrate the class in a simple program. Include a class diagram.

5. Write a class named Employee that has the following fields:


 name – The name field references a String object that holds the
employee’s name.
 idNumber – The idNumber is an int variable that holds the
employee’s ID number.
 department – The department field references a String object that
holds the name of the department where the employee works.
 position – The position field references a String object that holds
the employee’s job title.
The class should have the following constructors:
 A constructor that accepts the following values as arguments and
assigns them to the appropriate fields: employee’s name,
employee’s ID number, department, and position.
 A constructor that accepts the following values as arguments and
assigns them to the appropriate fields: employee’s name and
employee’s ID number. The department and position fields should
be assigned an empty string ("").
 A no-argument constructor that assigns empty strings ("") to the
name, department and position fields, and 0 to the idNumber field.
Write appropriate mutator methods that store values in these fields and
accessor methods that return the values in these fields. Include a
toString() and equals() method for the class. Once you have written the
class, write a separate program that creates three Employee objects to
hold the following data:

Name ID Number Department Position


Susan 47899 Accounting Vice
Meyers President
Mark Jones 39119 IT Programmer
Joy Rogers 81774 Manufacturing Engineer

The program should store this data in the three objects, compare any two
objects using the equals method, then display the data for each employee
on the screen using the toString method. Save the main program as
EmployeeData.java.

Part 2:

1. Write an abstract class named Person with fields for holding a


person’s name, address, and telephone number. Include an abstract
toString() method in the Person class that constructs a string
containing all the relevant data of the attributes in the class. Write one
or more constructors and the appropriate mutator and accessor
methods for the class’s fields. Next, write a class named Customer,
which extends the Person class. The Customer class should have a
field for a customer number and a boolean field indicating whether the
customer wishes to be on a mailing list. Write appropriate mutator and
accessor methods for the class’s fields. Include both no-argument and
multiple argument constructors for the class. Write a toString()method
for the Customer class. Write a program to create an object of the
Customer class and display its details using the toString()method in
the main program. Save the main program as
CustomerClassDemo.java.

2. Create an interface named Shape containing two methods getArea()


and getPerimeter() that return a result of type double. Write two
classes Rectangle and Circle that implement the Shape interface. The
Rectangle class should include:
 Two double data fields named width and height. The default
values are 1 for both
width and height.
 A no-argument constructor that creates a default rectangle.
 A constructor that creates a rectangle with the specified width and
height
The Circle class should include:
 One double data field named radius. The default value is 1.
 A no-argument constructor that creates a default circle.
 A constructor that creates a circle with the specified radius.
Write a program that creates an array of Shape objects (both Rectangle
and Circle), calculates and displays the area and perimeter of each
object. Format any numerical output to two decimal places. Save the
main program as ShapesMain.java.
You may want to make use of the following formulas:

Rectangle Circle
Area width * height 3.1415 * radius *
radius
Perimeter 2(width * height) 2 * 3.1415 * radius

3. a. Create an abstract class named Book. Include a String field for the
book's title and a double field for the book's price. Within the class,
include a constructor that requires the book title, and two get methods-
one that returns the title and one that returns the price. Include an
abstract method named setPrice(). Create two child classes of Book:
Fiction and NonFiction. Each must include a setPrice() method that
sets the price for all Fiction Books to £24.99 and for all NonFiction
Books to £37.99. Write a constructor for each subclass, and include a
call to setPrice() within each. Write an application demonstrating that
you can create both a Fiction and a NonFiction Book, and display
their fields. Save the files as Book.java, Fiction.java, NonFiction.java,
and UseBook.java.

b. Write an application named BookArray In which you create an


array that holds 5 Books, some Fiction and some NonFiction. Using a
for loop, display details about all 5 books. Save the file as
BookArray.java.
4. Create an abstract Auto class with fields for the car make and price.
Include get and set methods for these fields; the setPrice() method is
abstract. Create two subclasses for individual automobile makers (for
example, Ford or BMW), and include appropriate setPrice() methods
in each subclass (for example £20,000 or £35000). Finally, write an
application that uses the Auto class and subclasses to display
Information about different cars. Save the files as Auto.java,
Ford.java, BMW.java and UseAuto.java.

5. Write an application named UseChildren that creates and displays at


least two Child objects-one Male and one Female. Child is an abstract
class and Male and Female are subclasses. The Child class contains
fields that holds the name, gender and age of a child. the Child class
constructor requires a name and a gender. The Child class also
contains two abstract methods named setAge() and display(). The
Male and Female subclass constructors require only a name; they pass
the name and appropriate gender to the Child. The subclass
constructors also prompt the user for an age using the setAge()
method, and then display the Child's data using the display() method.
Save the files as Child.java, male.java, Female.java, and
UseChildren.java.

6. a. Create an interface named Turner, with a single method named


turn(). Create a class named Leaf that Implements turn() to display
"Changing colors". Create a class named Page that implements turn()
to display "Going to the next page". Create a class named Pancake
that implements turn() to display "Flipping". Write an application
named DemoTurners that creates one object of each of these class
types and demonstrates the turn() method for each class. Save the files
as Turner.java, Leaf.java, Page.java, Pancake.java, and
DemoTurners.java.

b. Think of two more objects that use turn(), create classes for them,
and then add objects to the DemoTurners application, renaming It
DemoTurners2.java. Save the files, using the names of new objects
that use turn().

7. Create an abstract class named GeometricFigure. Each figure includes


a height, a width, a figure type, and an area. Include an abstract
method to determine the area of the figure. Create two subclasses
called Square and Triangle. Create an application that demonstrates
creating objects of both subclasses, and store them in an array. Save
the files as GeometricFigure.java, Square.java, Triangle.java, and
UseGeometric.java.

8. Modify previous exercise, adding an interface called SideObject that


contains a method called displaySides(); this method displays the
number of sides the object possesses. Modify the GeometricFigure
subclasses to include the use of the interface to display the number of
sides of the figure. Create an application that demonstrates the use of
both subclasses. Save the files as GeometricFigure2.java,
Square2.java, Triangle2.java, SidedObject.java, and
UseGeometric2.java.

9. Create an Account class to simulate an ATM machine. Create ten


accounts in an array with id 0,1,….9 and initialize balance £50. The
system prompts the user to enter an id. If the id is entered incorrectly,
ask the user for a correct id. Once an id is accepted, the main menu is
displayed as shown in sample run. You can enter a choice 1 for
viewing the current balance, 2 for withdrawing money, 3 for
depositing money, and 4 for exiting the main menu. If the user tries to
withdraw more money than is available, the program reports a "Not
enough funds" message. Once you exit, the system will prompt for an
id again. So, once the system starts, it will not stop.

Sample Run:
Enter an id: 3

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 50.0.

Main menu:
1: check balance
2:withdraw
3: deposit
4:exit
Enter a choice:4

Enter an id:

You might also like