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

Lab7-Abstract Class and Interface

This document provides instructions for a lab assignment on implementing abstract classes and interfaces in Java. It contains 3 questions: 1. Modify the Shape hierarchy to make Shape an abstract class with an abstract draw() method. This produces a compile error when trying to instantiate Shape. 2. Create an interface LabelledShape with an abstract displayName() method. Make Triangle implement this interface. This ensures any shape can display its name. 3. Design and implement a multi-level menu system using abstract classes and/or interfaces. The menu allows navigating options for File, Edit, and Help and displays the selected choices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Lab7-Abstract Class and Interface

This document provides instructions for a lab assignment on implementing abstract classes and interfaces in Java. It contains 3 questions: 1. Modify the Shape hierarchy to make Shape an abstract class with an abstract draw() method. This produces a compile error when trying to instantiate Shape. 2. Create an interface LabelledShape with an abstract displayName() method. Make Triangle implement this interface. This ensures any shape can display its name. 3. Design and implement a multi-level menu system using abstract classes and/or interfaces. The menu allows navigating options for File, Edit, and Help and displays the selected choices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SSK3101 Update : 20 May 2021

UNIVERSITI PUTRA MALAYSIA


FAKULTI SAINS KOMPUTER DAN TEKNOLOGI MAKLUMAT

LAB ASSIGNMENT 7

OBJECTIVE:
Learning Objective: Construct An Object-oriented Program Using Abstract and Interface
(P4)

PROGRAMME OUTCOME (PO):


PO2: Combine existing utilities to build large-scale problem's application

DURATION:
1 week / 3 hours

SUBMISSION:
Please submit your assignment to your demonstrator containing the following materials:
- UML diagram
- Java source code
- List of the test data (if involved user input)
- Print screen of the output.

DUE DATE OF SUBMISSION: Refer to PutraBlast

1
SSK3101 Update : 20 May 2021

QUESTION 1:

1. (Implement and use the abstract class concept)

Retrieve your previous answer for Question 2 from Lab Assignment 4. To


accomplish the following task, you need to have the class Shape, Triangle, and
Square produced in the assignment.

1. Modify the class Shape to become an abstract class.

2. Modify the method draw() in class Shape to become an abstract method.

3. Create an empty main() method (the body of the main method should be empty).
Make sure you can compile without error.

4. Rewrite the following source codes as your main() method.

public static void main(String[] args)


{
Shape sh = new Shape(); //**
sh.draw(); //**
Triangle tr = new Triangle();
tr.draw();
Square sq = new Square();
sq.draw();
}

5. Recompile your program. You should get a compile-error.

6. Remove the first and second lines (marked with ‘//**’) and recompile your
program. Your program should be successfully compiled.

7. Explain:

a. Why class Shape should become abstract class instead of Triangle or


Square?
b. Why method draw() should become abstract method?
c. Why did the deleted lines produce compile-error?

QUESTION 2:

2. (Implement and use the interface concept)

1. Use the same program from Question 1 above.

2. Create a new interface named LabelledShape which contains an abstract method


named displayName(). The purpose of this method is to ensure any implementing

2
SSK3101 Update : 20 May 2021

class must print out a text indicating the name or the type of the Shape e.g. an
object of class Triangle should display “A Triangle”.

3. Modify the class Triangle to implement the interface LabelledShape. Implement a


method for the abstract method displayName() and display a text “A Triangle”.

4. Modify your main method as follows:

public static void main(String[] args)


{
Triangle tr = new Triangle();
tr.draw();
tr.displayName();
Square sq = new Square();
sq.draw();
}

8. Compile and run your program.

9. Explain:

a. Why the class Triangle must implement the method displayName()?


b. Why class Square don’t have to implement displayName() even though it
extends Shape?

QUESTION 3:

3. (Design and implement abstract class and interface from a specified problem)

You are assigned a task to develop a multi-level menu system for an application. The
menu system contains the following structure:

1-File
1-- New
2-- Open
3-- Close
4-- Print
2- Edit
3- Help
1-- Info
2-- About

In the main menu, user should be given the 3 options – File, Edit, Help. If user choose
File, she will be shown the options New, Open, Close, Print. If user choose New then
display “choose New”, or “choose Open” for Open and so on. If user choose Edit, display
“choose Edit”. If User choose Help, display the options Info and About. User can exit
anytime by choosing 0. For example, the following output in Figure 1 shows an example
of a user using the menu system in command prompt:

3
SSK3101 Update : 20 May 2021

MAIN MENU <continue…>


1 -File > MAIN MENU
2 -Edit 1 -File >
3 -About > 2 -Edit
[1-3, 0 to exit]? 1 3 -About >
1 --New [1-3, 0 to exit]? 3
2 --Open 1 --Help
3 --Close 2 --Info
4 --Print [1-2, 0 to exit]? 1
[1-4, 0 to exit]? 2
User choose Help
User choose Open
MAIN MENU
MAIN MENU 1 -File >
1 -File > 2 -Edit
2 -Edit 3 -About >
3 -About > [1-3, 0 to exit]? 0
[1-3, 0 to exit]? 2 System exit. Bye

User choose Edit

<continue…>
Figure 1 Figure 1 (cont.)

Develop the menu system by implementing the concept of abstract class or interface. You
may also use other Java features you already learned - array, looping, if-else statement,
etc.

(Tips: A menu can be represented by a class Menu. A menu item is contained by a menu
or a sub menu. A sub menu is a menu and also a menu item, thus should derive features
from Menu and Menu Item. )

You might also like