0% found this document useful (0 votes)
13 views

Lab 4

The document describes an exercise on inheritance and interfaces in Java. It defines classes like Item, Vase, Painting and interfaces like IIo, IUtility. The main method creates an item based on user input, and outputs its information by calling virtual methods.

Uploaded by

dongtnhe172234
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 4

The document describes an exercise on inheritance and interfaces in Java. It defines classes like Item, Vase, Painting and interfaces like IIo, IUtility. The main method creates an item based on user input, and outputs its information by calling virtual methods.

Uploaded by

dongtnhe172234
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

==================================================================

Lab 4

Inheritance and Interface

==================================================================

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 1: Create a new project named “Q1”.

Step 2: Create a package named “DTO”, it contains some files: IIo.java, Item.java,
Vase.java, and Painting.java

Step 3: Create a package named “Utility”, it contains files: IUtility.java, and


Utility.java

Step 4: Create a java class named “Main”.

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

Represents relationship of making use.

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

 The method output(): print out all fields

3. In the file Vase.java: Declares Vase class.


 The method input(Scanner sc): Using methods of Utility class to input
all fields of the Vase class. Verify: height from 1 to 100, metarial is not empty
 The method output(): print out all fields of the Vase class
Hint:
public class Vase{

public void input(Scanner sc){
input(sc); // call the inherited method
….

}
public void output(){
output(); // call the inherited method
System.out.println(“Height:” + height);
System.out.println(“Material:”+ material);
}

}

3. You do the same for Painting class

Verify: height from 1 to 100, width from 1 to 100

4. In file IUtility.java: Declares IUtility interface:

int getInt(Scanner sc, String msg, int min, int max);

It allows the user to enter an integer from min to max.

public static String getString(Scanner sc, String msg, String pattern);

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.

6. In the file “Main.java”. you type like as follow:


public class Main {
public static void main(String[] args) {
Item item=null;
int choice=0;
Scanner sc = new Scanner(System.in);
System.out.println("1. Create a Vase:");
System.out.println("2. Create a Painting:");
Utility utility = new Utility();
choice = utility.getInt(sc, "Enter choice (1-2):", 1, 2);
switch(choice){
case 1:
item = new Vase();
((Vase)item).input(sc);
break;
case 2:
item =new Painting();
((Painting) item).input(sc);
break;
}
if(item!=null){
System.out.println("OUTPUT:");
if(item instanceof Vase)
{
System.out.println("Vase's information:");
((Vase) item).output();
}
else if(item instanceof Painting)
{
System.out.println("Painting's information:");
((Painting) item).output();
}
}
else System.out.println("you need to create an object");

}
}
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

You might also like