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

Oop Lab Assignment (Abstract Class and Interface

The document discusses abstract classes and interfaces in Java. It provides examples of abstract classes like Shape and interfaces like Eccentric. It also discusses subclassing an abstract class and implementing an interface. Students are given exercises to design classes for shapes like triangles and circles as well as a take home assignment on abstract classes and interfaces.

Uploaded by

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

Oop Lab Assignment (Abstract Class and Interface

The document discusses abstract classes and interfaces in Java. It provides examples of abstract classes like Shape and interfaces like Eccentric. It also discusses subclassing an abstract class and implementing an interface. Students are given exercises to design classes for shapes like triangles and circles as well as a take home assignment on abstract classes and interfaces.

Uploaded by

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

CSL-210: Object-Oriented Programming Lab

BS(IT)- Semester 02
(Fall 2023)
Engr. Saba

Name: Ali Sufyan.


Enrollment no: 02-235232-017.

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

Lab10: Abstract Class and Interface


To gain experience with:
1. Abstract Classes and their purpose
2. Interfaces and their purpose
3. Exercise for practice

TOOL/
LAB: PAGE
SOFTWAR
NO:
EUSED
Lab 1 Getting Started with NetBeans NetBeans 8.0.2 4-8

Lab 2 Introduction to Java NetBeans 8.0.2 9 - 22

Lab 3 Structured Programming NetBeans 8.0.2 23 - 33

Lab 4 Methods and Recursion NetBeans 8.0.2

Lab 5 Classes and Objects NetBeans 8.0.2

Lab 6 Association and Aggregation NetBeans 8.0.2

Lab 7 Inheritance NetBeans 8.0.2

Lab 8 Polymorphism NetBeans 8.0.2

Lab 9 NetBeans GUI NetBeans 8.0.2

Lab Abstraction and Interfaces NetBeans 8.0.2


10
Lab Files I/O NetBeans 8.0.2
11
Lab JDBC NetBeans 8.0.2
12
Lab Exception Handling NetBeans 8.0.2
13

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

1. Abstract Classes (in Java)

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:

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

OUTPUT:

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

2. Interfaces (in Java)

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:

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

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

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

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

With this modification, the class diagram would look as follows:

<<Interface>>
Eccentric Shape

Ellipse
Rectangle

Circle Square

Solution:

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

Output:

Take Home Assignment

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

 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 :

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

Output:

Department of Computer Sciences Semester BS IT 2


CSL-210 Object Oriented Programming 2Lab 10: Abstract Class And Interface

You might also like