Create a class called Area that implements the Comparable in

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\"

Solution

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

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 consider
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 consider
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 consider

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site