Question

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

Choose any entity of your choice to represent a superclass. It must not be any of the following: - Human, Student, Vehicle, Person, Car, Animal, Book

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

• 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. java language.

Homework Answers

Answer #1

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 sub_book(String sub_name)
{
System.out.println("This is "+sub_name+" subject book");
}
  
// 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 TestBook
{
public static void main (String[] args)
{
//Test Book's constructor
Book book1 = new Book("The Secret Seven","Enid Blyton",27.72);
  
//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);
  
}
}

I hope this answer is helpful to you, Please Upvote(Thums Up) my answer. Thank you.

Know the answer?
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
Similar Questions
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using the ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. PLEASE CODE THIS IN JAVA
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number...
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods: constructor add sub mul div toString You must also provide a Main class and main method to fully test your Rational number class.
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...