Question

In: Computer Science

JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...

JAVA Programming ECLIPSE IDE

1.

Create an abstract class called Book. The class should declare the following variables:
an instance variable that describes the title - String
an instance variable that describes the ISBN - String
an instance variable that describes the publisher - String
an instance variable that describes the price - double
an instance variable that describes the year – integer

Provide a toString() method that returns the information stored in the above variables.

Create the getter and setter methods for each instance variable except price. Provide the necessary constructors. Include an abstract method setPrice(double price) to determine the price for a book. Include an abstract method getGenre() to return the genre of the book.

Create two subclasses called ScienceBook and ChildrenBook.


These subclasses should override the abstract methods setPrice and getGenre of class Book.

Use the following rule for setting the price for a book:
science books will have a 10% discount per each book
children books will have a fixed price (specified by user).

Write a driver program (another class with main method) that uses the above hierarchy. In your driver program you must implement an interaction with the user.
Use showInputDialog method to let the user input book information.
Use showMessageDialog method to display book information including price and type for both science and children books.

Solutions

Expert Solution

Book.java :

//Java class
public abstract class Book {
   //an instance variable that describes the title - String
   private String title;
   //an instance variable that describes the ISBN - String
   private String ISBN;
   //an instance variable that describes the publisher - String
   private String publisher;
   //an instance variable that describes the price - double
   private double price;
   //an instance variable that describes the year – integer
   private int year;
   //default constructor
   public Book()
   {
       this.title="";
       this.ISBN="";
       this.publisher="";
       this.price=0;
       this.year=0;
   }
   //getter and setter methods
   public String getTitle()
   {
       return this.title;
   }
   public void setTitle(String title)
   {
       this.title=title;
   }
   public String getISBN()
   {
       return this.ISBN;
   }
   public void setISBN(String isbn)
   {
       this.ISBN=isbn;
   }
   public String getPublisher()
   {
       return this.publisher;
   }
   public void setPublisher(String pub)
   {
       this.publisher=pub;
   }
   public int getYear()
   {
       return this.year;
   }
   public void setYear(int year)
   {
       this.year=year;
   }
   // an abstract method setPrice(double price) to determine the price for a book.
   public abstract void setPrice(double price);
   //abstract method getGenre() to return the genre of the book.
   public abstract String getGenre();
  
   // toString() method
   public String toString()
   {
       return "Title : "+this.title+"\nISBN : "+this.ISBN+"\nPublisher : "+this.publisher+"\nYear : "+this.year;
   }

}
*************************************************

ScienceBook.java :

//Java class with name ScienceBook that extends Book class
public class ScienceBook extends Book {
   private double price;
   @Override
   public void setPrice(double price) {
       //give 10% discount for science book
       this.price=price-price*0.10;      
   }

   @Override
   public String getGenre() {
       return "Science Book";
   }
   public double getPrice()
   {
       return this.price;
   }

}
*****************************************

ChildrenBook.java :

//Java class with name ChildrenBook that extends Book class
public class ChildrenBook extends Book {
   private double price;
   @Override
   public void setPrice(double price) {
       this.price=price;
      
   }

   @Override
   public String getGenre() {
       return "Children Book";
   }
   public double getPrice()
   {
       return this.price;
   }

}
*******************************************

BookDriver.java :

//import package
import javax.swing.*;

//Java class
public class BookDriver {
   // main() method
   public static void main(String[] args) {
       // Object of ScienceBook class
       ScienceBook sb = new ScienceBook();
       // asking user book title and set book title
       sb.setTitle(JOptionPane.showInputDialog(null, "Enter Book title : "));
       // asking user book ISBN and set book ISBN
       sb.setISBN(JOptionPane.showInputDialog(null, "Enter Book ISBN : "));
       // asking user book publisher and set book publisher
       sb.setPublisher(JOptionPane.showInputDialog(null, "Enter Book publisher : "));
       // asking user book year and set book year
       sb.setYear(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Book year : ")));
       // asking user book price and set book price
       sb.setPrice(Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Book Price : ")));
       // print ScienceBook details
       JOptionPane.showMessageDialog(null, sb.toString() + "\nType : " + sb.getGenre() + "\n Price : " + sb.getPrice(),
               "Book Details", JOptionPane.INFORMATION_MESSAGE);
       // Object of ChildrenBook class
       ChildrenBook cb = new ChildrenBook();
       // asking user book title and set book title
       cb.setTitle(JOptionPane.showInputDialog(null, "Enter Book title : "));
       // asking user book ISBN and set book ISBN
       cb.setISBN(JOptionPane.showInputDialog(null, "Enter Book ISBN : "));
       // asking user book publisher and set book publisher
       cb.setPublisher(JOptionPane.showInputDialog(null, "Enter Book publisher : "));
       // asking user book year and set book year
       cb.setYear(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Book year : ")));
       // asking user book price and set book price
       cb.setPrice(Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Book Price : ")));
       // print ScienceBook details
       JOptionPane.showMessageDialog(null, sb.toString() + "\nType : " + sb.getGenre() + "\n Price : " + sb.getPrice(),
               "Book Details", JOptionPane.INFORMATION_MESSAGE);
   }

}
===============================================

Screen asking book title :

Screen showing book details :


Related Solutions

JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Task #1 The if Statement, Comparing Strings, and Flags (JAVA using Eclipse IDE 14) Create the...
Task #1 The if Statement, Comparing Strings, and Flags (JAVA using Eclipse IDE 14) Create the file PizzaOrder.java. Prompt the user to input the type of pizza that they want to order. In the end, you should print out the final order and price. Here is a sample output. Welcome to May and Adam’s Pizzeria Enter your first name: Amy Pizza Size(inches)     Cost         10            $10.99         12            $12.99         14            $14.99         16            $16.99 What size pizza would you...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT