Create an abstract class named Book Include a String field f
Create an abstract class named Book. Include a String field for the book’s title and a double field for the book’s price. Within the class, include a constructor that requires the book title, and add two get methods—one that returns the title and one that returns the price. Include an abstract method named setPrice(). Create two child classes of Book: Fiction and NonFiction. Each must include a setPrice() method that sets the price for all Fiction Books to $24.99 and for all NonFiction Books to $37.99. Write a constructor for each subclass, and include a call to setPrice() within each. Write an application demonstrating that you can create both a Fiction and a NonFiction Book and display their fields. Save the files as Book.java, Fiction.java, NonFiction.java, and UseBook.java. Also, write an application named BookArray in which you create an array that holds 10 Books, some Fiction and some NonFiction. Using a for loop, display details about all 10 books. Save the file as BookArray.java.
Solution
Book.java
public abstract class Book {
//Declaring variables
private String title;
protected double price;
//Parameterized constructor
public Book(String title) {
super();
this.title = title;
}
//Getter methods
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
//Abstract method
public abstract void setPrice();
}
________________________________________
Fiction.java
public class Fiction extends Book {
//Creating Parameterized constructor
public Fiction(String title) {
super(title);
setPrice();
}
/* Method implementation for setPrice() method
* of super class Abstract method
*/
@Override
public void setPrice() {
price=24.99;
}
//toString() method which displays the contents of Fiction Class Object
@Override
public String toString() {
System.out.println(\"_____Fiction Book_____\");
return \"Title :\"+getTitle()+\"\ Price :$\"+getPrice()+\"\ \";
}
}
______________________________________
NonFiction.java
import org.students.Book;
public class NonFiction extends Book {
//Parameterized constructor
public NonFiction(String title) {
super(title);
setPrice();
}
/* Method implementation for setPrice() method
* of super class Abstract method
*/
@Override
public void setPrice() {
price=37.99;
}
//toString() method which displays the contents of Fiction Class Object
@Override
public String toString() {
System.out.println(\"_____Non-Fiction Book_____\");
return \"Title :\"+getTitle()+\"\ Price :$\"+getPrice()+\"\ \";
}
}
_________________________________________
UseBook.java (This class contains main()method)
public class UseBook {
public static void main(String[] args) {
//Creating and Object to Fiction Class
Fiction f=new Fiction(\"Outlnader\");
//calling the toString() method on the Fiction class Object
System.out.println(f.toString());
//Creating and Object to Non-Fiction Class
NonFiction nf=new NonFiction(\"Silent Spring\");
//calling the toString() method on the Non-Fiction class Object
System.out.println(nf.toString());
}
}
_______________________________
Output:
_____Fiction Book_____
Title :Outlnader
Price :$24.99
_____Non-Fiction Book_____
Title :Silent Spring
Price :$37.99
_________________________________
BookArray.java (This class Contains main() method)
public class BookArray {
public static void main(String[] args) {
//Creating Book array which stores Fiction and Non-Fiction Book Objects
Book bookArray[]={new Fiction(\"Dune\"),
new Fiction(\"Ender\'s Game\"),
new Fiction(\"Fahrenheit 51\"),
new Fiction(\"Brave New World\"),
new Fiction(\"A Game of Thornes\"),
new NonFiction(\"Homage to Catlonia\"),
new NonFiction(\"A Room of One\'s Own\"),
new NonFiction(\"Black Boy\"),
new NonFiction(\"Fast Food Nation\"),
new NonFiction(\"Meditations\")};
//This for loop will displays the Each Fiction and Non-Fiction book details
for(int i=0;i<bookArray.length;i++)
{
//Displaying the information of each book
System.out.println(bookArray[i].toString());
}
}
}
__________________________________
Output2:
_____Fiction Book_____
Title :Dune
Price :$24.99
_____Fiction Book_____
Title :Ender\'s Game
Price :$24.99
_____Fiction Book_____
Title :Fahrenheit 51
Price :$24.99
_____Fiction Book_____
Title :Brave New World
Price :$24.99
_____Fiction Book_____
Title :A Game of Thornes
Price :$24.99
_____Non-Fiction Book_____
Title :Homage to Catlonia
Price :$37.99
_____Non-Fiction Book_____
Title :A Room of One\'s Own
Price :$37.99
_____Non-Fiction Book_____
Title :Black Boy
Price :$37.99
_____Non-Fiction Book_____
Title :Fast Food Nation
Price :$37.99
_____Non-Fiction Book_____
Title :Meditations
Price :$37.99
_______________________Thank You



