Java: no compilation errors, but my result is wrong

My purpose was to create a class called MyRectangle to represent the rectangles.

Mandatory data fields: width, height and color. Use a double data type for width and height and a string for color. Then write a program to test the MyRectangle class. In the client program, create two MyRectangle objects. Assign the width and height to each of the two objects. Set the first object to red and the second to yellow. Display all properties of both objects, including their area.

I wrote everything and I get no errors, but my output remains the same no matter what values ​​I insert in the rectangles.

package MyRectangle;

public class MyRectangle{

  private double width = 1.0;
  private double height = 1.0;
  private static String color = "black";

    public MyRectangle(double par, double par1){ 

      width ++;
      height ++;

  }

    //Parameters for width, height, and color //

    public MyRectangle(double widthParam, double heightParam, String colorParam){ 

      width = widthParam;
      height = heightParam;
      color = colorParam;
      width ++;
      height ++;

  }

    // Accessor width //

  public double getWidth(){

  return width;

  }

  public void setWidth(double widthParam){

  width = (widthParam >= 0) ? widthParam: 0;

  }

  // Accessor height //

  public double getHeight(){

  return height;

  }

  public void setHeight(double heightParam){

  height = (heightParam >= 0) ? heightParam: 0;

  }

  // Accessor color //

  public static String getColor(){

  return color;

  }  

  public static void setColor(String colorParam){

  color = colorParam;

  }

  // Accessor area //

  public double findArea(){

  return width * height;

  }

}

class MyRectangleTest {

        @SuppressWarnings("static-access")
        public static void main(String args[]) {

  // Create triangle and set color value to red //          

  MyRectangle r1 = new MyRectangle(5.0, 25.0);
  r1.setColor("Red");

  System.out.println(r1);
  System.out.println("The area of rectangle one is: " + r1.findArea());

  // Create triangle and set color value to yellow //

  MyRectangle r2 = new MyRectangle(3.0, 9.0);
  r2.setColor("Yellow");

  System.out.println(r2);
  System.out.println("The area of rectangle one is: " + r2.findArea());

        }
}
+4
source share
2 answers

, , .

, 2 2:

  private double width = 1.0;
  private double height = 1.0;
  ...

    public MyRectangle(double par, double par1){

      width ++;
      height ++;

  }

- :

  public MyRectangle(double width, double height){

      this.width = width;
      this.height = height;

  }

, , , .

: System.out.println(r1); System.out.println(r2); Display all properties of both objects toString():

@Override
public String toString()
{
    return "width = " + width + " height = " + height + " color = " + color;
}
+4

:

  • color static, , , , .
  • (double, double) .
  • , .
  • , - , .

, :

public class MyRectangle {

  private double width;
  private double height;
  private String color;
  private static final String DEFAULT_COLOR = "black";

    public MyRectangle(double width, double height) { 
      this (width, height, DEFAULT_COLOR);
    }

    public MyRectangle(double width, double height, String color) { 
      this.width = width;
      this.height = height;
      this.color = color;
  }

  // Rest of the required methods
}
+3

Source: https://habr.com/ru/post/1598556/


All Articles