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.
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:
- 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.
Trending now
This is a popular solution!
Step by step
Solved in 7 steps with 2 images