In: Computer Science
• 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.
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());
}
}