Post date: Feb 04, 2015 7:45:52 AM
Chapter 7, 8: Polymorphism
7.1 Static Binding
7.2 Dynamic Binding
7.4 Polymorphism
(Selection of Method and Incremental Development)
Modularity
Practical Exercises:
POLYMORPHISM: In class exercises
http://triton.towson.edu/~izimand/237/Exercises/Ex-Polymorphism.html
Using an inheritance hierarchy, design a Java program to model 3-dimensional shapes (sphere and cube). Make a top level shape interface with methods for getting the area and the volume (+ method toString [and equals](optional) ). Next, build classes and subclasses for the above 3dimensional shapes. make sure that you place common behavior in superclasses whenever possible. Also, use abstract classes as appropriate. Add methods to subclasses to represent unique behavior particular to each 3-dimensional shape.
Write the definitions of these classes and a client program showing them in use.
Exercise: The Author and Book Classes
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html
A class called Author is designed as shown in the class diagram. It contains:
Three private instance variables: name (String),email (String), and gender (char of either 'm' or'f');
One constructor to initialize the name, email andgender with the given values;
public Author (String name, String email, char gender) {......}
(There is no default constructor for Author, as there are no defaults for name, email and gender.)
public getters/setters: getName(), getEmail(),setEmail(), and getGender();
(There are no setters for name and gender, as these attributes cannot be changed.)
A toString() method that returns "author-name (gender) at email", e.g., "Tan Ah Teck (m) at ahTeck@somewhere.com".
Write the Author class. Also write a test program called TestAuthor to test the constructor and public methods. Try changing theemail of an author, e.g.,
Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm'); System.out.println(anAuthor); // call toString() anAuthor.setEmail("paul@nowhere.com") System.out.println(anAuthor);
A class called Book is designed as shown in the class diagram. It contains:
Four private instance variables:name (String), author (of the classAuthor you have just created, assume that each book has one and only one author), price (double), and qtyInStock (int);
Two constructors:
public Book (String name, Author author, double price) {...} public Book (String name, Author author, double price, int qtyInStock) {...}
public methods getName(),getAuthor(), getPrice(),setPrice(), getQtyInStock(),setQtyInStock().
toString() that returns "'book-name' by author-name (gender) at email".
(Take note that the Author's toString() method returns "author-name (gender) at email".)
Write the class Book (which uses the Author class written earlier). Also write a test program called TestBook to test the constructor andpublic methods in the class Book. Take Note that you have to construct an instance of Author before you can construct an instance ofBook. E.g.,
Author anAuthor = new Author(......); Book aBook = new Book("Java for dummy", anAuthor, 19.95, 1000); // Use an anonymous instance of Author Book anotherBook = new Book("more Java for dummy", new Author(......), 29.95, 888);
Take note that both Book and Author classes have a variable called name. However, it can be differentiated via the referencing instance. For a Book instance says aBook, aBook.name refers to the name of the book; whereas for an Author's instance say auAuthor,anAuthor.name refers to the name of the author. There is no need (and not recommended) to call the variables bookName andauthorName.
TRY:
Printing the name and email of the author from a Book instance. (Hint: aBook.getAuthor().getName(),aBook.getAuthor().getEmail()).
Introduce new methods called getAuthorName(), getAuthorEmail(), getAuthorGender() in the Book class to return the name,email and gender of the author of the book. For example,
public String getAuthorName() { ...... }