java homework Question 4: The equals Method Write an equals method for the Shirt
ID: 3673298 • Letter: J
Question
java homework
Question 4: The equals Method
Write an equals method for the Shirt class. 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() {
return price;
}
public void setSize(Size size) {
this.size = size;
}
public void setColor(String color) {
this.color = color;
}
public void setPrice(double price) {
if(price > 0) {
this.price = price;
}
}
@Override
public String toString() {
return "Size = " + size + " " +
"Color = " + color + " " +
"Price = " + price + " ";
}
}
Question 6: enum
Note that enums were covered last week, but a few people mentioned wanting some more practice on these, so I am including another question here this week.
Write an enum to represent a school semester- fall, spring, and summer. The enum is also described by the number of weeks (fall and spring have 18, summer has 6). Override the toString to print output like "Fall Semester (18 weeks)".
Create three instances of the enum: sanMateoFall, sanMateoSpring, sanMateoSummer. Sum up the weeks covered in these three semesters and print that value to the console.
Explanation / Answer
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() {
return price;
}
public void setSize(Size size) {
this.size = size;
}
public void setColor(String color) {
this.color = color;
}
public void setPrice(double price) {
if(price > 0) {
this.price = price;
}
}
@Override
public String toString() {
return "Size = " + size + " " +
"Color = " + color + " " +
"Price = " + price + " ";
}
public void equals(Size s, String c, double p)
{
if(size==s)
{
if(color.equals(c)==0)
{
if(price==p)
{
System.out.println("Two shirts are equal");
}
else
{
System.out.println("Two shirts are not equal");
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.