How to do this? (Java)
There are two ways to reuse existing classes, namely, composition and inheritance.
With composition (aka aggregation), you define a new class, which is composed of existing
classes. With inheritance, you derive a new class based on an existing class, with modifications or
A class called Author is designed as shown in the class diagram. It contains:
gender: char--->'m' or'f'
+Author (name : String, email : String,
+set Emaill(email: String) : void
+getGender (): char
+toString ():String--->"name (gender) at email"
● Three private member variables: name (String), email (String), and gender (char of
either 'm' or 'f' - you might also use a boolean variable called isMale having value
of true or false).
●A constructor to initialize the name, email and gender with the given values.
(There is no default constructor, as there is no default value for name, email and gender.)
●Public getters/setters: getName(), getEmail(), setEmail(), and getGender().
(There are no setters for name and gender, as these properties are not designed to be
●A toString() method that returns "name (gender) at email", e.g., "Tan Ah Teck (m) at
A Book is written by one Author - Using an "Object" Member Variable
-name: String --name:String
-author: Author -------(1)-----> --email:StrinE
-price: double --gender: char
+Book(name: String, author: Author,
price: double, qty: int)
+getName () : String
+getAuthor () :Author
+setPrice (price: double) : void
+setQty (qty: int) : void
+toString(): String--->" 'book-name' by author-name (gender) at email"
■Let's design a Book class. Assume that a book is written by one (and exactly one) author. The
Book class (as shown in the class diagram) contains the following members:
●Four private member variables: name (String), author (an instance of the Author class we
have just created, assuming that each book has exactly one author), price (double),
and qty (int).
●The public getters and
setters: getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty().
●A toString() that returns "'book-name' by author-name (gender) at email". You could reuse
the Author's toString() method, which returns "author-name (gender) at email".