Question

Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

DisplayAll Method which is mentioned in the shoes class

void displayAll(ArrayList<Shoes> sh){

System.out.println("Displaying All methods");

for(Shoes a:sh){

a.display();

}

}

//Shoes.java

import java.util.ArrayList;

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);

}

//method that displays all the shoes details

void displayAll(ArrayList<Shoes> sh){

System.out.println("Displaying All methods");

for(Shoes a:sh){

a.display();

}

}

}

Demo class to check that

//Demo.java

import java.util.ArrayList;

public class Demo {

public static void main(String[] args) {

Shoes shoe = new Shoes();

ArrayList<Shoes> sh = new ArrayList<Shoes>();

Shoes sh1=new Shoes(1,"Brand1", 100.0);

Shoes sh2=new Shoes(2,"Brand2", 200.0);

Shoes sh3=new Shoes(2,"Brand3", 300.0);

sh.add(sh1);

sh.add(sh2);

sh.add(sh3);

shoe.displayAll(sh);

}

}

Output:

Problems Javadoc e Declaratic <terminated> Demo [Java Application] C Displaying All methods Size:1 Brand :Brandl Price : $100.0 ize2 Brand :Brand2 Price :$200.0 Size :2 Brand :Brand3 Price : $300.0

Add a comment
Know the answer?
Add Answer to:
Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Write an equals method for the Shirt class provided below. Two shirts are logically equivalent if...

    Write an equals method for the Shirt class provided below. Two shirts are logically equivalent if they have the same size, color, and price. public class Shirt { private Size size; private String color; private double price; enum Size { SMALL, MEDIUM, LARGE } public Shirt(Size size, String color, double price) { this.size = size; this.color = color; this.price = price; } public Size getSize() { return size; } public String getColor() { return color; } public double getPrice() {...

  • UPDATE THE UNIVERSE PROGRAM: C. Research the size of each CelestialObject and its distance from the...

    UPDATE THE UNIVERSE PROGRAM: C. Research the size of each CelestialObject and its distance from the sun (for planets) and the distance of the moons from the planets D. Calculate the distance between any two CelestialObject (e.g. between 2 planets and between two moons) COMPLETE STEPS C AND D and THE MODIFICATION BELOW. SUBMIT THE UPDATES! 1. GALAXY CLASS //**MODIFICATION NEEDED** //Use the DBPM format to complete the class. //Review the SolarSystem class for the Star attribute as an example...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT