University of Gondar
Institute of Technology
Department of Computer Engineering
Advanced Programming
Part II: OOP in java lab exercise
1. Write a Java program to create a class called "Person" with a name, age, id, and department
attribute. Create two instances of the "Person" class, set their attributes using the
constructor, and print their name and age.
2. Write a Java program to create a class called Rectangle with private instance variables
length and width. Provide public getter and setter methods to access and modify these
variables. (encapsulation)
3. Write a Java program to create a class called Circle with a private instance variable radius.
Provide public getter and setter methods to access and modify the radius variable.
However, provide two methods called calculateArea() and calculatePerimeter() that return
the calculated area and perimeter based on the current radius value. (encapsulation)
4. Write a java program to create a class called Animal with a method makeSound() which
displays a message “animal sound”. Create a subclass called cat with a method catSound()
which displays a message “cat sound”. Create an object for the subclass and print the
messages of the two methods. (single inheritance)
5. Write a java program to create a class called Animal with a method makeSound() which
displays a message “animal sound”. Create a subclass called Cat with a method catSound()
which displays a message “cat sound”. Create an object for the subclass and print the
messages of the two methods. Create another subclass called BabyCat extends from Cat
with a method babyCatSound() which displays a message”baby cat sound”.create an object
for BabyCat and print the messages of all methods.(multilevel inheritance)
6. Write a java program to create a class called Animal with a method makeSound() which
displays a message “animal sound”. Create a subclass called cat with a method catSound()
which displays a message “cat sound”. Create another sub class called dog that extends
from Animal with a method dogSound() which displays a message “dog sound”. Create an
1|Page by Wondimu B.
object for cat and dog class and print the messages of the two superclass method.
(Hierarchical inheritance).
7. Write a java program to create a class called Display having three methods as follows: each
method calculates the sum of the two number and return back the value. (Polymorphism
using method overloading).
addition(double a, int b)
addition(double a, double b)
addition(int a, double b)
8. Write a Java program to create a class called Vehicle with a method called drive() which
prints a message”reparing a vehicle”. Create a subclass called Car that overrides the drive()
method to print "Repairing a car". In the main() method, create an instance of both the
Vehicle and Car classes, and call the drive() method on each object.(polymorphism using
method overriding)
9. Write a Java program to create an abstract class Shape with concrete methods display()
which prints “Abstraction is one feature of OOP” and abstract methods calculateArea() and
calculatePerimeter(). Create subclasses Circle and Rectangle that extend the Shape class
and implement the respective methods to calculate the area and perimeter of each shape.
(abstraction using abstract class).
10. Write a Java program to create an interface Shape with the getArea() method. Create two
classes Rectangle, and Circle, that implement the Shape interface. Implement the getArea()
method for each of the two classes.(abstraction using Interface)
11. Write a java program to create two interface named Walkable and Swimmable with the
walk(), and swim () method respectively. Create a class called Duck that implements the
the methods of the two interfaces. the first method prints a message”Duck is walking” and
the second method prints a message “Duck is swimming”. (multiple Inheritance using
interface in java )
2|Page by Wondimu B.