1

I've been having trouble with this assignment because of a problem with java unable to find the variable I placed. The assignment is : Implement a Rectangle class with the following properties.

A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The top and bottom edges are at y and y + height.

A method getPerimeter calculates and returns the perimeter of the Rectangle.

A method getArea calculates and returns the area of the Rectangle.

A method draw displays a new instance of a Rectangle object. Refer to the DrawingTool API for details on DrawingTool methods.

Try your rectangle with both the default constructor and with a constructor that can take the x and y coordinates, the length of the rectangle, and the width. Here are some sample constructor calls:

Rectangle rectA = new Rectangle();

Rectangle rectB = new Rectangle(0, -80, 400, 160);

Rectangle rectC = new Rectangle(100, -100, 20, 300);

Here is my driver for the assignment:

public class Driver_class
{
public static void main(String[] args) {
        P4_Icel_Murad_Rectangle rectA = new P4_Icel_Murad_Rectangle();
        P4_Icel_Murad_Rectangle rectB = new P4_Icel_Murad_Rectangle(0,-80,400,160);
        P4_Icel_Murad_Rectangle rectC = new P4_Icel_Murad_Rectangle(100,-100,20,300);

}
}

and my main()

public class P4_Icel_Murad_Rectangle
{

    /**
     * Constructor for objects of class P4_Icel_Murad
     */
    public P4_Icel_Murad_Rectangle(double x, double y, double width, double height)
    {
        // initialise instance variables

        DrawingTool Pen;
        SketchPad Paper;
        //new sketchpad
        Paper = new SketchPad(500,500);
        Pen = new DrawingTool(Paper);
        getPerimeter();
        getArea();
        draw();        
    }
   //Constructor # 2
   public P4_Icel_Murad_Rectangle()
   {
       double x = 0;
         double y = 0;
         double width = 0;
         double height = 0;
       DrawingTool Pen;
        SketchPad Paper;
        //new sketchpad
        Paper = new SketchPad(500,500);
        Pen = new DrawingTool(Paper);
        getPerimeter();
        getArea();
        draw();
    }
public double getPerimeter(){
     double per = (width * 2) + height * 2;
    return per;
}
public double getArea(){
    double area = width * height;
    return area;
}
public void draw(){
 pen.down();
 pen.turnRight(90);
 pen.forward(x);
 pen.turnLeft(90);
 pen.forward(width);
 pen.turnLeft(90);
 pen.forward(height);
 pen.turnLeft();
 pen.forward(y);

}
}

Java says that it can't find the variable width, although I have already listed it out. Thanks for the help in advance!

3
  • 1
    Why are you calling getArea(), getPerimeter() and draw() in the constructor? Note: you can call another constructor from a constructor to reduce duplicated code: public P4_Icel_Murad_Rectangle() { this(0,0,0,0);}
    – fabian
    Sep 1, 2015 at 7:59
  • should i place getArea(), get Perimeter() and draw() outside the constructor?
    – user5129558
    Sep 1, 2015 at 8:03
  • At least getArea() and getPerimeter() just "throw away" the returned value and do not have any effect (except decreasing the performance). As for draw(): In most cases you want to seperate object creation from object "use".
    – fabian
    Sep 1, 2015 at 8:08

2 Answers 2

0
public static class P4_Icel_Murad_Rectangle {

    SketchPad Paper = new SketchPad(500, 500);
    DrawingTool pen = new DrawingTool(Paper);

    double x = 0;
    double y = 0;
    double width = 0;
    double height = 0;

    /**
     * Constructor for objects of class P4_Icel_Murad
     */
    public P4_Icel_Murad_Rectangle(double x, double y, double width, double height) {
        // initialise instance variables

        getPerimeter();
        getArea();
        draw();
    }

    // Constructor # 2
    public P4_Icel_Murad_Rectangle() {
        getPerimeter();
        getArea();
        draw();
    }

    public double getPerimeter() {
        double per = (width * 2) + height * 2;
        return per;
    }

    public double getArea() {
        double area = width * height;
        return area;
    }

    public void draw() {
        pen.down();
        pen.turnRight(90);
        pen.forward(x);
        pen.turnLeft(90);
        pen.forward(width);
        pen.turnLeft(90);
        pen.forward(height);
        pen.turnLeft();
        pen.forward(y);

    }
}
2
  • doesn't like static for some reason, however it shows up 3 windows, but not actual drawing
    – user5129558
    Sep 1, 2015 at 8:04
  • You can remove static. I was testing something and forgot to remove it before posting. My bad. Sep 1, 2015 at 8:16
0

You variables are only declared in the scope of your constructor. Make them private in the class.

1
  • thank you that worked, however I'm only getting a blank sketchpad when I try to draw the three rectangles
    – user5129558
    Sep 1, 2015 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.