Create a class called Area that implements the Comparable interface. An area is
ID: 3845136 • Letter: C
Question
Create a class called Area that implements the Comparable interface.
An area is a rectangle constructed to be width x height.
Comparison: Two areas are considered "equal" if their perimeters are the same. An area is considered "less than" another area if its perimeter is smaller. An area is considered "greater than" if its perimeter is larger.
Constructor
public Area (int width, int height) throws IllegalArgumentException
width, height are the width and height of the region
throws IllegalArgumentException for non-positive width or height
You must implement the Comparable<Area> interface
You must override the equals method to fulfill the definition of equals.
There are no "setters" or "getters"
Below code has error.
Constructor did properly compare two equal-perimeter areas
Constructor did properly compare two equal-perimeter areas
import java.util.Scanner;
class Area implements Comparable<Area>
{
double width;
double height;
Area(double width, double height)
{
if(width <0 || height <0)
{
throw new IllegalArgumentException("Parameter should not be ngative");
}
this.width = width;
this.height = height;
}
double getArea()
{
return width * height;
}
double getPerimeter()
{
return 2 * (width + height);
}
@Override
public int compareTo(Area o) {
// TODO Auto-generated method stub
if(this.width==o.width && this.height==o.height)
return 0;
if(this.width<o.width || this.height<o.height)
return -1;
else
return 1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Area other = (Area) obj;
if (Double.doubleToLongBits(height) != Double
.doubleToLongBits(other.height))
return false;
if (Double.doubleToLongBits(width) != Double
.doubleToLongBits(other.width))
return false;
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int width,height,secondWidth,secondHeight;
System.out.println("Enter Parameter value for Rectangle 1 ");
System.out.println("Enter width");
height=sc.nextInt();
System.out.println("Enter Height");
width=sc.nextInt();
System.out.println("Enter Parameter value for Rectangle 2 ");
System.out.println("Enter width");
secondWidth=sc.nextInt();
System.out.println("Enter Height");
secondHeight=sc.nextInt();
Area area1 = new Area(width,height);
Area area2 = new Area (secondWidth,secondHeight);
if(area1.compareTo(area2) == 0)
{
System.out.println("AREA are equal");
}
else if(area1.compareTo(area2) == -1)
{
System.out.println("Area 1 is Smaller than Area 2");
}
else
{
System.out.println("Area 1 is Greater than Area 2");
}
}
}
Explanation / Answer
Two Mistakes I found
1)This is wrong:
System.out.println("Enter width");
height=sc.nextInt(); //This should be width
System.out.println("Enter Height");
width=sc.nextInt(); //This should be height
Improvement
System.out.println("Enter width");
width = sc.nextInt();
System.out.println("Enter Height");
height = sc.nextInt();
2) Sometimes This will give wrong Answer eg if height of one is less than of second but width is greater than other
public int compareTo(Area o) {
// TODO Auto-generated method stub
if(this.width==o.width && this.height==o.height)
return 0;
if(this.width<o.width || this.height<o.height)
return -1;
else
return 1;
}
Improvement
@Override
public int compareTo(Area o) {
// TODO Auto-generated method stub
if (this.width*this.height == o.width*o.height)
return 0;
if (this.width*this.height < o.width*o.height)
return -1;
else
return 1;
}
Complete Code:
import java.util.Scanner;
class Area implements Comparable<Area> {
double width;
double height;
Area(double width, double height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Parameter should not be ngative");
}
this.width = width;
this.height = height;
}
double getArea() {
return width * height;
}
double getPerimeter() {
return 2 * (width + height);
}
@Override
public int compareTo(Area o) {
// TODO Auto-generated method stub
if (this.width*this.height == o.width*o.height)
return 0;
if (this.width*this.height < o.width*o.height)
return -1;
else
return 1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Area other = (Area) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int width, height, secondWidth, secondHeight;
System.out.println("Enter Parameter value for Rectangle 1 ");
System.out.println("Enter width");
width = sc.nextInt();
System.out.println("Enter Height");
height = sc.nextInt();
System.out.println("Enter Parameter value for Rectangle 2 ");
System.out.println("Enter width");
secondWidth = sc.nextInt();
System.out.println("Enter Height");
secondHeight = sc.nextInt();
Area area1 = new Area(width, height);
Area area2 = new Area(secondWidth, secondHeight);
if (area1.compareTo(area2) == 0) {
System.out.println("AREA are equal");
} else if (area1.compareTo(area2) == -1) {
System.out.println("Area 1 is Smaller than Area 2");
} else {
System.out.println("Area 1 is Greater than Area 2");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.