Workshop #4: Inheritance
Learning Outcomes:
Upon successful completion of this workshop, you will have demonstrated the abilities to:
● Design and implement classes in the “is-a” relationship.
● Practice casting
● Describe to your instructor what you have learned in completing this workshop.
Requirements:
Part 1: [7 points]
To complete this task you should read and study the lecture Inheritance
Step 1: Create a new project named “ItemManager”.
Step 2: Create a package named “DTO”, it contains some files: [Link], [Link],
[Link], and [Link]
Step 3: Create another package named “GUI”, it contains the [Link] file
1
Implement the class diagram as follows:
Item
#value: int
#creator: String
+Item()
+Item(int, String)
+getters/setters
+output():void
+input():void
Vase
-height: int AntiqueShop
-material: String
+Vase()
+Vase(int, String, int, String)
+getters/setters +main():void
+outputVase():void
+InputVase(): void
The AntiqueShop class is making use of Vase, Statue, and Painting, in the sense
Statue
that it has declared references to them, and thus there is a dependency.
-weight: int
-colour: String
+Statue()
+Statue(int, String, int, String)
Requirement:
1. +getters/setters In the file [Link],
+outputStatue():void
+inputStatue():void
● The method input(): Using Scanner class to input
Painting
all fields of the Item class. Verify: value>0,
-height: int creator is not empty
-width: int
-isWatercolour: boolean
-isFramed: boolean
● The method output(): print out all fields
+Painting()
+Painting(int, String, int, int,
2. boolean, boolean ) In the file [Link],
+getters/setters
+outputPaiting():void ● The method inputVase(): Using Scanner class to
+inputPainting():void
input all fields of the Vase class.
● The method outputVase(): print out all fields of the Vase class
Hint:
2
public class Vase{
…
//this method is used to input all fileds of a vase object
public void inputVase(){
input(); // call the inherited method to input two fields: value, creator
//TODO: you is required to add more your code to input two fields : height, material
// use try..catch/throws to handle exceptions
}
//this method displays information of a vase object
public void outputVase(){
output(); // call the inherited method to print two fields out: value, creator
[Link](“Height:” + height);
[Link](“Material:”+ material);
}
…
}
3. You do the same for Statue class, Painting class
4. In the file “[Link]”. you type like as follow:
3
5.
public class AntiqueShop {
public static void main(String[] args){
Item item=null;
int choice=0;
Scanner sc=….
do{
[Link](“1. Create a Vase:”);
[Link](“2. Create a Statue:”);
[Link](“3. Create a Painting:”);
[Link](“4. Display the Item:”);
[Link](“Input a choice:”);
Choice=[Link]();
switch(choice) {
case 1:
item=new Vase();
((Vase)item).inputVase();
break;
case 2:
item =new Statue();
((Statue) item).inputStatue();
break;
case 3:
item =new Painting();
((Painting) item).inputPainting();
break;
case 4:
if(item!=null) {
if(item instanceof Vase)
6. (Optional) Now, you is required to update the above program. You should create a new
class named Menu. This class contains one static method
//use this method to show pre-defined options
//input: an array contains the list of options
//output: return a user’s choice that is inputted from the keyboard.
4
⇨ Update the main method to use the Menu class.
public class AntiqueShop {
public static void main(String[] args){
String[] options={“ Create a Vase “,”Create a Statue”,” Create a Statue”,” display the item”};
Item item=null;
int choice=0;
do{
choice=[Link](options);
switch(choice){
case 1:
item=new Vase();
Part 2: Draw the memory map when the program runs [3 points]
Explain step by step what happened when the program runs and answer some questions.
- What is stored in the static heap, stack, dynamic heap?
- What are objects in the program?
- What is the item variable storing?
- Why must you cast to call the method inputVase()/outputVase()?
- What is the error thrown when you cast it wrong?
- What methods can you call if you don’t cast the item variable?