Question: Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll , that takes an ArrayList
Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom)
Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts]
The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object [25 pts]
In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects) [25 pts]
In the main method, make a call to the displayAll method, passing your ArrayList of objects [25 pts]
--------------------------------------------------------------------------------------------------------------------------------------------------------
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
