1. Create a subclass of the class you created in Assignment 2. (10 point) 2. Cre
ID: 3588706 • Letter: 1
Question
1. Create a subclass of the class you created in Assignment 2. (10 point)
2. Create at least 1 instance field (attribute) for your subclass (10 point)
3. Create getter (accessor) and setter (mutator) methods for the new instance field (10 point)
4. Create a Method to display your data (15 point)
Create a method called display, that simply prints out the values of all instance variables of your object
It must display all the fields from the super class, along with the new field that belongs to the subclass
This method will have no parameters and not return a value
5. Create 2 Constructors
Create a default constructor (no parameters) that assigns all your instance variables to default values. It should also set the fields for the super class as well. (10 point)
Create a parameterized constructor that takes all instance variables (from both superclass and subclass) as parameters, and sets the instance variables to the values provided by the parameters (15 point)
6. Testing your program. In the main method of Demo.java:
Create an instance of your new subclass (an object) by using the default constructor.
Call all your objects setter (mutator) methods to assign values to your object (5 point)
Call the objects display method, to print out it's values (5 point)
Create another instance of your class (another object) by using the parameterized constructor (10 point)
Call the objects display method, to print out it's values
7. Write Javadoc style comments for ALL of your methods. Including the methods in your super class. (10 point)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Assignment 2 codes a.k.a superclass code
Shoes.java
public class Shoes {
// Declaring instance variables
private int size;
private String brand;
private double price;
// Zero argumented constructor
public Shoes() {
this.size = 7;
this.brand = "Red Tape";
this.price = 50;
}
// Parameterized constructor
public Shoes(int size, String brand, double price) {
super();
this.size = size;
this.brand = brand;
this.price = price;
}
// getters and setters
/*
* This method will get the price of instance variable
*
* @params void
*
* @return double
*/
public int getSize() {
return size;
}
/*
* This method will set the size of instance variable
*
* @params int
*
* @return void
*/
public void setSize(int size) {
this.size = size;
}
/*
* This method will get the brand name of instance variable
*
* @params void
*
* @return String
*/
public String getBrand() {
return brand;
}
/*
* This method will set the brand of instance variable
*
* @params String
*
* @return void
*/
public void setBrand(String brand) {
this.brand = brand;
}
/*
* This method will return the price of instance variable
*
* @params void
*
* @return double
*/
public double getPrice() {
return price;
}
/*
* This method will set the price of instance variable
*
* @params double
*
* @return void
*/
public void setPrice(double price) {
this.price = price;
}
/*
* This method will display the values of instance variables
*
* @params void
*
* @return void
*/
void display() {
System.out.println("Size :" + size);
System.out.println("Brand :" + brand);
System.out.println("Price :$" + price);
}
}
_______________________________________
Demo.java
public class Demo {
public static void main(String[] args) {
//Creating an instance of Shoes class object using Default constructor
Shoes s1 = new Shoes();
//Calling the setter methods by putting the values in the arguments
s1.setSize(9);
s1.setBrand("Reebok");
s1.setPrice(75);
//calling the display() method on the Shoes class
s1.display();
/* Creating an instance of Shoes class object using Parameterized
* constructor by putting the values as arguments
*/
Shoes s2 = new Shoes(6, "Puma", 60);
//calling the display() method on the Shoes class
s2.display();
}
}
Explanation / Answer
Shoes.java
public class Shoes {
// Declaring instance variables
private int size;
private String brand;
private double price;
// Zero argumented constructor
public Shoes() {
this.size = 7;
this.brand = "Red Tape";
this.price = 50;
}
// Parameterized constructor
public Shoes(int size, String brand, double price) {
super();
this.size = size;
this.brand = brand;
this.price = price;
}
// getters and setters
/*
* This method will get the price of instance variable
*
* @params void
*
* @return double
*/
public int getSize() {
return size;
}
/*
* This method will set the size of instance variable
*
* @params int
*
* @return void
*/
public void setSize(int size) {
this.size = size;
}
/*
* This method will get the brand name of instance variable
*
* @params void
*
* @return String
*/
public String getBrand() {
return brand;
}
/*
* This method will set the brand of instance variable
*
* @params String
*
* @return void
*/
public void setBrand(String brand) {
this.brand = brand;
}
/*
* This method will return the price of instance variable
*
* @params void
*
* @return double
*/
public double getPrice() {
return price;
}
/*
* This method will set the price of instance variable
*
* @params double
*
* @return void
*/
public void setPrice(double price) {
this.price = price;
}
/*
* This method will display the values of instance variables
*
* @params void
*
* @return void
*/
void display() {
System.out.println("Size :" + size);
System.out.println("Brand :" + brand);
System.out.println("Price :$" + price);
}
}
__________________
SportsShoes.java
public class SportsShoes extends Shoes {
/*
* Declaring instance variables
*/
private String shoesType;
/*
* Zero argumented constructor
*/
public SportsShoes() {
super(6, "Adidas", 50);
this.shoesType = "Basket Ball";
}
/*
* Parameterized constructor
*/
public SportsShoes(String shoesType, int size, String brand, double price) {
super(size, brand, price);
this.shoesType = shoesType;
}
// getters and setters
/*
* This method will get the shoes type of instance variable
*
* @params void
*
* @return String
*/
public String getShoesType() {
return shoesType;
}
/*
* This method will set the shoes type of instance variable
*
* @params String
*
* @return void
*/
public void setShoesType(String shoesType) {
this.shoesType = shoesType;
}
/*
* Overriding the super class Display method
* This method will display the values of instance variables
*
* @params void
*
* @return void
*/
@Override
public void display() {
super.display();
System.out.println("Shoe Type :" + shoesType);
}
}
____________________
Demo.java
public class Demo {
public static void main(String[] args) {
//Creating an instance of Sub class of type SportsShoes
SportsShoes sh1=new SportsShoes();
//calling the setter method on it by passing the value
sh1.setShoesType("Tenies Shoes");
System.out.println("**** Displaying the Shoes Info ****");
//Displaying the shoes info
sh1.display();
//Creating an Instance of Sports Shoes class by passing the values as arguments
SportsShoes sh2=new SportsShoes("Joggers Shoes",9, "Nike", 72);
System.out.println("**** Displaying the Shoes Info ****");
//Displaying the shoes info
sh2.display();
}
}
___________________
Output:
**** Displaying the Shoes Info ****
Size :6
Brand :Adidas
Price :$50.0
Shoe Type :Tenies Shoes
**** Displaying the Shoes Info ****
Size :9
Brand :Nike
Price :$72.0
Shoe Type :Joggers Shoes
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.