Question

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 input = new Scanner(System.in);
       double length = 0.0;
       double width = 0.0;
      
      
       System.out.print("Enter in length: ");
       length = input.nextDouble();
      
       System.out.print("Enter in width: ");
       width = input.nextDouble();
      
      
       ComparableRectangleAlsoCloneable1 obj = new ComparableRectangleAlsoCloneable1();
      
       obj.setLength(length);
       obj.setWidth(width);
      
       ComparableRectangleAlsoCloneable1 obj2 = new ComparableRectangleAlsoCloneable1();
  
       obj2.setLength(20);
       obj2.setWidth(4);
      
       System.out.println(obj.getArea());
       System.out.println(obj2.getArea());
      
      
       System.out.println("obj Compare to obj2: " + obj.compareTo(obj2));
      
       //compare areas and print
       /*if(obj.compareTo(obj2) == 0)
       {
           System.out.println("Both areas are the same.");
       }
       else if (obj.compareTo(obj2) > 0)
       {
           System.out.println("Rectangle 1 area is greater than rectangle 2 area.");
       }
       else
       {
           System.out.println("Rectangle 1 area is less than rectangle 2 area.");
       }*/
      
      
      
      
   }

  
}

public class ComparableRectangleAlsoCloneable1 implements Comparable<ComparableRectangleAlsoCloneable1>
{
   private static double length, width;
  
  

   ComparableRectangleAlsoCloneable1()
   {
       length = 0.0;
       width = 0.0;
   }
  
  
  
   public static void setLength(double l)
   {
       length = l;
   }
  
   public static void setWidth(double w)
   {
       width = w;
   }
  
   public double getLength()
   {
       return length;
   }
  
   public double getWidth()
   {
       return width;
   }
  
   public double getArea()
   {
       return length * width;
   }
  

   public int compareTo(ComparableRectangleAlsoCloneable1 obj)
   {
       if(this.getArea() > obj.getArea())
       {
           return 1;
       }
       else if (this.getArea() < obj.getArea())
       {
           return -1;
       }
       else
       {
           return 0;
       }
   }
  
   //for cloning
/*   @Override
   public Object clone() throws CloneNotSupportedException
   {
       return super.clone();
   }*/

  
   //to string method
   @Override
   public String toString()
   {
       return "Length: " + getLength() + ", Width: " + getWidth() + ", Area: " + getArea();
   }

  
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

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 input = new Scanner(System.in);
       double length = 0.0;
       double width = 0.0;

       System.out.print("Enter in length: ");
       length = input.nextDouble();

       System.out.print("Enter in width: ");
       width = input.nextDouble();

       ComparableRectangleAlsoCloneable1 obj = new ComparableRectangleAlsoCloneable1();

       obj.setLength(length);
       obj.setWidth(width);
       System.out.println(obj.getArea());

       ComparableRectangleAlsoCloneable1 obj2 = new ComparableRectangleAlsoCloneable1();

       obj2.setLength(5);
       obj2.setWidth(5);

       System.out.println(obj2.getArea());
      
       System.out.println("obj Compare to obj2: " + obj.compareTo(obj2));

   }

}

class ComparableRectangleAlsoCloneable1 implements Comparable<ComparableRectangleAlsoCloneable1> {
   private double length, width;

   ComparableRectangleAlsoCloneable1() {
       length = 0.0;
       width = 0.0;
   }

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

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

   public double getLength() {
       return length;
   }

   public double getWidth() {
       return width;
   }

   public double getArea() {
       return length * width;
   }

   public int compareTo(ComparableRectangleAlsoCloneable1 obj) {
       return new Double(obj.getArea()).compareTo(this.getArea());
   }

   // for cloning
   /*
   * @Override public Object clone() throws CloneNotSupportedException {
   * return super.clone(); }
   */

   // to string method
   @Override
   public String toString() {
       return "Length: " + getLength() + ", Width: " + getWidth() + ", Area: " + getArea();
   }

}

// you have created variables as static because of that for every object has same values because of that your getting the same value

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....
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
  • 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...

  • 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()    {   ...

  • 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...

  • 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; }...

  • I have this program: It passes on certain test cases. The Demo five has the %.1f...

    I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as: Enter length: \n Enter width: \n You entered: 87.3, 2.0, and 174.7\n -- Rectangle info --\n Length: 87.34\n Width: 2.0\n Area: 174.68\n And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you. Program Content:...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • 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...

  • 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...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • 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...

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