Answer to Question #241401 in Java | JSP | JSF for princesses

Question #241401

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


1
Expert's answer
2021-09-23T13:46:34-0400




abstract class Book {
	private String title;
	protected double price;


	public Book(String title) {
		this.title = title;
	}


	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}




	/**
	 * @return the price
	 */
	public double getPrice() {
		return price;
	}


	public abstract void setPrice();
		
}


class Fiction extends Book{


	public Fiction(String title) {
		super(title);
		
	}


	@Override
	public void setPrice() {
		price=24.99;
	}
}
class NonFiction extends Book{


	public NonFiction(String title) {
		super(title);
		
	}


	@Override
	public void setPrice() {
		price=37.99;
	}
}


public class Main {


	public static void main(String[] args) {
		Fiction Fiction=new Fiction("Fiction");
		Fiction.setPrice();
		NonFiction NonFiction=new NonFiction("NonFiction");
		NonFiction.setPrice();
		System.out.println("Title: "+Fiction.getTitle());
		System.out.println("Price: "+Fiction.getPrice());
		
		System.out.println("Title: "+NonFiction.getTitle());
		System.out.println("Price: "+NonFiction.getPrice());
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS