Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java I was graded on the following code and it failed because I did not include an equals method. How would I create an equals

Java

I was graded on the following code and it failed because I did not include an equals method. How would I create an equals method in the following code in java?

public class Clothing2 { //attributes of clothing class private String color; private String displayName; private double price; //Constructor method public Clothing2(String color, String displayName, double price) { this.color = color; this.displayName = displayName; this.price = price; } //create getters public String getColor() { return this.color; } public String getDisplayName() { return this.displayName; } public double getPrice() { return this.price; } //create setters public void setColor(String color) { this.color = color; } public void setDisplayName(String displayName) { this.displayName = displayName; } public void setPrice(double price) { this.price = price; } //to String method public String toString() { return this.color + " " + this.displayName+ " " + "$" + this.price; } public boolean equals(Clothing2 items) { return this.color == items.color && this.displayName == items.displayName && this.price == items.price; } public Clothing2(Clothing2 items) { this(items.color, items.displayName, items.price); } }

Driver

public class Purchases2 { //main method public static void main(String[] args) { //creathing the clothing objects Clothing wear1 = new Clothing("Black", "Khaki Pants", 15.67); Clothing wear2 = new Clothing("Red", "Nikke Shirt", 21.55); //print of the first clothing object System.out.println("The cost is $" + wear1.getPrice() + " for these " + wear1.getDisplayName()+ " in " + wear1.getColor()); //print of the second clothing object System.out.println("The cost is $" + wear2.getPrice() + " for these " + wear2.getDisplayName() + " in " + wear2.getColor()); //print using the toString method System.out.println(); System.out.println(wear1); System.out.println(wear2); System.out.println(); //Compare the 2 items if(wear1.equals(wear2)) { System.out.println("items are identical"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

How do you prepare a cash flow statement

Answered: 1 week ago

Question

Ensure continued excellence in people management.

Answered: 1 week ago

Question

1. Design an effective socialization program for employees.

Answered: 1 week ago