Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: No compile errors, but my output is wrong

My assignment was to create a class named MyRectangle to represent rectangles.

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

I've written everything out and am getting no errors, but my output stays the same no matter what values I put in for 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());

        }
}
like image 219
Dio King Avatar asked Mar 16 '23 09:03

Dio King


2 Answers

The constructor you are using makes no sense.

You ignore the passed rectangle dimensions, so you'll always get a 2 by 2 rectangle:

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

    public MyRectangle(double par, double par1){

      width ++;
      height ++;

  }

It should be something like :

  public MyRectangle(double width, double height){

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

  }

In addition, the color member shouldn't be static, unless you want all your rectangles to have the same color.

One last thing - in order for System.out.println(r1); and System.out.println(r2); to Display all properties of both objects, you must override toString():

@Override
public String toString()
{
    return "width = " + width + " height = " + height + " color = " + color;
}
like image 85
Eran Avatar answered Mar 29 '23 13:03

Eran


There are a couple of things wrong here:

  1. The color member is static, which means in belongs to the class, instead of each instance having its own.
  2. The (double, double) constructor doesn't store the height and the width.
  3. Both constructors increment the height and the width, for no good reason.
  4. Since you don't have a default constructor, the default values for the members are redundant - there's no flow where they won't be overwritten.

To sum it up, your class should be declared more or less like this:

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
}
like image 42
Mureinik Avatar answered Mar 29 '23 13:03

Mureinik