java programming Question 2 (30 Points) Now right click on the NetBeans project
ID: 3756626 • Letter: J
Question
java programming
Question 2 (30 Points) Now right click on the NetBeans project for Question 1 and choose “Copy” to make a copy of it and save it as “Question2”.
1
Add a public class called Book with the following properties (Make sure to check the UML diagram).
• Four private instance variables: name (String), author (of the class Author you have just created), price (double), and qtyInStock (int). Assume that each book is written by one author. • One constructor which constructs an instance with the values given. • Getters and setters: getName(), getAuthor(), getPrice(), setPrice(), getQtyInStock(), setQtyInStock(). There are no setter for name and author. • Use override toString() method to print complete information about Book which includes name, price, quantity in stock as well as complete author information. i.e. when we call: aBook.toString(), it should print all that information.
Now in your main() method, create following objects as test cases and make sure to test all the public methods in the class Book. Note that you have to construct an instance of Author before you can construct an instance of Book OR you have to use an anonymous instance of Author. I have provided examples for both cases(Try both ways!). Test case:
Author sipser = new Author ("Michael Sipser", "msipser@mit.edu", ’m’); Book toc = new Book ("Theory of Computation", sipser, 200.00, 12);
OR
Book toc = new Book ("Theory of Computation", new Author ("Michael Sipser", "msipser@mit.edu", ’m’), 200.00, 12);
In your main method try:
1. Printing the book name, price and qtyInStock from a Book instance. (Hint: toc.getName()) 2. After obtaining the Author object, print the Author (name, email & gender) of the book.
Explanation / Answer
class Author
{
private String name;
private String email;
private char gender;
public Author(String name,String email,char gender)// constructor
{
this.name = name;
this.email = email;
this.gender = gender;
}
public String toString()
{
return " Author : name : "+name +" email : "+email+" gender : "+gender;
}
}
class Book
{
private String name;
private Author author;
private double price;
private int qtyInStock;
public Book(String name,Author author,double price,int qtyInStock)// constructor
{
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
//get and set methods
public String getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getQtyInStock()
{
return qtyInStock;
}
public void setQtyInStock(int qtyInStock)
{
this.qtyInStock = qtyInStock;
}
public String toString()
{
return "Book : Name : "+name +" Price : $"+price+ "Quantity in stock : "+qtyInStock+ author.toString();
}
}
class TestBook
{
public static void main (String[] args)
{
Author sipser = new Author ("Michael Sipser", "msipser@mit.edu", 'm');
Book toc = new Book ("Theory of Computation", sipser, 200.00, 12);
System.out.println("Book Name : "+toc.getName()+" Price : $"+toc.getPrice()+" Quantity in stock : "+toc.getQtyInStock());
System.out.println("Author information : "+toc.getAuthor());
Book toc1 = new Book ("Theory of Computation", new Author ("Michael Sipser", "msipser@mit.edu", 'm'), 200.00, 12);
System.out.println("Book Name : "+toc1.getName()+" Price : $"+toc1.getPrice()+" Quantity in stock : "+toc1.getQtyInStock());
System.out.println("Author information : "+toc1.getAuthor());
}
}
Output:
Book Name : Theory of Computation Price : $200.0 Quantity in stock : 12
Author information : Author : name : Michael Sipser email : msipser@mit.edu gender : m
Book Name : Theory of Computation Price : $200.0 Quantity in stock : 12
Author information : Author : name : Michael Sipser email : msipser@mit.edu gender : m
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.