JavaOOP_Hw6_PolymorphismAbstraction
JavaOOP_Hw6_PolymorphismAbstraction
1. Coding style:
3. The problems in this tutorial are certainly NOT challenging. There are tens
of thousands of challenging problems available – used in training for various
programming contests (such as International Collegiate Programming Contest
(ICPC), International Olympiad in Informatics (IOI)).
1
HaQT Object-Oriented Programming
2
HaQT Object-Oriented Programming
3
HaQT Object-Oriented Programming
47 // Downcast R e c t a n g l e r e c t a n g l e 2 t o Square
Square s q u a r e 1 = ( Square ) r e c t a n g l e 2 ;
49 System . out . p r i n t l n ( s q u a r e 1 ) ;
System . out . p r i n t l n ( s q u a r e 1 . getArea ( ) ) ;
51 System . out . p r i n t l n ( s q u a r e 1 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( s q u a r e 1 . g e t S i d e ( ) ) ;
53 System . out . p r i n t l n ( s q u a r e 1 . g et L e ng t h ( ) ) ;
4
HaQT Object-Oriented Programming
5
HaQT Object-Oriented Programming
6
HaQT Object-Oriented Programming
1. Write the interface called GeometricObject, which declares two abstract methods: get-
Parameter() and getArea(), as specified in the class diagram.
Hints:
1 p u b l i c i n t e r f a c e GeometricObject {
p u b l i c double getPerimeter ( ) ;
3 ......
}
2. Write the implementation class Circle, with a protected variable radius, which imple-
ments the interface GeometricObject.
Hints:
1 p u b l i c c l a s s C i r c l e implements GeometricObject {
// P r i v a t e v a r i a b l e
3 ......
5 // C o n s t r u c t o r
......
7
// Implement methods d e f i n e d i n t h e i n t e r f a c e GeometricObject
9 @Override
p u b l i c double getPerimeter ( ) { . . . . . . }
11
......
13 }
3. Write a test program called TestCircle to test the methods defined in Circle.
4. The class ResizableCircle is defined as a subclass of the class Circle, which also imple-
ments an interface called Resizable, as shown in class diagram. The interface Resizable
declares an abstract method resize(), which modifies the dimension (such as radius)
by the given percentage. Write the interface Resizable and the class ResizableCircle.
Hints:
7
HaQT Object-Oriented Programming
p u b l i c c l a s s R e s i z a b l e C i r c l e e x t e n d s C i r c l e implements
,→ R e s i z e a b l e {
2
// C o n s t r u c t o r
4 p u b l i c R e s i z a b l e C i r c l e ( double r a d i u s ) {
super ( . . . ) ;
6 }
8 // Implement methods d e f i n e d i n t h e i n t e r f a c e R e s i z a b l e
@Override
10 p u b l i c double r e s i z e ( i n t percent ) { . . . . . . }
}
5. Write a test program called TestResizableCircle to test the methods defined in Resiz-
ableCircle.
8
HaQT Object-Oriented Programming
a b s t r a c t p u b l i c c l a s s Animal {
2 abstract public void g r e e t i n g ( ) ;
}
1 p u b l i c c l a s s Cat e x t e n d s Animal {
@Override
3 public void g r e e t i n g ( ) {
System . out . p r i n t l n ( ”Meow ! ” ) ;
5 }
}
p u b l i c c l a s s Dog e x t e n d s Animal {
2 @Override
public void g r e e t i n g ( ) {
4 System . out . p r i n t l n ( ”Woof ! ” ) ;
}
6
p u b l i c v o i d g r e e t i n g ( Dog a n o t h e r ) {
8 System . out . p r i n t l n ( ” Woooooooooof ! ” ) ;
}
10 }
p u b l i c c l a s s BigDog e x t e n d s Dog {
2 @Override
public void g r e e t i n g ( ) {
4 System . out . p r i n t l n ( ”Woow! ” ) ;
}
6
@Override
8 p u b l i c v o i d g r e e t i n g ( Dog a n o t h e r ) {
System . out . p r i n t l n ( ”Woooooowwwww ! ” ) ;
10 }
}
9
HaQT Object-Oriented Programming
Explain the outputs (or error) for the following test program.
1 p u b l i c c l a s s TestAnimal {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 // Using t h e s u b c l a s s e s
Cat c a t 1 = new Cat ( ) ;
5 cat1 . greeting () ;
Dog dog1 = new Dog ( ) ;
7 dog1 . g r e e t i n g ( ) ;
BigDog bigDog1 = new BigDog ( ) ;
9 bigDog1 . g r e e t i n g ( ) ;
11 // Using Polymorphism
Animal animal1 = new Cat ( ) ;
13 animal1 . g r e e t i n g ( ) ;
Animal animal2 = new Dog ( ) ;
15 animal2 . g r e e t i n g ( ) ;
Animal animal3 = new BigDog ( ) ;
17 animal3 . g r e e t i n g ( ) ;
Animal animal4 = new Animal ( ) ;
19
// Downcast
21 Dog dog2 = ( Dog ) animal2 ;
BigDog bigDog2 = ( BigDog ) animal3 ;
23 Dog dog3 = ( Dog ) animal3 ;
Cat c a t 2 = ( Cat ) animal2 ;
25 dog2 . g r e e t i n g ( dog3 ) ;
dog3 . g r e e t i n g ( dog2 ) ;
27 dog2 . g r e e t i n g ( bigDog2 ) ;
bigDog2 . g r e e t i n g ( dog2 ) ;
29 bigDog2 . g r e e t i n g ( bigDog1 ) ;
}
31 }
Let’s write two concrete classes - MovablePoint and MovableCircle - that implement the
Movable interface.
10
HaQT Object-Oriented Programming
For the MovablePoint class, declare the instance variable x, y, xSpeed and ySpeed with
package access as shown with ’ ’ in the class diagram (i.e., classes in the same package
can access these variables directly). For the MovableCircle class, use a MovablePoint to
represent its center (which contains four variable x, y, xSpeed and ySpeed). In other words,
the MovableCircle composes a MovablePoint, and its radius.
8 // C o n s t r u c t o r
p u b l i c MovablePoint ( i n t x , i n t y , i n t xSpeed , i n t ySpeed ) {
11
HaQT Object-Oriented Programming
10 this .x = x;
......
12 }
......
14
// Implement a b s t r a c t methods d e c l a r e d i n t h e i n t e r f a c e Movable
16 @Override
p u b l i c v o i d moveUp ( ) {
18 y −= ySpeed ; // y−a x i s p o i n t i n g down f o r 2D g r a p h i c s
}
20 ......
}
7 // C o n s t r u c t o r
p u b l i c M o v a b l e C i r c l e ( i n t x , i n t y , i n t xSpeed , i n t ySpeed , i n t r a d i u s
,→ ) {
9 // C a l l t h e MovablePoint ’ s c o n s t r u c t o r t o a l l o c a t e t h e c e n t e r
,→ i n s t a n c e .
c e n t e r = new MovablePoint ( x , y , xSpeed , ySpeed ) ;
11 ......
}
13 ......
12
HaQT Object-Oriented Programming
5
Movable m2 = new M o v a b l e C i r c l e ( 1 , 2 , 3 , 4 , 2 0 ) ; // u p c a s t
7 System . out . p r i n t l n (m2) ;
m2 . moveRight ( ) ;
9 System . out . p r i n t l n (m2) ;
Write a new class called MovableRectangle, which composes two MovablePoints (representing
the top-left and bottom-right corners) and implementing the Movable Interface. Make sure
that the two points has the same speed.
13