Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

New to Java. Please Help. Create an abstract class named Book. Include a String field for the books title and a double field for the

New to Java. Please Help.

Create an abstract class named Book. Include a String field for the books title and a double field for the books price. Within the class, include a constructor that requires the book title, and add two get methodsone 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.

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.

//Book.java public abstract class Book { //instance variables private String bookTitle; protected double bookPrice; //parameterized constructor public Book(String title) { bookTitle=title; { bookPrice=0; } //getBookTitle method public String getBookTitle() { return bookTitle; } //getBookPrice method public double getBookPrice() { return bookPrice; } //abstract setBookPrice method public abstract void setBookPrice(); } }

//Ficition.java public class Fiction extends Book { //parameterized constructor public Fiction(String title) { //call to a super class constructor super(title); //call to setBookPrice method setBookPrice(); } //setBookPrice method implemention public void setBookPrice() { bookPrice=24.99; } }

//NonFiction.java public class NonFiction extends Book { //parameterized constructor public NonFiction(String title) { //call to super class constructor super(title); //call to setBookPrice method setBookPrice(); } //setBookPrice method public void setBookPrice() { bookPrice=37.00; } }

//UseBook.java public class UseBook { //start main method public static void main(String[]args) { //create object for Fiction class Fiction fic=new Fiction("The Case of the Family Jewels"); //display the details of Fcition class System.out.println("Fiction Book Details:"); System.out.println("Name:"+fic.getBookTitle()); System.out.println("Price:$"+fic.getBookPrice()); //create object for NonFiction class NonFiciton nonfic=new NonFiction("Freedom in Chains"); //display details of NonFiction class System.out.println("/nNon-Fiction Book Details:"); System.out.println("Name:"+nonFic.getBookTitle()); System.out.println("Price:$"+nonFic.getBookPrice()); } }

Book books[]=new Book[10]; //create and store 5 Fiction objects books[0]=new Ficiton("The First Fruits"); books[1]=new Fiction("Murder without Ease"); books[2]=new Fiction("Death Be My Deadline"); books[3]=new Fiction("The Sprint of the Place"); books[4]=new Fiction("Aforthought"); //create and store 5 NonFiction objects books[5]=new NonFiction("The Diary of a Young Girl"); books[6]=new NonFiction("Into the Wild"); books[7]=new NonFiction("The Double Helix"); books[8]=new NonFiction("Black Boy"); books[9]=new NonFiction("The True Believer"); //display the details of all books System.out.println("Details of all the books:"); for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago