Lab 4
Lab 4
Lab 4
==================================================================
Learning Outcomes:
Upon successful completion of this workshop, you will have demonstrated the abilities
to:
Design and implement classes in the “is-a” relationship and implement interface..
Practice casting
Describe to your instructor what you have learned in completing this workshop.
Requirements:
To complete this task you should read and study the lecture Inheritance and Interface
Step 2: Create a package named “DTO”, it contains some files: IIo.java, Item.java,
Vase.java, and Painting.java
1
Implement the class diagram as follows:
<interface> <interface>
IIo IUtility
+void input(Scanner sc); +int getInt(Scanner sc, String msg, int min, int max);
+void output(); +String getString(Scanner sc, String msg, boolean isEmpty, String pattern);
Item Utility
#value: int +getInt(Scanner sc, String msg, int min, int max)
#creator: String +getString(Scanner sc, String msg, boolean isEmpty, String pattern)
+Item()
+Item(int, String)
+getters/setters
+output():void
+input(Scanner):void
Vase
-height: int
-material: String
+Vase()
+Vase(int, String, int, String)
+getters/setters
+output():void
+Input(Scanner): void
Painting
-height: int
-width: int
-isWatercolour: boolean
-isFramed: boolean
+Painting()
+Painting(int, String, int, int,
boolean, boolean )
+getters/setters
+output():void
+input(Scanner):void
Main
+main():void
Requirement:
2
1. In the file IIo.java: Declares interface IIo:
Method void input(Scanner sc);
Method void output();
2. In the file Item.java: Declares Item class that implements interface IIo.
The method input(Scanner sc): Using methods of Utility class to
input all fields of the Item class. Verify: value from 1 to 100, creator is not
empty
}
public void output(){
output(); // call the inherited method
System.out.println(“Height:” + height);
System.out.println(“Material:”+ material);
}
…
}
It allows the user to enter a string that must matche with the given pattern.
3
5. In file Utility.java: Declares Utility class that implements IUtility interface.
}
}
Your program output looks like as below:
Case: choice = 1:
1. Create a Vase:
2. Create a Painting:
Enter choice (1-2):0
Enter choice (1-2):1
Enter value (1-100):10
Enter creator (not empty):
Enter creator (not empty):Hoa Phat
Enter height (1-100):10
Enter material (not empty):
Enter material (not empty):Glass
OUTPUT:
Vase's information:
Value: 10
Creator: Hoa Phat
4
Height: 10
Material: Glass
Case: choice = 2:
1. Create a Vase:
2. Create a Painting:
Enter choice (1-2):2
Enter value (1-100):10
Enter creator (not empty):Hoa Phat
Enter height (1-100):10
Enter width (1-100):10
Enter isWatercolour (1-true, 0-false):-1
Enter isWatercolour (1-true, 0-false):1
Enter isFramed (1-true, 0-false):1
OUTPUT:
Painting's information:
Value: 10
Creator: Hoa Phat
Height: 10
Width: 10
isWatercolour: Yes
isFramed: Yes