*****Please Implement in Java******* Instructions: Rewrite the provided Rectangl
ID: 3587928 • Letter: #
Question
*****Please Implement in Java*******
Instructions:
Rewrite the provided Rectangle class to extend GeometricObject and implement the Comparable interface. Override the equals method of the Object class.
Two Rectangle objects are equal if their areas are the same.
Implement the compareTo to be consistent with equals method.
The compareTo method will return 0, if two rectangle are eqauls
The compareTo method return 1 , if the area of rectangle1 is bigger than the area of rectangle2
The compareTo method return -1, if the area of rectangle2 is bigger than the area of rectangle1
Sample Run:
Note:
The first two numbers are the width and height of the first rectangle followed by the width and height of the second rectangle follow by "==" for equals or ">=<" for compareTo method.
The width and height of a rectangle is a positive value and the default value is 1.
For zero and negative numbers use the default values.
Input
2 2
0.5 8
==
Output
true
Input
2 2
0.5 8
>=<
Output
0
Input
2 3
0.5 8
>=<
Output
1
**** Use the following DriverMain class******
class DriverMain{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
double w1 = input.nextDouble();
double h1 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
String s = input.next();
Rectangle rectangle1 = new Rectangle(w1, h1);
Rectangle rectangle2 = new Rectangle(w2, h2);
if(s.equals("=="))
System.out.print(rectangle1.equals(rectangle2)) ;
else if(s.equals(">=<"))
System.out.print(rectangle1.compareTo(rectangle2));
else
System.out.print("Invalid");
}
}
************ Rectangle class**************
class Rectangle {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
}
***************** abstract class GeometricObject************
import java.util.*;
import java.lang.*;
import java.io.*;
abstract class GeometricObject {
//assign default values
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;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
Explanation / Answer
GeometricObject.java
import java.util.*;
import java.lang.*;
import java.io.*;
abstract class GeometricObject {
//assign default values
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;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
____________________
Rectangle.java
class Rectangle extends GeometricObject implements Comparable < Rectangle > {
private double width;
private double height;
public Rectangle() {}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
public int compareTo(Rectangle rectangle2) {
if (this.getArea() > rectangle2.getArea())
return 1;
else if (rectangle2.getArea() > this.getArea())
return -1;
else
return 0;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
public boolean equals(Rectangle rect) {
if (this.getArea() == rect.getArea())
return true;
else
return false;
}
}
________________
DriverMain.java
import java.util.Scanner;
class DriverMain {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double w1 = input.nextDouble();
double h1 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
String s = input.next();
Rectangle rectangle1 = new Rectangle(w1, h1);
Rectangle rectangle2 = new Rectangle(w2, h2);
if (s.equals("=="))
System.out.print(rectangle1.equals(rectangle2));
else if (s.equals(">=<"))
System.out.print(rectangle1.compareTo(rectangle2));
else
System.out.print("Invalid");
}
}
__________________
Input#1:
2 2
0.5 8
==
Output#1:
True
_______________
Input#2:
2 2
0.5 8
>=<
_____________
Output#2:
0
____________
Intput#3:
2 3
0.5 8
>=<
_________
Output#3:
1
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.