A class called Book is designed as shown in the class diagram. It co.docx
1. A class called Book is designed as shown in the class diagram.
It contains: Four private instance variables: name (String),
author (of the class Author you have just created, assume that
each book has one and only one author), price (double), and
qtylnStock (int); Two constructors: -public Book (String name,
Author author, double price) {...} public Book (String name,
Author author, double price, int qtylnStock) {...} public
methods getName(), getAuthor(), getPrice(), setPrice(),
getQtylnStock(), setQtylnStock(). toString() that returns
"'book-name' by author-name (gender) at email". Take note
that the Author's toString() method returns "author-name
(gender) at email".) Write the Book class in the answer box
below assuming that the Author class has been done for you in
the system.
Solution
public class Author {
String name;
String email;
char gender;
public Author() {
// TODO Auto-generated constructor stub
}
/**
* @param name
2. * @param email
* @param gender
*/
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the email
*/
public String getEmail() {
3. return email;
}
/**
* @param email
* the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the gender
*/
public char getGender() {
return gender;
}
/**
* @param gender
* the gender to set
*/
public void setGender(char gender) {
this.gender = gender;
}
@Override
public String toString() {
// TODO Auto-generated method stub
4. return getName() + " (" + getGender() + ") at " +
getEmail();
}
}
public class Book {
private String name;
private Author author;
private double price;
private int qtyInStock = 0;
/**
* @param name
* @param author
* @param price
*/
public Book(String name, Author author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
/**
* @param name
* @param author
* @param price
* @param qtyInStock
*/
5. public Book(String name, Author author, double price, int
qtyInStock) {
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the author
*/
public Author getAuthor() {
return author;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
6. /**
* @param price
* the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the qtyInStock
*/
public int getQtyInStock() {
return qtyInStock;
}
/**
* @param qtyInStock
* the qtyInStock to set
*/
public void setQtyInStock(int qtyInStock) {
this.qtyInStock = qtyInStock;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
7. @Override
public String toString() {
return name + " by " + author.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
Author author = new Author( "Rajesh",
"Rajesh@mail.com", 'M');
Book book = new Book("Srinivas", author, 400.23);
System.out.println(book);
}
}
OUTPUT:
Srinivas by Rajesh (M) at Rajesh@mail.com