computer science
COMP 1230 – Summer 2018 Lab 4 (OOP Concepts: Inheritance and Polymorphism)
Total Points: 60 Due: Wednesday, June 6, 2018 by Midnight.
In this lab, you will practice inheritance and polymorphism, two very important concepts of OOP languages. Submission Instruction: For each exercise, please test your code by running it with at least three different test cases by changing the input values. Take a snap-shot (screen capture) of your test runs and save them for submission. Please put all the source codes and snap-shots (screen captures) of your test runs in a folder. Name the folder as Lab6_FName (replace the FName with your first name) and zip it. Submit the zipped folder through the moodle.tru.ca.
Exercise 1 [5 points]: Create the following classes shown in the UML diagram. Then, create PointTest.java class with main method to test all functionality of these classes.
Exercise 2 [10 points]: The following figure shows a UML diagram in which the class Student is inherited from the class Person
a. Implement a Person class. The person constructor takes two strings: a first name and a last name. The constructor initializes the email address to the first letter of the first name followed by first five letters of the last name followed by @tru.ca. If the last name has fewer than five letters, the e-mail address will be the first letter of the first name followed by the entire last name followed by a @tru.ca. Examples:
Name Email Address
Jane Smith [email protected]
Musfiq Rahman [email protected]
John Morris [email protected]
Mary Key [email protected]
Person
// ivars
- firstName: String
- lastName: String
- email: String
+ Person(String fname, String lname)
+ getFirstName(): String
+ getLastName(): String
+ getEmailAddress(): String
+ toString(): String
Student
// class variables; initial value = 10023;
+ lastIdAssigend : int
// ivars
- studentId: int
- gpa : double + Student(String fname, String lname)
+ addCourse(int credits, double grade): void
+ getStudentId(): int
+ getGPA(): double
+ toString(): String
b. Override Object’s toString method for the Person class. The toString method should return the
present state of the object. c. Now, create a Student class that is a subclass of Person and implements Comparable interface. d. The Student constructor will be called with two String parameters, the first name and last name
of the student. When the student is constructed, the inherited fields lastName, firstName, and email will be properly initialized, the student’s gpa and number of credit will be set to 0. The variable lastIdAssigend will be properly incremented each time a Student object is constructed and the studentId will be set to the next available ID number as tracked by the class variable lastIdAssigend.
e. Override the object’s toString method for the Student class. The toString method should return the present state of the object. Note that it should use the toString() method from its superclass.
f. The addCourse() method should update the credits completed, calculate, and update the gpa value. Use the following values for grade:
Example GPA calculation: GRADE CREDIT CALCULATION (A) 4.0 x 4 = 16.00 (B) 3.0 x 4 = 12.00 (B) 3.0 x 4 = 12.00 (A) 4.0 x 1 = 4.00 (C) 2.0 x 3 = 6.00 GPA = 50.00 / 16 = 3.125; the getGPA() method should return this value.
g. Students are compared to each other by comparing GPAs. Override the compareTo() method for
the student class. Note that to override the compareTo() method, the Student class must implement Comparable interface.
Now, test your code with the supplied client code (StudentClient.java). Note: You should not modify this client code. We will use the same client code to test your classes.
Exercise 3 [10 points]:
In this exercise, you have to use java Interface.
Imagine you are writing a computer game that involves a hero who encounters various monsters.
Monsters have several characteristics: hit points, strength, age, and name (the class Monster.java is
supplied). The number of hit points a monster has is how far it is from death. A monster with a large
number of hit points is hard to kill. Similarly, the hero also has hit points.
When the hit points of the hero reach zero, the game is over. The strength of a monster affects how
many hit points the hero loses when the monster hits the hero. The age and name of the monster do
not affect the outcome of battles between the monster and the hero.
Can you make the necessary changes to the Monster class (supplied with this lab) and write a tester
(client) class with main method that will present the hero with monsters in increasing order of difficulty.
Hints: Monster needs a compareTo() method.
Save your program in the MonsterClient.java file.
Exercise 4 [5 points]:
This exercise will also test your knowledge on java Interface.
In this exercise, you need to write a static “search” method that accepts an array of Comparable objects
and a Comparable ‘key’ as parameter and it returns true if the key is present in the array, otherwise it
should return false. Test your implementation with two different type of Comparable objects. For
example,
public static boolean search(Comparable [] array, Comparable key){
// iterate through the array and look for the value ‘key’
// in the array. If found, then return true otherwise
// return false.
}
You should test the search method by first, creating different arrays containing objects that
implements Comparable interface (such as String, Integer, Float, etc.) and passing these arrays
to the search method and try to look for a value.
Save your code in MySearchTest.java
Exercise 5 [10 points]:
You are asked to write a discount system for a beauty saloon, which provides services
and sells beauty products. It offers 3 types of memberships: Premium, Gold and
Silver. Premium, gold and silver members receive a discount of 20%, 15%, and 10%,
respectively, for all services provided. Customers without membership receive no
discount. All members receives a flat 10% discount on products purchased (this might
change in future). Your system shall consist of three classes: Customer, Discount and
Visit, as shown in the class diagram. It shall compute the total bill if a customer
purchases $x of products and $y of services, for a visit. Also write a test program to
exercise all the classes.
The class DiscountRate contains only static variables and methods (underlined in the class
diagram).
Save your test class in BeautySaloon.java file.
Exercise 6 [20 points]:
Description
For this exercise, you will be writing a java application in support of a “Dessert
Shoppe” which sells candy by the pound, cookies by the dozen, ice cream, and
sundaes (ice cream with a topping). Your program will be used for the checkout
system.
To do this, you will implement an inheritance hierarchy of classes derived from
a DessertItem abstract superclass.
The Candy, Cookie, and IceCream classes will be derived from
the DessertItem class.
The Sundae class will be derived from the IceCream class.
You will also write a Checkout class which maintains a list (Vector)
of DessertItem's.
The DessertItem Class
The DessertItem class is an abstract superclass from which specific types
of DessertItems can be derived. It contains only one data member, a name. It also
defines a few methods. All of the DessertItem class methods except
the getCost() method are defined in a generic way in the file, DessertItem.java,
provided for you along with the other homework specific files in the directory.
The getCost() method is an abstract method that is not defined in
the DessertItem class because the method of determining the costs varies based on
the type of item. Tax amounts should be rounded to the nearest cent. For example, the
calculating the tax on a food item with a cost of 199 cents with a tax rate of 2.0%
should be 4 cents.
DO NOT CHANGE THE DessertItem.java file! Your code must work with this
class as it is provided.
The DessertShoppe Class
The DessertShoppe class is also provided for you in the file, DessertShoppe.java. It
contains constants such as the tax rate as well the name of the store, the maximum
size of an item name and the width used to display the costs of the items on the
receipt. Your code should use these constants wherever necessary! The
DessertShoppe class also contains the cents2dollarsAndCentsmethod which takes an
integer number of cents and returns it as a String formatted in dollars and cents. For
example, 105 cents would be returned as "1.05".
DO NOT CHANGE THE DessertShoppe.java file! Your code must work with this
class as it is provided.
The Derived Classes
All the classes which are derived from the DessertItem class must define a
constructor. Please see the provided TestCheckout class, TestCheckout.java (see a
sample test run of this test class at the end of this document), to determine the
parameters for the various constructors. Each derived class should be implemented by
creating a file with the correct name, eg., Candy.java.
The Candy class should be derived from the DessertItem class. A Candy item has
a weight and a price per pound which are used to determine its cost. For example,
2.30 lbs.of fudge @ .89 /lb. = 205 cents. The cost should be rounded to the nearest
cent.
The Cookie class should be derived from the DessertItem class. A Cookie item has
a number and a price per dozen which are used to determine its cost. For example, 4
cookies @ 399 cents /dz. = 133 cents. The cost should be rounded to the nearest cent.
The IceCream class should be derived from the DessertItem class.
An IceCream item simply has a cost.
The Sundae class should be derived from the IceCream class. The cost of a Sundae is
the cost of the IceCream plus the cost of the topping.
You must implement all derived classes with appropriate behavior.
The Checkout Class
The Checkout class, provides methods to enter dessert items into the cash register,
clear the cash register, get the number of items, get the total cost of the items (before
tax), get the total tax for the items, and get a String representing a receipt for the
dessert items. The Checkout class must use an array to store the DessertItem's (max
size of the array should be set from constant taken from the DessertShoppe class). The
total tax should be rounded to the nearest cent. The complete specifications for
the Checkout class are provided for you in JavaDoc format (provided in javadoc
folder).
Testing
A simple test driver, TestCheckout.java along with its expected output (see below),
are provided for you to test your class implementations. You must add additional tests
to the driver to more thoroughly test your code.
Sample Test Run Output from TestCheckout.java file:
Number of items: 4
Total cost: 1331
Total tax: 87
Cost + Tax: 1418
Kamloops Dessert Shoppe
--------------------
2.25 lbs. @ 3.99/lb.
Peanut Butter Fudge 8.98
Vanilla Ice Cream 1.05
Hot Fudge Sundae with
Choc. Chip Ice Cream 1.95
4 @ 3.99/dz.
Oatmeal Raisin Cookies 1.33
Tax .87
Total Cost 14.18
Number of items: 6
Total cost: 1192
Total tax: 77
Cost + Tax: 1269
Kamloops Dessert Shoppe
--------------------
Strawberry Ice Cream 1.45
Caramel Sundae with
Vanilla Ice Cream 1.55
1.33 lbs. @ .89/lb.
Gummy Worms 1.18
4 @ 3.99/dz.
Chocolate Chip Cookies 1.33
1.5 lbs. @ 2.09/lb.
Salt Water Taffy 3.14
3.0 lbs. @ 1.09/lb.
Candy Corn 3.27
Tax .77
Total Cost 12.69
Note that for this exercise, you need to submit 5 source
files, Candy.java, Cookie.java, IceCream.java, Sundae.java, and Checkout.java.
Note: for all of the exercises, you should submit test drivers. Code should be properly
commented.