Question

In Java

Create an interface called Amount that includes a method called setPricel) Create an abstract class named Book that inherits

Create an interface called

Amount

that includes a method called

setPrice

().

Create an abstract class named

Book

that inherits from the interface Amount. 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 getter methods

— one that returns the title and one that returns the

price. Include an abstract method named

setPrice

().

Create a child class of Book:

NonFiction

. Must include a

setPrice

() method that sets the price for all

NonFiction Books to $37.99. Should also include a

constructor

.

Create a child class of Book:

Fiction

(Make the class

final

). Must include a

setPrice

() method that sets the

price for all Fiction Books to $ 24.99. Should also include a

constructor

. Also include a

ratings

field that

can be assigned to either G, PG, R, NR.

Create another child class that inherits from NonFiction called

Adults

. Include a

setPrice

() method that

sets the price to $50.00. Also include a field called

level

which can be assigned to painful, dull,

interesting and exciting. Also include a

constructor

Create a

Driver

class that creates an array of objects for all the classes (Only the ones that are possible)

and display their fields.

Thank you :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Amount.java

/** interface called Amount that includes a method called
* setPrice()
*/

public interface Amount
{
void setPrice();
}
========================================================================================

Book.java

/** an abstract class named Book that inherits from the interface Amount.
* Include a String field for
* the book’s title and a double field for the book’s Price
*/

public abstract class Book implements Amount
{
/** Member variables */
String bookTitle;
double bookPrice;

/** Constructor that requires the book title */
public Book(String title)
{
bookTitle = title;
}
  
/** getter methods */
public String getTitle()
{
return bookTitle;
}
  
public double getPrice()
{
return bookPrice;
}
  
/** abstract method named setPrice() */
public abstract void setPrice();
}

NonFiction.java

/** NonFiction is the subclass that inherits Book */
public class NonFiction extends Book
{
public NonFiction(String title)
{
/** calling the superclass constructor */
super(title);
}
  
public void setPrice()
{
bookPrice = 37.99;
}
}

=====================================================================================

Fiction.java

/** Fiction is a final class which inherits from Book */
public final class Fiction extends Book
{
/** Member variable */
String ratings;
  
public Fiction(String title, String r)
{
/** calling super class constructor */
super(title);
ratings = r;
}
  
/** override setPrice method */
public void setPrice()
{
bookPrice = 24.99;
}
}

===================================================================

Adults.java


/** Adults is a subclass of NonFiction */
public class Adults extends NonFiction
{
/** Member variable */
String level;
  
/** Constructor */
public Adults(String title,String l)
{
super(title);
level = l;
}
  
/** Overridden setPrice() method */
public void setPrice()
{
bookPrice = 50.00;
}
}

==================================================================================

Driver.java

public class Driver
{
public static void main(String[] s)
{
/** Cannot create object for an interface Amount */
/** Cannot create object for an abstract class */
  
Object object[] = new Object[10];
  
NonFiction nf = new NonFiction("Title1");
nf.setPrice();
System.out.println("Book Title :: " + nf.getTitle());
System.out.println("Book Price :: $" + nf.getPrice());
System.out.println();
object[0] = nf;
  
Fiction f = new Fiction("Title2","PG");
f.setPrice();
System.out.println("Book Title :: " + f.getTitle());
System.out.println("Ratings :: " + f.ratings);
System.out.println("Book Price :: $" + f.getPrice());
System.out.println();
object[1] = f;
  
Adults a = new Adults("Title3","dull");
a.setPrice();
System.out.println("Book Title :: " + a.getTitle());
System.out.println("Level :: " + a.level);
System.out.println("Book Price :: $" + a.getPrice());
System.out.println();
object[2] = a;
}
  
}

Sample output:

Add a comment
Know the answer?
Add Answer to:
In Java Create an interface called Amount that includes a method called setPrice (). Create an a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java • Create an interface called Person. In this interface create a method called printData()...

    in java • Create an interface called Person. In this interface create a method called printData() • Implement interface Person by classes Student, Teacher, Admin. You need to think the relevant data attributes for each class. Define atleast 4 attributes for each class. • In each class override a method toString() and create a string which holds all data • In the method printData in each class print the data using toString() method. Design the UML and write a code...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....

    Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp. Also uses Super in the TextBook Class section. Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attributes, title and price, and get/set methods for the two attributes. - price: double Вook) Book(String, double) getTitle(): String setTitle(String): void + getPrice() double setPrice(double): void toString() String + The first constructor doesn't...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double...

    Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....

  • Write an example method that overrides the + operator to create a new book whose title...

    Write an example method that overrides the + operator to create a new book whose title is a concatenation of the titles of two books. For example, if the first book's title is "The Adventures of Tom Sawyer" and the second book's title is "The Adventures of Huckleberry Finn", the concatenated title will be "The Adventures of Tom Sawyer and The Adventures of Huckleberry Finn". Assume the book class is defined as: class Book { public Book(string title) { Title...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT