Lesson 4
Classes and objects relations.
Objectives
- Understanding the concepts of agregation and inheritance.
- Implementing UML class diagrams.
Documentation
Pachete java
Specificatori de acces
Reutilizarea claselor
Thinking in Java : Chapter 6 - Reusing classes
Exercises
Considering the exercises bellow. Follow the instructions and implement the programs. For each program create an appropriate test class for testing each program.
Exercise 1
A class called circle is designed as shown in the following class diagram. It contains:
- Two private instance variables: radius (of type double) and color (of type String), with default value of 1.0 and “red”, respectively.
- Two overloaded constructors;
- Two public methods: getRadius() and getArea().
Exercise 2
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 and gender 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”
Exercise 3
A class called Book is designed as shown in the class diagram. It contains:
- Four private instance variables: name (String), author (of the class Author 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”.)
Exercise 4
Modify the Book class from previous exercise to support one or more authors by changing the instance variable authors to an Author array. Reuse the Author class written earlier.
Notes:
- The constructors take an array of Author (i.e., Author[]), instead of an Author instance.
- The toString() method shall return “book-name by n authors”, where n is the number of authors.
- A new method printAuthors() to print the names of all the authors.
- You shall re-use the Author class written earlier.
Exercise 5
In this exercise, a subclass called Cylinder is derived from the superclass Circle as shown in the class diagram.
Notes:
- Reuse Circle class from the previous exercise;
- Use 'super' keyword to call constructors from the base class;
- Observe that getArea() method from derived class returns a wrong value because it uses the formula for circle;
- Overwrite getArea() method in order to correctly calculate the area for the derived class;
Exercise 6
Write a superclass called Shape (as shown in the class diagram), which contains:
- Two instance variables color (String) and filled (boolean).
- Two constructors: a no-arg (no-argument) constructor that initializes the color to “green” and filled to true, and a constructor that initializes the color and filled to the given values.
- Getter and setter for all the instance variables. By convention, the getter for a boolean variable xxx is called isXXX() (instead of getXxx() for all the other types).
- A toString() method that returns “A Shape with color of xxx and filled/Not filled”.
The Circle class contains:
- An instance variable radius (double).
- Three constructors as shown. The no-arg constructor initializes the radius to 1.0.
- Getter and setter for the instance variable radius.
- Methods getArea() and getPerimeter().
- Override the toString() method inherited, to return “A Circle with radius=xxx, which is a subclass of yyy”, where yyy is the output of the toString() method from the superclass.
The Rectangle class contains:
- Two instance variables width (double) and length (double).
- Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.
- Getter and setter for all the instance variables.
- Methods getArea() and getPerimeter().
- Override the toString() method inherited, to return “A Rectangle with width=xxx and length=zzz, which is a subclass of yyy”, where yyy is the output of the toString() method from the superclass.
Write a class called Square, as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.
- Provide the appropriate constructors
- Override the toString() method to return “A Square with side=xxx, which is a subclass of yyy”, where yyy is the output of the toString() method from the superclass.
- Override the setLength() and setWidth() to change both the width and length, so as to maintain the square geometry.