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...
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...
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...
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...
Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated; public GeometricShapes() { dateCreated = new java.util.Date(); } public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public void setColor (String color)...
re-implement the Bookshelf class using a linkedlist and a Node class. Implement the above add, remove, and search methods again. here is the code for that: public class Book { private String title; private double price; public Book() { title=""; price=0; } public Book(String title, double price) { this.title = title; this.price = price; } public void setTitle(String title) { this.title = title; } public void setPrice(double price) { this.price = price; } public...
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...
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')...
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)...