Question

Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

Create a Java Project in Eclipse with the following:

Include the Rectangle class supplied below.

Override the toString method for Rectangle.

Override the equals method for Rectangle.

Implement the comparable Interface for Rectangle (Compare by area)

Rectangle class:


public class Rectangle {
  
   private double length;
   private double width;
  
   public Rectangle(double l, double w)
   {
       length = l;
       width = w;
   }
  
   public double getLength()
   {
       return length;
   }
  
   public double getWidth()
   {
       return width;
   }
  
   public void setLength(double l)
   {
       length = l;
   }
  
   public void setWidth(double w)
   {
       width = w;
   }
  
   public void display()
   {
       System.out.print("Rectangle: lenght = " + length + ", width = " + width);
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Rectangle implements Comparable<Rectangle> {

    private double length;
    private double width;

    public Rectangle(double l, double w) {
        length = l;
        width = w;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public void setLength(double l) {
        length = l;
    }

    public void setWidth(double w) {
        width = w;
    }

    public void display() {
        System.out.print("Rectangle: length = " + length + ", width = " + width);
    }

    @Override
    public String toString() {
        return "Rectangle: length = " + length + ", width = " + width;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        Rectangle rectangle = (Rectangle) o;
        return length == rectangle.length && width == rectangle.width;
    }
    
    @Override
    public int compareTo(Rectangle rectangle) {
        return Double.compare(length * width, rectangle.length * rectangle.width);
    }
}
Add a comment
Know the answer?
Add Answer to:
Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT