
Lavi M.
asked 10/20/20How to do this? (Java)
Composition
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
extensions.
A class called Author is designed as shown in the class diagram. It contains:
name: String
email:String
gender: char--->'m' or'f'
Methods:
+Author (name : String, email : String,
gender: char)
Methods:
+getName():String
+get 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
changed.)
●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
Book: Author:
-name: String --name:String
-author: Author -------(1)-----> --email:StrinE
-price: double --gender: char
-qty: int
Methods:
+Book(name: String, author: Author,
price: double, qty: int)
+getName () : String
+getAuthor () :Author
+getPrice(): double
+setPrice (price: double) : void
+getQty(): int
+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".
1 Expert Answer

Patrick B. answered 10/20/20
Math and computer tutor/teacher
//********* Author.java ****************//
class Author
{
private String name;
private String email;
private boolean gender; //male=false; female=true;
public Author( String name, String email, boolean gender)
{
this.name = new String(name);
this.email = new String(email);
this.gender = gender;
}
public String getName() { return(name); }
public String getEmail() { return(email); }
public boolean getGender() { return(gender); }
public Author(Author author)
{
this.name = new String(author.getName());
this.email = new String(author.getEmail());
this.gender = author.getGender();
}
public void setName( String name) { this.name = new String(name); }
public void setEmail(String email) { this.email = new String(email); }
public void setGenderMale() { this.gender = false; }
public void setGenderFemale() { this.gender = true; }
@Override public String toString()
{
char chGender = (this.gender) ? 'F' : 'M';
return(
new String(
this.name + "(" + chGender + ") at " + this.email
)
);
}
}
//******** Book.java ***********************//
class Book
{
private String title;
private Author author;
private double price;
private int qty;
Book( String title, Author author, double price, int qty)
{
this.title = new String(title);
this.author = new Author(author);
this.price = price;
this.qty = qty;
}
public String getTitle() { return(title); }
public Author getAuthor() { return new Author(this.author); }
public double getPrice() { return(price); }
public int getQty() { return(qty); }
Book( Book book)
{
this.title = book.getTitle();
this.author = new Author( book.getAuthor());
this.price = book.getPrice();
this.qty = book.getQty();
}
public void setPrice(double price) { this.price = price; }
public void setQty( int qty) { this.qty = qty; }
@Override public String toString()
{
return(
new String(
"book title:" + this.title + " by Author " + this.author.toString() +
" price = " + this.price + " : qty = " + this.qty
)
);
}
}
//**** main ************//
class LaviMain
{
public void Go()
{
Author author = new Author("Patrick Baldwin","[email protected]",false);
Book book = new Book("How to create nested objects in Java",author,42.35,10);
System.out.println(book.toString());
}
public static void main(String args[])
{
LaviMain x = new LaviMain();
x.Go();
}
}
Lavi M.
Thanks but its missing the collection and the implements and especially where The program will print a selection screen where the user can choose the operation he/she wants to perform. The selection screen will be repeated after each selection until the staff type the number 4 to completely exit from the program: 1. Add an administration staff (by providing all her/his information) to the list of all administration staff 2. Add a doctor (by providing all her/his information) to the list of all doctors 3. Print all working staff (remember to differentiate between administration staff and doctors in the output printout 4. Exit the program10/31/20
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section under this link.10/20/20