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

JavaOOP_Hw6_PolymorphismAbstraction

The document discusses the principles of writing good programs, emphasizing the importance of coding style, documentation, and understanding polymorphism and abstraction in object-oriented programming. It includes exercises on creating abstract classes, interfaces, and their implementations, specifically focusing on geometric shapes and movable objects. Additionally, it outlines the need for proper naming conventions, meaningful comments, and the significance of learning through practice rather than passive observation.

Uploaded by

Minh Khôi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaOOP_Hw6_PolymorphismAbstraction

The document discusses the principles of writing good programs, emphasizing the importance of coding style, documentation, and understanding polymorphism and abstraction in object-oriented programming. It includes exercises on creating abstract classes, interfaces, and their implementations, specifically focusing on geometric shapes and movable objects. Additionally, it outlines the need for proper naming conventions, meaningful comments, and the significance of learning through practice rather than passive observation.

Uploaded by

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

HaQT Object-Oriented Programming

Homework 6. Polymorphism & Abstraction

Writing Good Programs


The only way to learn programming is program, program and program. Learning
programming is like learning cycling, swimming or any other sports. You can’t learn
by watching or reading books. Start to program immediately. On the other hands, to
improve your programming, you need to read many books and study how the masters
program.
It is easy to write programs that work. It is much harder to write programs that not
only work but also easy to maintain and understood by others – I call these good
programs. In the real world, writing program is not meaningful. You have to write
good programs, so that others can understand and maintain your programs.
Pay particular attention to:

1. Coding style:

• Read Java code convention: ”Java Style and Commenting Guide”.


• Follow the Java Naming Conventions for variables, methods, and classes
STRICTLY. Use CamelCase for names. Variable and method names begin
with lowercase, while class names begin with uppercase. Use nouns for
variables (e.g., radius) and class names (e.g., Circle). Use verbs for methods
(e.g., getArea(), isEmpty()).
• Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2,
and x1688 - they are meaningless. Avoid single-alphabet names like i, j, k.
They are easy to type, but usually meaningless. Use single-alphabet names
only when their meaning is clear, e.g., x, y, z for co-ordinates and i for array
index. Use meaningful names like row and col (instead of x and y, i and j,
x1 and x2), numStudents (not n), maxGrade, size (not n), and upperbound
(not n again). Differentiate between singular and plural nouns (e.g., use
books for an array of books, and book for each item).
• Use consistent indentation and coding style. Many IDEs (such as Eclipse /
NetBeans) can re-format your source codes with a single click.

2. Program Documentation: Comment! Comment! and more Comment to


explain your code to other people and to yourself three days later.

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

1 Exercises on Polymorphism, Abstract Classes and


Interfaces
1.1 Abstract Superclass Shape and Its Concrete Subclasses
Rewrite the superclass Shape and its subclasses Circle, Rectangle and Square, as shown in
the class diagram.

Shape is an abstract class containing 2 abstract methods: getArea() and getPerimeter(),


where its concrete subclasses must provide its implementation. All instance variables shall
have protected access, i.e., accessible by its subclasses and classes in the same package. Mark
all the overridden methods with annotation @Override.

2
HaQT Object-Oriented Programming

In this exercise, Shape shall be defined as an abstract class, which contains:


• Two protected instance variables color(String) and filled (boolean). The protected
variables can be accessed by its subclasses and classes in the same package. They are
denoted with a ’#’ sign in the class diagram.
• Getter and setter for all the instance variables, and toString().
• Two abstract methods getArea() and getPerimeter() (shown in italics in the class
diagram).
The subclasses Circle and Rectangle shall override the abstract methods getArea() and get-
Perimeter() and provide the proper implementation. They also override the toString().
Write a test class to test these statements involving polymorphism and explain the outputs.
Some statements may trigger compilation errors. Explain the errors, if any.

1 Shape shape1 = new C i r c l e ( 5 . 5 , ” r e d ” , f a l s e ) ; // Upcast C i r c l e t o


,→ Shape
System . out . p r i n t l n ( shape1 ) ; // which v e r s i o n ?
3 System . out . p r i n t l n ( shape1 . getArea ( ) ) ; // which v e r s i o n ?
System . out . p r i n t l n ( shape1 . g e t P e r i m e t e r ( ) ) ; // which v e r s i o n ?
5 System . out . p r i n t l n ( shape1 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( shape1 . i s F i l l e d ( ) ) ;
7 System . out . p r i n t l n ( shape1 . g e t R a d i u s ( ) ) ;

9 C i r c l e c i r c l e 1 = ( C i r c l e ) shape1 ; // Downcast back t o


,→ C i r c l e
System . out . p r i n t l n ( c i r c l e 1 ) ;
11 System . out . p r i n t l n ( c i r c l e 1 . getArea ( ) ) ;
System . out . p r i n t l n ( c i r c l e 1 . g e t P e r i m e t e r ( ) ) ;
13 System . out . p r i n t l n ( c i r c l e 1 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( c i r c l e 1 . i s F i l l e d ( ) ) ;
15 System . out . p r i n t l n ( c i r c l e 1 . g e t R a d i u s ( ) ) ;

17 Shape shape2 = new Shape ( ) ;

19 Shape shape3 = new R e c t a n g l e ( 1 . 0 , 2 . 0 , ” r e d ” , f a l s e ) ; // Upcast


System . out . p r i n t l n ( shape3 ) ;
21 System . out . p r i n t l n ( shape3 . getArea ( ) ) ;
System . out . p r i n t l n ( shape3 . g e t P e r i m e t e r ( ) ) ;
23 System . out . p r i n t l n ( shape3 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( shape3 . get L en g t h ( ) ) ;
25
R e c t a n g l e r e c t a n g l e 1 = ( R e c t a n g l e ) shape3 ; // downcast
27 System . out . p r i n t l n ( r e c t a n g l e 1 ) ;
System . out . p r i n t l n ( r e c t a n g l e 1 . getArea ( ) ) ;
29 System . out . p r i n t l n ( r e c t a n g l e 1 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( r e c t a n g l e 1 . ge t Le n g th ( ) ) ;
31
Shape shape4 = new Square ( 6 . 6 ) ; // Upcast
33 System . out . p r i n t l n ( shape4 ) ;

3
HaQT Object-Oriented Programming

System . out . p r i n t l n ( shape4 . getArea ( ) ) ;


35 System . out . p r i n t l n ( shape4 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( shape4 . g e t S i d e ( ) ) ;
37

// Take n o t e t h a t we downcast Shape shape4 t o R e c t a n g l e ,


39 // which i s a s u p e r c l a s s o f Square , i n s t e a d o f Square
R e c t a n g l e r e c t a n g l e 2 = ( R e c t a n g l e ) shape4 ;
41 System . out . p r i n t l n ( r e c t a n g l e 2 ) ;
System . out . p r i n t l n ( r e c t a n g l e 2 . getArea ( ) ) ;
43 System . out . p r i n t l n ( r e c t a n g l e 2 . g e t C o l o r ( ) ) ;
System . out . p r i n t l n ( r e c t a n g l e 2 . g e t S i d e ( ) ) ;
45 System . out . p r i n t l n ( r e c t a n g l e 2 . ge t Le n g th ( ) ) ;

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

What is the usage of the abstract method and abstract class?

1.2 GeometricObject Interface and its Implementation Classes


Circle and Rectangle
Write an interface called GeometricObject, which contains 2 abstract methods: getArea()
and getPerimeter(), as shown in the class diagram. Also write an implementation class called
Circle. Mark all the overridden methods with annotation @Override.

4
HaQT Object-Oriented Programming

1.3 Movable Interface and its Implementation MovablePoint Class


Write an interface called Movable, which contains 4 abstract methods moveUp(), move-
Down(), moveLeft() and moveRight(), as shown in the class diagram. Also write an im-
plementation class called MovablePoint. Mark all the overridden methods with annotation
@Override.

1.4 Movable Interface and its Implementation Classes Movable-


Point and MovableCircle
Write an interface called Movable, which contains 4 abstract methods moveUp(), move-
Down(), moveLeft() and moveRight(), as shown in the class diagram. Also write the imple-
mentation classes called MovablePoint and MovableCircle. Mark all the overridden methods
with annotation @Override.

5
HaQT Object-Oriented Programming

1.5 Interfaces Resizable and GeometricObject

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:

public interface Resizable {


2 p u b l i c double r e s i z e ( . . . ) ;
}

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.

1.6 Abstract Superclass Animal and its Implementation Subclasses


Write the codes for all the classes shown in the class diagram. Mark all the overridden
methods with annotation @Override.

8
HaQT Object-Oriented Programming

1.7 Another View of Abstract Superclass Animal and its Imple-


mentation Subclasses
Examine the following codes and draw the class diagram.

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 }

1.8 Interface Movable and its implementation subclasses Movable-


Point and MovableCircle
Suppose that we have a set of objects with some common behaviors: they could move up,
down, left or right. The exact behaviors (such as how to move and how far to move) depend
on the objects themselves. One common way to model these common behaviors is to define
an interface called Movable, with abstract methods moveUp(), moveDown(), moveLeft()
and moveRight(). The classes that implement the Movable interface will provide actual
implementation to these abstract methods.

Let’s write two concrete classes - MovablePoint and MovableCircle - that implement the
Movable interface.

10
HaQT Object-Oriented Programming

The code for the interface Movable is straight forward.

1 p u b l i c i n t e r f a c e Movable { // saved a s ” Movable . j a v a ”


p u b l i c v o i d moveUp ( ) ;
3 ......
}

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.

p u b l i c c l a s s MovablePoint implements Movable { // saved a s ”


,→ MovablePoint . j a v a ”
2 // i n s t a n c e v a r i a b l e s
int x ;
4 int y ;
i n t xSpeed ;
6 i n t ySpeed ; // package a c c e s s

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 ......
}

1 p u b l i c c l a s s M o v a b l e C i r c l e implements Movable { // saved a s ”


,→ M o v a b l e C i r c l e . j a v a ”
// i n s t a n c e v a r i a b l e s
3 p r i v a t e MovablePoint c e n t e r ; // can u s e c e n t e r . x , c e n t e r . y d i r e c t l y
// b e c a u s e they a r e package a c c e s s i b l e
5 private int radius ;

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 ......

15 // 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


@Override
17 p u b l i c v o i d moveUp ( ) {
c e n t e r . y −= c e n t e r . ySpeed ;
19 }
......
21 }

Write a test program and try out these statements:

1 Movable m1 = new MovablePoint ( 5 , 6 , 1 0 , 1 5 ) ; // u p c a s t


System . out . p r i n t l n (m1) ;
3 m1 . moveLeft ( ) ;
System . out . p r i n t l n (m1) ;

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.

What is the difference between an interface and an abstract class?

13

You might also like