Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1, Write an Author Java class as defined below: No default values for the variables char of 'm' or 'F' Author -name: String -email:String -gender:char

image text in transcribedimage text in transcribedimage text in transcribed

1, Write an Author Java class as defined below: No default values for the variables char of 'm' or 'F' Author -name: String -email:String -gender:char +Author(name: String, email:String, gender:char) +getName():String +getEmail():String +setEmail(email:String):void +getGender():char +toString():String "Author[name=?, email=?, gender=?]" The Author class (as shown in the class diagram) is designed to model a book's author. It contains: a. Three private instance: variables: name (String), email (String), and gender (char of either 'm' or 'f); b. 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.) c. public getters/setters: getName(), getEmail(), setEmail(), and getGender(); (There are no setters for name and gender, as these attributes cannot be changed.) d. A toString() method that returns "Author[name=?,email=?,gender=?]", e.g., "Author[name=Tan Ah Teck, email=ahTeck@somewhere.com, gender=m)". 2. Write a Book Java class which uses the Author class written earlier as defined below: Book 1 has Author -name: String -email:String -gender:char -name: String -author:Author -price: double -qty:int = 0 +Book(name: String, author:Author, price: double) +Book(name:String, author:Author, price:double, qty:int) +getName(): String +getAuthor(): Author +getPrice(): double +se ce(p ple):void +getQty(): int +setQty(qty:int):void +toString():String.. "Book [name=?, Author[name=?, email=?, gender=?],price=?,qty=?]" You need to reuse Author's toString(). A class called Book is designed as shown in the class diagram) to model a book written by one author. It contains: a. Four private instance variables: name (String), author of the class Author you have just created, assume that a book has one and only one author), price (double), and qty (int); b. Two constructors: public Book (String name, Author author, double price) { ...... } public Book (String name, Author author, double price, int qty) { ...... } c. public methods getName(), getAuthor(), getPrice(), setPrice(), getQty0, setQty(). d. A toString() that returns: "Book[name=?,Author[name=?,email=?,gender=?],price=?,qty=?". Author's toString(). You should reuse 3. 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", 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.getAuthor()); // Author's toString( System.out.println("Author's name is: " + dummyBook.getAuthor().getName()); System.out.println("Author's email is: " + dummyBook.getAuthor().getEmail(); // Use an anonymous instance of Author to construct a Book instance Book another Book = new Book("more Java", new Author("Paul Tan", "paul@somewhere.com", 'm'), 29.95); System.out.println(anotherBook); // toString( Take note that both Book and Author classes have a variable called name. However, it can be differentiated via the referencing instance. For a Book instance says aBook, aBook.name refers to the name of the book; whereas for an Author's instance say auAuthor, anAuthor.name refers to the name of the author. There is no need and not recommended) to call the variables bookName and authorName. 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. 3. For example, public String getAuthorName() { return author.getName(); // cannot use author.name as name is private in Author class }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions