JAVA PROGRAMMING: Design an abstract class called Book. Include a String field f
ID: 3919836 • Letter: J
Question
JAVA PROGRAMMING:
Design an abstract class called Book. Include a String field for the book’s title and a field for the book’s price. Include a default constructor and include two get methods for the title and the price. Add an abstract method and call it set_price(). Create two subclasses of Book called Literature and Science. Each must include a set_price() method that sets the price for all Science books to $50.15 and for all Literature books to $23.11. Provide a constructor for each subclass. Provide a program to demonstrate that you can create both a Science and a Literature Book and show their fields. The files should be named: Book.java, Sience.java, Literature.java and Testbook.java
Explanation / Answer
public abstract class Book { private String title; protected double price; public Book() { } public String getTitle() { return title; } public double getPrice() { return price; } public void setTitle(String title) { this.title = title; } public abstract void setPrice(); } public class Science extends Book { public Science() { setPrice(); } @Override public void setPrice() { price = 50.15; } } public class Literature extends Book { public Literature() { setPrice(); } @Override public void setPrice() { price = 23.11; } } public class Testbook { public static void main(String[] args) { Science science = new Science(); science.setTitle("science 1"); System.out.println("Science. Title: " + science.getTitle() + ", Price: $" + science.getPrice()); Literature literature = new Literature(); literature.setTitle("literature 1"); System.out.println("Literature. Title: " + literature.getTitle() + ", Price: $" + literature.getPrice()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.