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

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 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 { 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*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"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!