Java Exercise9 [Abstract Class and Interface]
1. Write a Java program which defines an abstract GeometricObject class and contains two
abstract methods getArea() and getPerimeter() which are implemented in the subclasses Circle
and Rectangle as shown in the following class diagram. Write a test program which implements
all the classes and accept all the necessary inputs from keyboard.
GeometricObject
- color : String
- filled : boolean
- dateCreated : Date
#GeometricObject(c : String, f : boolean)
+ getColor() : String
+ setColor(c : String):void
+ isFilled(f: boolean): boolean
+ setFilled(f:boolean):void
+ getDeteCreated():Date
+ getArea():double
+getPerimeter():double
Circle Rectangle
-radius : double - width : double
+ Circle(c : String, f : Boolean, rad : double) - height : double
+ getRadius() : double + Rectangle(c : String, f : Boolean, w : double, h: double)
+ setRadius(rad : double):void + getWidth() : double
+ getDiameter():double + setWidth(w : double):void
+ prictCircle():void + getHeight(): double
+ setHeight(h:double):void
+ prictRectangle():void
2. Write a Java program which defines an abstract Animal class and an interface Edible and
contains two abstract methods howToEat() and sound() which are implemented in their
subclasses as shown in the following class diagram. Write a test program which implements all
the classes and accept all the necessary inputs from keyboard.
«interface» Animal
Edible
+sound(): String
+howToEat(): String
Fruit Chicken Tiger
- - -
Orange Apple
1
- -
3. Create an interface called Vehicle with methods start() and stop() and then create two
subclasses Car and Motorcycle these classes implement all the methods in the interface.
4. Create an outer class called University with static inner class Department and non-static inner
class Student as show in the following class diagram. Write a test program that implements the
inner classes.
University
+ universityName : String
+ location : String
+ University(location : String)
+ displayUniversity() : void
Department Student
~ departmentName : String ~ id : int
~ noOfStudent: int ~ name : String
~ Department(depName : String, noStud : int) ~ Student(id : int, name : String)
~ displayDepartment():void ~ displayStudent():void