Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can I get a little assistance with this one? Introduction to Abstract Classes Th

ID: 3712135 • Letter: C

Question

Can I get a little assistance with this one?

Introduction to Abstract Classes This exercise is designed to introduce you to the concept of inheritance using an abstract class You are to design three classes, a base class and two derived classes according to the specifications below I. Create an abstract class named Books Fields: title - a String field price- a float field Constructors: A default constructor A constructor with two arguments, title and price. Two getters, one for each field of the class toString0, an abstract method Methods II. Create a subclass named Fiction: Field numPages - an integer field Constructors: A constructor with two arguments, the title and the number of pages that sets the price to a default value of $27.95 A constructor with three arguments, the title, number of pages and the price. Getter methods for the class field toStringO- to display all the field values Create a subclass named NonFiction: Methods Field: topic a String field to describe the topic area of the book (e.g.: History Biography, Science, etc.) Constructors: A constructor with two arguments, the title and the topic that sets the price to a default value of $34.75 A constructor with three arguments, the title, the topic and the price. Getter methods for the class field toString0- to display all the field values Methods

Explanation / Answer

//Books.java
public abstract class Books
{
   //declare variabls
   private String title ;
   private float price;
   //Books constructor to set tile & price
   public Books(String title, float price)
   {
       this.title = title;
       this.price=price;
   }
   //Method to get title
   public String getTitle()
   {
       return title;
   }
   //Method to get price
   public float getPrice()
   {
       return price;
   }
   //abstract toString method
   public abstract String toString();
}//end of the class

--------------------------------------------------------------------------

//Fiction.java
public class Fiction extends Books
{
   private int numPages;
   private final static float DEFAULT_PRICE= 27.95f;
  
   //constructor that takes tile and number of pages
   //set price as defaut price
   public Fiction(String title, int numPages) {
       //calling super class Books constructor
       super(title, DEFAULT_PRICE);
       this.numPages=numPages;
   }
   //constructor to set title, number of pages and price
   public Fiction(String title, int numPages,float price) {
       //calling super class Books constructor
       super(title, price);
       this.numPages=numPages;  
   }
  
   //Returns the pages
   public int getpags(){
       return numPages;
   }
  
   //Override
   public String toString() {
       return String.format("Fiction Title: %s, price: %5.2f,# of pages %d",
               getTitle(),
               getPrice(),
               getpags());
   }
}//end of the class Fiction

--------------------------------------------------------------------------

//NonFiction.java
public class NonFiction extends Books{
   private String topic;
   private final static float DEFAULT_PRICE= 34.75f;
  
   //constructor that takes tile and topic
   public NonFiction(String title, String topic) {
       super(title, DEFAULT_PRICE);
       this.topic=topic;
   }

   /*Constructor that takes tile, topic and price*/
   public NonFiction(String title,
           String topic,float price) {
       super(title, price);
       this.topic=topic;  
   }
   //Return topic string
   public String gettopic(){
       return topic;
   }

   //Override
   public String toString() {
       return String.format("NonFiction Title: %s, price: %5.2f,Topic %s",
               getTitle(),
               getPrice(),
               gettopic());
   }
}

--------------------------------------------------------------------------

//TestBooks.java
public class TestBooks
{
   public static void main(String[] args)
   {
       //set size
       final int size=5;
       //create an array of Books of size=5
       Books someBook[] = new Books[size];
       //set Fiction and NonFiction class objects
       someBook[0] = new Fiction("Avatar",250);
       someBook[1] = new NonFiction("Introduction to Java","Programming");
       someBook[2] = new Fiction("EveryDayMiracles",250,450);
       someBook[3] = new NonFiction("The Road Not Taken","Eassay",85.0f);
       someBook[4] = new Fiction("Love and Hatee",250,42.5f);

       //print book object to console
       for (Books book : someBook) {
           System.out.println(book.toString());
       }

   }

}

----------------------------------------------------------------------------------------------------------

Sample output:

Fiction Title: Avatar, price: 27.95,# of pages 250
NonFiction Title: Introduction to Java, price: 34.75,Topic Programming
Fiction Title: EveryDayMiracles, price: 450.00,# of pages 250
NonFiction Title: The Road Not Taken, price: 85.00,Topic Eassay
Fiction Title: Love and Hatee, price: 42.50,# of pages 250

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote