In: Computer Science
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.
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 :