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

COMP 2030SEF-Lab03-Interface

The document outlines instructions for a lab exercise in COMP 2030SEF, focusing on the implementation of an interface class in Java. It includes steps for setting up the environment, studying example classes, and creating a new class 'InterfaceShape' with methods for calculating perimeter and area, as well as drawing shapes. The lab requires the creation of classes 'Square' and 'Rectangle' that implement the 'Shape' interface, along with specific output requirements for each shape.

Uploaded by

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

COMP 2030SEF-Lab03-Interface

The document outlines instructions for a lab exercise in COMP 2030SEF, focusing on the implementation of an interface class in Java. It includes steps for setting up the environment, studying example classes, and creating a new class 'InterfaceShape' with methods for calculating perimeter and area, as well as drawing shapes. The lab requires the creation of classes 'Square' and 'Rectangle' that implement the 'Shape' interface, along with specific output requirements for each shape.

Uploaded by

zirunw6
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 2030SEF Lab 03 Interface Class

Instructions

1. Download the lab materials from OLE and unzip it to your desktop.

2. Boot up the partition Ewin10 in school labs.


3. Open the sample and source folders contain project files using NetBeans.
4. The sample project contains examples discussed in the lecture.
5. The source project contains source codes for the lab exercises.

[Part I] Example

Study the examples provided from the sample project with the order specified below:
 InAnimal.java
- Animal (superclass: interface class)
- Rabbit (subclass)
- Snake (subclass)

[Part II] Demonstration

Q1 Basic tasks on Interface


Create a java file named InterfaceShape.java, and then complete all the following into
this java file:
i. Create an interface Shape with three abstract methods details below:
a. abstract method perimeter() returns double type
b. abstract method area() returns double type
c. abstract method drawShape() with no return value

ii. Create a class Square with details below:


a. Add an attribute length (double type)
b. Add a constructor method Square(double aLength)
c. Add a getter method getLength()
d. Add a method perimeter() which returns the calculated perimeter value.

1
iii. Create a class InterfaceShape, with details below:
a. Add a main method():
class InterfaceShape {
public static void main(String[] args) {

}
}
b. Add a new Square object mySquare and call the constructor method passing 3
as the parameter.
c. Call the appropriate methods to output the following:
---------- Capture Output ----------
[Square]
length: 3.0
perimeter: 12.0

iv. In class Square, which implements Shape and add details as below:
a. Add a method area() which returns the area value.
b. Add a method drawShape() [no returned value] which prints out a square
shape.

v. In class InterfaceShape, call the appropriate methods to output the following:


---------- Capture Output ----------
area: 9.0

drawShape:
***
***
***

vi. Create a class Rectangle which implements Shape with details below:
a. Add an attributes width and height (double type)
b. Add a constructor method Rectangle(double aWidth, double aHeight)
c. Add a getter methods
d. Add a method perimeter() which returns the calculated perimeter value.
e. Add a method area() which returns the area value.
f. Add a method drawShape() [no returned value] which prints out a square
shape.

2
vii. In class InterfaceShape, add details below:
a. Add a new Rectangle object myRectangle and call the constructor method as
i.e. Rectangle(5, 2)
b. Call the appropriate methods to output the following:
---------- Capture Output ----------
[Rectangle]
Width: 5.0
Height: 2.0
perimeter: 14.0
area: 10.0

drawShape:
*****
*****

viii. Finally, check your output as:

---------- Capture Output ----------


[Square]
length: 3.0
perimeter: 12.0
area: 9.0

drawShape:
***
***
***

[Rectangle]
Width: 5.0
Height: 2.0
perimeter: 14.0
area: 10.0

drawShape:
*****
*****

You might also like