Question

In: Computer Science

• Choose any entity of your choice to represent a superclass. It must not be any...

• Choose any entity of your choice to represent a superclass. It must not be any of the following:

- Human, Student, Vehicle, Person, Car, Animal

• Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the details for the class objects with captions for the data.

java language

• Create 2 subclasses for the superclass

• Each subclass must have the following: - 2 instance variables - A constructor that first initializes its object variables using the superclass constructor, then initializes the rest. - Getters and setters - A method that overrides the print method in the superclass

• Create a test class and test the print methods in all the classes.

Solutions

Expert Solution

Question:

• Choose any entity of your choice to represent a superclass. It must not be any of the following:

- Human, Student, Vehicle, Person, Car, Animal

• Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the details for the class objects with captions for the data.

java language

• Create 2 subclasses for the superclass

• Each subclass must have the following: - 2 instance variables - A constructor that first initializes its object variables using the superclass constructor, then initializes the rest. - Getters and setters - A method that overrides the print method in the superclass

• Create a test class and test the print methods in all the classes.

Answer:

import java.io.*;
import java.lang.*;

class Book
{
   //instance variables
   String name;
   String author_name;
   double price;
  
   //default constructor
   Book()
   {
       System.out.println("Default constructor called");
   }
  
   //parameterized constructor to initialize all instance variables
   public Book(String name, String author_name, double price)
   {
       this.name=name;
       this.author_name=author_name;
       this.price=price;
   }
  
   //instance methods
   public void story_book(String genre)   
   {
       System.out.println("This is "+genre+" type story book");
   }
  
   public void story_book(String genre, int upper_age_limit) //method overloading
   {
       System.out.println("This is "+genre+" type story book for upto "+upper_age_limit+" years");
   }
  
   public void print_book_details()
   {
       System.out.println("Name of the book: "+name);
       System.out.println("Author of the book: "+author_name);
       System.out.println("Price of the book: "+price+" USD");
   }
  
   // Getters and Setters
   // Returns the name of this book
   public String getName() {
       return name;
   }

   // Returns the name of the author of this book
   public String getAuthor() {
       return author_name;
   }
  
   // Returns the price of this book
   public double getPrice() {
       return price;
   }
  
   // Sets the price of this book
   public void setPrice(double price) {
       this.price = price;
   }
  
}

class Fiction_book extends Book
{
   //instance variables
   int discount_rate;
   String language;
  
   //parameterized constructor
   Fiction_book(String name, String author_name, double price, int discount_rate, String language)
   {
       super(name,author_name,price); //initailizing the superclass objects
       this.discount_rate=discount_rate;
       this.language=language;
   }
  
   //Getters and setters
   // Returns the language of this book
   public String getLanguage() {
       return language;
   }
  
   // Returns the discount rate of this book
   public int getDiscountRate() {
       return discount_rate;
   }
  
   // Sets the discount rate of this book
   public void setDiscountRate(int discount_rate) {
       this.discount_rate = discount_rate;
   }

   //Overriding the parent method
   @Override
   public void print_book_details()
   {
       System.out.println("Name of the book: "+name);
       System.out.println("Author of the book: "+author_name);
       System.out.println("Price of the book: "+price+" USD");
       System.out.println("Discount rate of the book: "+discount_rate);
       System.out.println("Language of the book: "+language);
   }
  
}

class Horror_book extends Book
{
   //instance variables
   int no_of_pages;
   int no_of_chapters;
  
   //parameterized constructor
   Horror_book(String name, String author_name, double price, int no_of_pages, int no_of_chapters)
   {
       super(name,author_name,price); //initailizing the superclass objects
       this.no_of_pages=no_of_pages;
       this.no_of_chapters=no_of_chapters;
   }
  
   //Getters and setters
   // Returns the language of this book
   public int getNoOfPages() {
       return no_of_pages;
   }
  
   // Returns the discount rate of this book
   public int getNoOfChapters() {
       return no_of_chapters;
   }
  
   // Sets the discount rate of this book
   public void setNoOfPages(int no_of_pages) {
       this.no_of_pages = no_of_pages;
   }

   //Overriding the parent method
   @Override
   public void print_book_details()
   {
       System.out.println("Name of the book: "+name);
       System.out.println("Author of the book: "+author_name);
       System.out.println("Price of the book: "+price+" USD");
       System.out.println("No. of pages in the book: "+no_of_pages);
       System.out.println("No. of chapters of the book: "+no_of_chapters);
   }
  
}

// Test class to test all the methods in all classes
class TestBook
{
   public static void main (String[] args)
   {
       //Test Book's constructor
       Book book1 = new Book("The Secret Seven","Enid Blyton",27.72);
      
       //For printing all details of the book
       book1.print_book_details();
      
       //Test setters and getters
       System.out.println("Book's name is: "+book1.getName());
       System.out.println("Book author's name is: "+book1.getAuthor());
       System.out.println("Book's price is: "+book1.getPrice()+" USD");
       book1.setPrice(25.42);
       System.out.println("Now, the book's price is: "+book1.getPrice()+" USD");
      
       //Test method overloading of instance methods
       book1.story_book("Children detective");
       book1.story_book("Children detective",18);
      
       System.out.println("---------------------------------------------------\n");
      
       //Test Fiction_book's constructor
       Fiction_book book2 = new Fiction_book("1984","George Orwell",25.84,10,"English");
  
       //For printing all details of the book (testing overriding of parent method)
       book2.print_book_details();
  
       //Test setters and getters
       System.out.println("Book's discount rate is: "+book2.getDiscountRate());
       System.out.println("Book's language is: "+book2.getLanguage());
       book2.setDiscountRate(15);
       System.out.println("Now, the book's discount rate is: "+book2.getDiscountRate());
      
       System.out.println("---------------------------------------------------\n");

       //Test Horror_book's constructor
       Horror_book book3 = new Horror_book("The Haunting of the Hill House","Shirley Jackson",14.50,700,15);
      
       //For printing all details of the book (testing overriding of parent method)
       book3.print_book_details();
      
       //Test setters and getters
       System.out.println("No. of pages in the book is: "+book3.getNoOfPages());
       System.out.println("No. of chapters in the book is: "+book3.getNoOfChapters());
       book3.setNoOfPages(600);
       System.out.println("Now, the no. of pages in the book is: "+book3.getNoOfPages());

   }

}

      


Related Solutions

choose any organisation of your choice be it service or manufacturing related, investigate the following; 1,...
choose any organisation of your choice be it service or manufacturing related, investigate the following; 1, identify clearly and state the organisation's product and service and the customers for those product and or services
Q1. Choose any ONE topic of your choice from the following and write your opinion either...
Q1. Choose any ONE topic of your choice from the following and write your opinion either PRO or CON about it in your own words word limit 100 WORDS. • Write your chosen question on top and mention pro or con. 1.There should be only girls and only boys school. PRO/CON 2.Is reading fiction a waste of time? Explain your answer using specific reasons and examples to support your position. PRO/CON 3.“Better a lie that soothes than a truth that...
Q1. Choose any ONE topic of your choice from the following and write your opinion either...
Q1. Choose any ONE topic of your choice from the following and write your opinion either PRO or CON about it in your own words word limit 100 WORDS. • Write your chosen question on top and mention pro or con. 1.There should be only girls and only boys school. PRO/CON 2.Is reading fiction a waste of time? Explain your answer using specific reasons and examples to support your position. PRO/CON 3.“Better a lie that soothes than a truth that...
Choose a topic to write a research paper on. Your topic must be related to any...
Choose a topic to write a research paper on. Your topic must be related to any aspect of childhood development. Some example topics include (but are not limited to): The social, emotional, and cognitive aspects development and growth in _______________ age group The reproductive system The advantages or disadvantages of Preschool The role of genetics in conception Sexual Behavior in adolescence and emerging adults Step 2: Write your research paper. Include: Your research paper should be 4-5 pages in length...
Choose any stock (your choice) from one of the US Stock exchanges and plot the monthly...
Choose any stock (your choice) from one of the US Stock exchanges and plot the monthly (beginning) prices of this stock for all of 2019, through Oct 1 of 2020. You should 22 data points. Briefly explain whether or not this stock would add credence to, or not, for the Efficient Markets Hypothesis (EMH). This is not a quantitative question, per se, but provide appropriate rationale to support your answer.
Choose any product of your choice (after from water products) and develop a marketing strategy for...
Choose any product of your choice (after from water products) and develop a marketing strategy for it.
Choose any interval-ratio variable of your choice from SPSS. Be sure to verify with a frequency...
Choose any interval-ratio variable of your choice from SPSS. Be sure to verify with a frequency distribution or figure that the variable is in fact interval-ratio. Run the appropriate measures of central tendency and dispersion for this variable and in a few sentences discuss what all of these measures tell us about the variable.
Choose any one organization of your choice from Bahrain , Prepare the recruitment process and find...
Choose any one organization of your choice from Bahrain , Prepare the recruitment process and find the problem? Analyze if you are the head HR which areas and points you will consider to choose the best intern and how to avoid the reference /scandal?
You must choose a diversity event or film, of your choice, ,to attend/view. After which, you...
You must choose a diversity event or film, of your choice, ,to attend/view. After which, you are to write a reaction paper of between 1-2, double-spaced, 12 pt font, pages discussing the event/video, what you learned and how it applies to Diversity.
Please assist...I choose AFLAC Select a company of your choice, any company but Southwest Airlines, and...
Please assist...I choose AFLAC Select a company of your choice, any company but Southwest Airlines, and write a six to eight (6-8) page paper in which you: 1.Evaluate a company’s recent (with in the last year) actions dealing with risk and uncertainty. 2.Offer advice for improving risk management. 3.Examine an adverse selection problem your company is facing and recommend how it should minimize its negative impact on transactions. 4.Determine the ways your company is dealing with the moral hazard problem...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT