Oop Lab Assignment (Abstract Class and Interface
Oop Lab Assignment (Abstract Class and Interface
BS(IT)- Semester 02
(Fall 2023)
Engr. Saba
TOOL/
LAB: PAGE
SOFTWAR
NO:
EUSED
Lab 1 Getting Started with NetBeans NetBeans 8.0.2 4-8
Exercises
Exercise 1
Design and implement a subclass “EquilateralTriangle” having a double variable side denoting the
three sides of the equilateral triangle [Note that since all the 3 sides are equal, the constructor will have only one
parameter]. The area and perimeter of the equilateral triangle are given as follows:
Area = ¼* 3 *(side)2
Perimeter = 3*side
Provide accessor methods for the sides. Test your class using the TestShapes and
DownCastingShapes classes.
SOLUTION:
OUTPUT:
Exercises
Exercise 1
By looking at the formulae for an ellipse, provide the missing code for all of the methods in the class Ellipse
including the toString() method. Test your program using the TestShapes.java class. Your output
should look as follows (for an ellipse with a = 10 and b = 7) (values are randomly generated).
SOLUTION:
OUTPUT:
Exercise 2
How about the following class Circle.Since a Circle is a special case of an Ellipse, will the output of
TestShapes.java be affected if the following class is used instead of the class Circle used previously:
Circle.java
public class Circle extends Ellipse {
public Circle(double radius){
super(radius, radius);
}
}
<<Interface>>
Eccentric Shape
Ellipse
Rectangle
Circle Square
Solution:
Output:
Consider an abstract class Person with three private attributes FirstName,LastName and Age.
It has one abstract method
String toString();
Also, consider an interface Payable that contains only one method
double getPayment().
Class Staff has an additional attribute that is salary, which represents the monthly salary.
Class Studenthas an additional attribute that is grades, which represents the grade character like A,
B, C, D or F.
In addition, the class Staff implements the interface Payable.
The payment of the staff must return its annual salary (salary *12).
The toString method of the each class must return all attributes of the class in text format. For a
student, it returns the following string
o Student: FirstName, LastName, Age, Grade
For the Staff, it must return the following string
o Staff: FirstName, LastName, Age, Salary.
Department of Computer Sciences Semester BS IT 2
CSL-210 Object Oriented Programming 2Lab 10: Abstract Class And Interface
CSL-210: Object-Oriented Programming Lab
BS(IT)- Semester 02
(Fall 2023)
Engr. Saba
Write the application class and create 3 objects of Student and 3 objects of Staff class and print the details.
Solution :
Output: