Question: How could I implement a toString and utilize equals method to condense my code. For instance use a toString to state the shirt is a
How could I implement a toString and utilize equals method to condense my code. For instance use a toString to state the shirt is a certain color and add a price.
class Clothing{ private String color; private String displayName; private float price; public Clothing(String color, String displayName, float price){ this.color = color; this.displayName = displayName; this.price = price; } public String getColor(){ return color; } public String getDisplayName(){ return displayName; } public float getPrice(){ return price; } public void setColor(String color){ this.color = color; } public void setDisplayName(String displayName){ this.displayName = displayName; } public void setPrice(float price){ this.price = price; } }
//purchases driver class public class Purchases { public static void main(String [] args){ Clothing cloth1 = new Clothing("blue", "Trousers", 19.99f); Clothing cloth2 = new Clothing("Green", "Dr. Who Hoodie", 7.0f); System.out.println("These " + cloth1.getDisplayName() + " are " + cloth1.getColor() + " in color and cost is " + cloth1.getPrice() + " Dollar."); System.out.println("These " + cloth2.getDisplayName() + " are " + cloth2.getColor() + " in color and cost is " + cloth2.getPrice() + " Dollar."); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
