Part 1: A class called Author (as shown in the class diagram) is designed to model a book's author. 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=?,email=?,gender=?]", e.g., "Author[name=Tan Ah Teck,email=ahTeck@somewhere.com,gender=m]". Write the Author class. Also, write a test driver (for testing purpose on your local machine) called TestAuthor to test all the public methods, e.g., Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); // Test the constructor System.out.println(ahTeck); // Test toString() ahTeck.setEmail("paulTan@nowhere.com"); // Test setter System.out.println("name is: " + ahTeck.getName()); // Test getter System.out.println("eamil is: " + ahTeck.getEmail()); // Test getter System.out.println("gender is: " + ahTeck.getGender()); // Test gExerciseOOP_MyPolynomial.pngetter Part 2:   A class called Book is designed (as shown in the class diagram) to model a book written by one or more authors. In reality, a book can be written by one or more authors. It contains: Four private instance variables: name (String), array of authors (of the class Author you have just created), price (double), and qty (int); Two constructors:public Book (String name, Author authors, double price) { ...... } public Book (String name, Author authors, double price, int qty) { ...... } public methods getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty(). The constructors take an array of Author (i.e., Author[]). In this design, once a Book instance is a constructor, you cannot add or remove the author. The toString() method shall return "Book[name=?,authors={Author[name=?,email=?,gender=?],......},price=?,qty=?]".  You should reuse Author’s toString(). Write the Book class (which uses the Author class has written earlier). Also, write a test driver called TestBook to test all the public methods in the class Book. Take Note that you have to construct an instance of Author before you can construct an instance of Book. E.g., // Construct an author instance Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); System.out.println(ahTeck); // Author's toString() Book dummyBook = new Book("Java for dummy", Author[] {ahTeck}, 19.95, 99); // Test Book's Constructor System.out.println(dummyBook); // Test Book's toString() // Test Getters and Setters dummyBook.setPrice(29.95); dummyBook.setQty(28); System.out.println("name is: " + dummyBook.getName()); System.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("Author is: " + dummyBook.getAuthors()); // Author's toString() System.out.println("Author's name is: " + dummyBook.getAuthors()[0].getName()); System.out.println("Author's email is: " + dummyBook.getAuthors()[0].getEmail()); // Or you can use an anonymous instance of Author to construct a Book instance Author[] authors = { new Author("Chyngyz", "aitmatov@gmail.kg", 'M'), new Author("Aleksei", "pushkin@yandex.ru", 'M') }; System.out.println(Arrays.toString(authors)); Book book = new Book("Nemo", new Author[] {new Author("Chyngyz", "aitmatov@gmail.kg", 'M'), new Author("Aleksei", "pushkin@yandex.ru", 'M')}, 15.0, 5); or Book book = new Book("Nemo", authors, 15.0, 5); System.out.println(book); // toString() 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() { return author.getName(); // cannot use author.name as name is private in Author class }     Required to: Write the code for the Book class. You shall re-use the Author class written earlier. Write a test driver (called TestBook) to test the Book class.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

This programming problem consists of two parts and shall lead you through all the concepts involved in OOP Composition.

Part 1:

A class called Author (as shown in the class diagram) is designed to model a book's author. 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=?,email=?,gender=?]", e.g., "Author[name=Tan Ah Teck,email=ahTeck@somewhere.com,gender=m]".

Write the Author class. Also, write a test driver (for testing purpose on your local machine) called TestAuthor to test all the public methods, e.g.,

Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); // Test the constructor

System.out.println(ahTeck); // Test toString() ahTeck.setEmail("paulTan@nowhere.com"); // Test setter System.out.println("name is: " + ahTeck.getName()); // Test getter System.out.println("eamil is: " + ahTeck.getEmail()); // Test getter System.out.println("gender is: " + ahTeck.getGender()); // Test gExerciseOOP_MyPolynomial.pngetter

Part 2:

 

A class called Book is designed (as shown in the class diagram) to model a book written by one or more authors. In reality, a book can be written by one or more authors. It contains:

  • Four private instance variables: name (String), array of authors (of the class Author you have just created), price (double), and qty (int);
  • Two constructors:public Book (String name, Author authors, double price) { ...... } public Book (String name, Author authors, double price, int qty) { ...... }
  • public methods getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty().
  • The constructors take an array of Author (i.e., Author[]). In this design, once a Book instance is a constructor, you cannot add or remove the author.
  • The toString() method shall return "Book[name=?,authors={Author[name=?,email=?,gender=?],......},price=?,qty=?]".  You should reuse Author’s toString().

Write the Book class (which uses the Author class has written earlier). Also, write a test driver called TestBook to test all the public methods in the class Book. Take Note that you have to construct an instance of Author before you can construct an instance of Book. E.g.,

// Construct an author instance

Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); System.out.println(ahTeck); // Author's toString() Book dummyBook = new Book("Java for dummy", Author[] {ahTeck}, 19.95, 99); // Test Book's Constructor System.out.println(dummyBook); // Test Book's toString() // Test Getters and Setters

dummyBook.setPrice(29.95);

dummyBook.setQty(28); System.out.println("name is: " + dummyBook.getName()); System.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("Author is: " + dummyBook.getAuthors()); // Author's toString() System.out.println("Author's name is: " + dummyBook.getAuthors()[0].getName());

System.out.println("Author's email is: " + dummyBook.getAuthors()[0].getEmail());

// Or you can use an anonymous instance of Author to construct a Book instance
Author[] authors = {
new Author("Chyngyz", "aitmatov@gmail.kg", 'M'),
new Author("Aleksei", "pushkin@yandex.ru", 'M')
};

System.out.println(Arrays.toString(authors));

Book book = new Book("Nemo", new Author[] {new Author("Chyngyz", "aitmatov@gmail.kg", 'M'), new Author("Aleksei", "pushkin@yandex.ru", 'M')}, 15.0, 5);

or

Book book = new Book("Nemo", authors, 15.0, 5);
System.out.println(book); // toString()

TRY:

  1. Printing the name and email of the author from a Book instance. (Hint: aBook.getAuthor().getName(), aBook.getAuthor().getEmail()).
  2. 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() { return author.getName(); // cannot use author.name as name is private in Author class }

 

 

Required to:

  1. Write the code for the Book class. You shall re-use the Author class written earlier.
  2. Write a test driver (called TestBook) to test the Book class.
Part-2
Book
Author
-name:String
-authors:Author[]
-price:double
-qty:int = 0
-name:String
-email:String
-gender:char
+Book (name:String, authors:Author[],
price:double)
+Book (name:String, authors:Author[],
price:double, qty:int)
+getName ():String
+getAuthors (): Author[]
+getPrice(): double
+setPrice(price:double):void
+getQty():int
+setQty(qty:int):void
+toString():String
+getAuthorNames():String •
"Book[name=?, authors={Author[name=?,
email=?,gender=?],......},price=?,
qty=?]"
"authorName1, authorName2"
Transcribed Image Text:Part-2 Book Author -name:String -authors:Author[] -price:double -qty:int = 0 -name:String -email:String -gender:char +Book (name:String, authors:Author[], price:double) +Book (name:String, authors:Author[], price:double, qty:int) +getName ():String +getAuthors (): Author[] +getPrice(): double +setPrice(price:double):void +getQty():int +setQty(qty:int):void +toString():String +getAuthorNames():String • "Book[name=?, authors={Author[name=?, email=?,gender=?],......},price=?, qty=?]" "authorName1, authorName2"
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 7 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY