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() {
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 + "\n" +
"Color = " + color + "\n" +
"Price = " + price + "\n";
}
}
code:
import java.util.*;
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 + "\n" +
"Color = " + color + "\n" +
"Price = " + price + "\n";
}
//equals method checks the conditions all the
parameters are same or not
public void equals(Shirt var1,Shirt var2)
{
if(var1.size.equals(var2.size)
&& var1.color.equals(var2.color) &&
var1.price==var2.price)
{
System.out.println("The two shirts are logically
equivalent");
}
else
System.out.println("The two shirts are not logically
equivalent");
}
public static void main(String[] args)
{
Scanner sc=new
Scanner(System.in);
String s1,s2,color1,color2;
double price1,price2;
//Taking size of the both shirts
and converting into enum Size
System.out.println("Sizes are only
SMALL,MEDIUM,LARGE");
System.out.print("Enter the size of
shirt 1:");
s1=sc.nextLine();
Size
size1=Size.valueOf(s1.toUpperCase());
System.out.print("Enter the size of
shirt 2:");
s2=sc.nextLine();
Size
size2=Size.valueOf(s2.toUpperCase());
//Asking colors to the user
System.out.print("Enter the color
of shirt 1:");
color1=sc.nextLine();
System.out.print("Enter the color
of shirt 2:");
color2=sc.nextLine();
//Asking price
System.out.print("Enter the price
of shirt 1:");
price1=sc.nextDouble();
System.out.print("Enter the price
of shirt 2:");
price2=sc.nextDouble();
//Creating Shirt class
objects
Shirt obj1=new
Shirt(size1,color1,price1);
Shirt obj2=new
Shirt(size2,color2,price2);
System.out.println("Dimensions of
shirt1:");
//printing the data of the
shirts
String d1=obj1.toString();
System.out.print(d1);
System.out.println("Dimnesions of
shirt2:");
String d2=obj2.toString();
System.out.print(d2);
System.out.println();
//using equals method to check the
shirts are equivalent or not
obj1.equals(obj1,obj2);
}
}
outputs:
If you have any queries, please comment below.
Please upvote , if you like this answer.
Write an equals method for the Shirt class provided below. Two shirts are logically equivalent if...
The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() { dateCreated = new java.util.Date(); } protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public...
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...
Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...
Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...
JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
Using the following Java Class.... public class MicroWave { // Represent possible microwave heat selections private final int LOW = 1; private final int MEDIUM = 2; private final int HIGH = 3; // specifies the microwave heat selection(default is MEDIUM) private int heatSelection; // specifies whether the microwave is on private boolean on; private String color; public MicroWave(int heatSelection, boolean on, String color) { this.heatSelection = heatSelection; this.on = on; this.color = color; } ...
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...
You will write a class called Shoe.java Shoe.java should have Three instance variables String brand (cannot be blank) double size (from 5 to 12) int color (a number from 1 to 5 representing one of 5 colors) This code is to control the amount of colors. the colors represented are as follows 1. red 2. green 3. blue 4. black 5. grey One constructor, one get method per instance variable, one set method per instance variable. You will need a...
I've written this program in C++ to compare 2 pizzas using classes and it works fine. I need to convert it to java using both buffered reader and scanner and can't get either to work. //C++ driver class for Pizza program #include <iostream> #include <string> #include <algorithm> #include <iomanip> using std::cout; using std::cin; using std::string; using std::endl; using std::transform; using std::setprecision; using std::ios; using std::setiosflags; #include "Pizza.h" int main() { char response = 'Y'; while (response == 'Y')...