0% found this document useful (0 votes)
48 views3 pages

COMP 1231 - M1 - Lab1 - Soln

This document provides solutions to an assignment on inheritance and polymorphism in Java. It includes sample code and class diagrams for exercises on words and books, food types, payment transactions, and monetary coins inheriting from a coin class. Students are instructed to complete coding exercises and class diagrams related to these inheritance concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

COMP 1231 - M1 - Lab1 - Soln

This document provides solutions to an assignment on inheritance and polymorphism in Java. It includes sample code and class diagrams for exercises on words and books, food types, payment transactions, and monetary coins inheriting from a coin class. Students are instructed to complete coding exercises and class diagrams related to these inheritance concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Faculty of Science

Module 1:
Lab 1 Solution

COMP 1231
Computer Programming II
COMP 1231: Computer Programming II S1-1

Module 1—Lab 1: Inheritance SOLUTIONS


The following exercises are intended to help you apply and practise the concepts
introduced in this module. This work is not submitted for marks.
1. Take a copy of Words.java, Book.java, and Dictionary.java from the
Chapter 8 directory in the “Source Code” area of your course. Compile and
run them to reproduce the effects described on pages 364–366 of your
textbook.
2. Take a copy of Words2.java, Book2.java, and Dictionary2.java from
the Chapter 8 directory in the “Source Code” area of your course. Compile
and run them to reproduce the effects described on pages 369–371 of your
textbook. Modify Words2.java to include another dictionary with a
different number of pages and definitions.
3. Draw an inheritance class hierarchy similar to that of Figure 8.4 (p. 376) to
show types of Food: Fruit (like Apple or Banana), Dairy (like Milk or
Cheese), and Vegetable (like Carrot, Bean, or Lettuce).
4. Create a UML class diagram showing an inheritance hierarchy containing
classes that represent different types of payment transactions at a store (cash,
credit card, etc.). Show some appropriate variables and method names for at
least two of these classes.
5. Design and implement a class called MonetaryCoin that is derived from the
Coin class presented in Chapter 5. Store a value in the monetary coin that
represents its value and add a method that returns its value. Create a driver
class to instantiate and compute the sum of several monetary coin objects.
Demonstrate that a monetary coin can be inherits its parent’s ability to be
flipped. (Hint: The Coin class and driver from Chapter 5 have been placed in
the Chapter 8 directory in the “Source Code” area of your course.)
S1-2 Lab 1: Solutions

Solutions
1. See pages 364–366 of your textbook.
2. See pages 369–371 of your textbook.
3. A sample diagram is below:

Food

Fruit Dairy Vegetable

Apple Banana Milk Cheese Carrot Bean Lettuce

4. A suggested solution is below:

Payment
-amtOwned
-amtTendered
+makeChange()

CreditCard
Cash
-cardNbr
-expiryDate
+validate()

5. See MonetaryCoin.java and MonetaryCoinDriver.java in the Lab 1


Solutions directory.

You might also like