Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Object-Oriented JAVA Programming 1. Write the class definition Book (Book.java)a

ID: 3762866 • Letter: O

Question

Object-Oriented JAVA Programming


1. Write the class definition Book (Book.java)as follows:

A book has the title, price and quantity as data members (private data members). It has two overloaded constructors, getters and setters for private data members, toString method, and computeTotalValue method which calculates and returns the total value (price * quantity).

2. Write a client program to test the Book class (you can hardcode it).

a. Create a Book object and initialize the data members appropriately.
b. Call method computeTotalValue and output the result.
c. Change the book object’s quantity data member.
d. Call method computeTotalValue again and output the result.

Explanation / Answer

Book.java


public class Book {

   private String title;
   private double price;
   private int quantity;

   public double computeTotalValue() {

       return (price * quantity);
   }

   /**
   * @return the title
   */
   public String getTitle() {
       return title;
   }

   /**
   * @return the price
   */
   public double getPrice() {
       return price;
   }

   /**
   * @return the quantity
   */
   public int getQuantity() {
       return quantity;
   }

   /**
   * @param title
   * the title to set
   */
   public void setTitle(String title) {
       this.title = title;
   }

   /**
   * @param price
   * the price to set
   */
   public void setPrice(double price) {
       this.price = price;
   }

   /**
   * @param quantity
   * the quantity to set
   */
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Book [title=" + title + ", price=" + price + ", quantity="
               + quantity + "]";
   }

   public Book(String title, double price, int quantity) {
       super();
       this.title = title;
       this.price = price;
       this.quantity = quantity;
   }

   public Book() {

   }

}

BookTest.java


/**
* @author srinivas
*
*/
public class BookTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Book book = new Book("BookName", 2563.36, 5);
       System.out.println(book);
       double totalVal = book.computeTotalValue();

       System.out.println("computeTotalValue=" + totalVal);
       book.setQuantity(10);
       System.out.println("After quantity changed:");
       System.out.println(book);
       totalVal = book.computeTotalValue();
       System.out.println("computeTotalValue=" + totalVal);
   }

}

OUTPUT:

Book [title=BookName, price=2563.36, quantity=5]
computeTotalValue=12816.800000000001
After quantity changed:
Book [title=BookName, price=2563.36, quantity=10]
computeTotalValue=25633.600000000002