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...

  • USING Java, create a program with the following: (I would like a different answer than the...

    USING Java, create a program with the following: (I would like a different answer than the answer on a similar question asked from someone else) 1-The first class is an abstract class called Weapon: a-Weapon has two different abstract methods: i-The first abstract method is void and is called attackType ii-The second abstract method is void and is called range b-Weapon has a constructor that accepts the weaponName c-Weapon also has a regular method that calculatesDamange and takes in attackType...

  • In the Book class you created in Exercise 4a, overload the Object class Equals() method to...

    In the Book class you created in Exercise 4a, overload the Object class Equals() method to consider two Books equal if they have the same ISBN. Create a program that declares three Books; two should have the same ISBN and one should have a different one. Demonstrate that the Equals() method works correctly to compare the Books. Save the program as BookDemo2. c. Write an application that declares two Book objects and uses an extension method named DisplayTitleAndAuthor() with each....

  • Create a base class named Book. Data fields include title and author; functions include those that...

    Create a base class named Book. Data fields include title and author; functions include those that can set and display the fields. Derive two classes from the Book class: Fiction, which also contains a numeric grade reading level, and NonFiction, which contains a variable to hold the number of pages. The functions that set and display data field values for the subclasses should call the appropriate parent class functions to set and display the common fields, and include specific code...

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

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