First, create an abstract class named Book. This class should have a String inst
ID: 3620877 • Letter: F
Question
First, create an abstract class named Book. This class should have a String instance variable for the book's title and a double instance variable for the book's price. Be sure to use appropriate access modifiers for these variables. Additionally, inside this class, include a constructor that receives the book's title. Also include two getter methods: one that returns the book's title and one that returns its price. Finally, include an abstract method named setPrice().Once you have your Book class written, it's time to write two subclasses: Fiction and NonFiction. Each of these classes will need to define its own constructor that accepts a String for the title and then calls on the Book constructor. Also, each class is required to define a setPrice() method. Have the Fiction class's setPrice() set the price field to $19.99 and have the NonFiction's setPrice() set the price field to $29.99.
Create a fourth class that creates a main method to test out your classes. Have the main method create both a Fiction and a NonFiction object. Be sure to call the setPrice method for each object. Finally, print out the price of each object to the console so that the user can see the different prices.
Explanation / Answer
Hi there, I am going to try to answer this as fast as I can for ya. public abstract class Book { protected String title; protected double price; public Book (String name) { this.title = name; } public getTitle() { return this.title; } public getPrice() { return this.price; } abstract public setPrice(double price); // end abstract class public class Fiction extends Book { public Fiction (String name) { super(name); } public setPrice(double price) { super.price = price; } // end fiction public class NonFiction extends Book { public NonFiction (String name) { super(name); } public setPrice(double price) { super.price = price; } // end non-fiction import java.until.Scanner; public class TestBook { public static void (String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the name of the book: "); String name = in.nextLine(); Book b1 = Fiction(name); Book b2 = NonFiction(name); b1.setPrice(19.99); b2.setPrice(29.99); System.out.println("Fiction Book title: " + b1.getTitle()); System.out.println("Fiction Book price: " + b1.getPrice()); System.out.println("NonFiction Book title: " + b2.getTitle()); System.out.println("NonFiction Book price: " + b2.getPrice()); } } I did not have a chance to test this so make sure that you check it over. I did this really fast. I hope it helps though :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.