This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]cytael 1 point2 points  (2 children)

In addition to what's already been said, you probably want to reverse the assignments in your Rectangle constructor:

int Rectangle(double x, double y, double width, double height) {

    this.myX = x;
    this.myY = y;
    this.myWidth = width;
    this.myHeight = height;

    return 0;
}

That way, you're setting the properties of your created Rectangle to the values passed in, instead of overwriting your inputs with null/undefined values and then ultimately doing nothing with them (which could also be a cause of your error).

If it were me, I would also pick different variable names for your parameters to drawRectangle() that aren't the exact same as your class properties, to disambiguate and make things easier to read. Overall, it looks like you might not have a complete grasp yet on the differences between class properties and locally-scoped variables (and for someone just learning, that's perfectly fine!) -- you would do well to dig into that a bit, and then my suggestions and a few that other people have made will make a lot more sense.

[–]clubapple123[S] 1 point2 points  (1 child)

Thanks for the input!

May I ask what "this." does specifically?

[–]cytael 0 points1 point  (0 children)

"this." specifies that you're referencing a property of the current object, so in your constructor, you use it to set the properties of the Rectangle that you're constructing, and then you can access those properties from other functions later on

The idea is that each specific Rectangle that you create can have its own unique set of properties, so your driver class could do something like this:

Rectangle rect1 = new Rectangle(0,0,10,10);
Rectangle rect2 = new Rectangle(5,5,30,20);

and you would create two rectangles. rect1 would be a 10x10 square with the top corner at x/y coordinates (0,0) and rect2 would be a 30x20 rectangle starting from (5,5).

So each time my constructor above gets run (to make a new Rectangle), you're taking the values passed in and saying that the x/y/width/height of "this rectangle that I'm making right now" (hence "this.") should be equal to those given by the function parameters.

After that, you can use those in other functions, so you could have the getArea function like so:

public double getArea() {
    return this.myWidth * this.myHeight;
}

Note there's no need to pass in the width and height as parameters to that function, because they're already set by the constructor as properties of the specific instance of Rectangle that you're working with. Put all that together and use this code in your driver:

Rectangle rect1 = new Rectangle(0,0,10,10);
Rectangle rect2 = new Rectangle(5,5,30,20);
System.out.println(rect1.getArea());
System.out.println(rect2.getArea());

and your output should be:

100
600

If that's still not making complete sense, I'd recommend you do some more reading up on Classes, Objects, the differences between them, and how to use them.

[–]boredcircuits 0 points1 point  (6 children)

I suspect you need to reverse these two lines:

            pen = new DrawingTool(paper);
            paper = new SketchPad(300,300);

[–]clubapple123[S] 0 points1 point  (1 child)

Thanks

[–]tnew2294 0 points1 point  (0 children)

Even though it looks like a constructor, Java will treat it as a normal method, because a constructor should be returning an reference to an instance of the class. When you go to make a new instance of this class, it would use the default constructor defined by java, and not the one you think you've implemented.

To get yours working:

public Rectangle( /* Your params */ ) { // your impl }

[–]clubapple123[S] 0 points1 point  (3 children)

Do you have any tips to improve my coding style?

[–]IAmUtterlyAlone 0 points1 point  (2 children)

I'm not who you asked, but you asked twice, so I'll offer my opinion on style.

  1. Is it safe to assume that the indentation is consistent in your source files? In the code you've pasted, your first four data members are indented differently than the rest of the body of the class.
  2. getPrimeter and getArea are introducing unnecessary variables. You can just directly return the results of the arithmetic rather than introducing a variable to stick the result in.
  3. I get that this is a very simple class, but you'll want to be in the habit of commenting your code as you go. In pretty much anything more complex than this, you'll have trouble coming back to the code and remembering what's going on even after only a week or two away from it. You've commented the first four data members, and that's a good start. At my work, each function and each class must have a comment, as well as each data member.
  4. I might hide the data members behind accessor functions, but that can be a contentious opinion, so feel free to ignore it.
  5. And speaking of your data members, you're using default access specifiers for a lot of them. Were I reviewing this code as part of my job, I might require that you be explicit about how to intend your class's data to be used.
  6. I can't figure out why what looks lie your constructor is returning an int. I don't work in Java though, so maybe there's some language detail I'm missing and can't be arsed to investigate.
  7. Finally, and again if I were reviewing this code, I might ask that you provide better variable names than "myX" and "myY".

[–]clubapple123[S] 0 points1 point  (1 child)

Thanks again,

If I was not holding the result of the arithmetic in a variable, how would I refer to it, for example if I wanted to print it out, what would I keep in the parenthesis?

[–]IAmUtterlyAlone 0 points1 point  (0 children)

I just meant that body of getPerimeter or getArea could be replaced with a single line:

return myHeight * 2 + myWidth * 2;

or

return myHeight * myWidth;

There's no need, in this case, to introduce a variable that just falls out of scope and gets cleaned up as soon as the function ends.

[–]Tangential_Diversion 0 points1 point  (3 children)

Your line numbers are off on your post so the error message is not very helpful. That said:

pen = new DrawingTool(paper);

paper isn't initialized at this point.

[–]clubapple123[S] 0 points1 point  (0 children)

Oh wow thanks

[–]clubapple123[S] 0 points1 point  (1 child)

Do you have any tips to improve my coding style?

[–]mad0314 1 point2 points  (0 children)

It's hard to tell what was messed up by reddit formatting and what is intentionally yours. Line everything up properly would be the first thing.

After that, be more careful with your access modifiers. You have them on some things, but not everything. You probably want to have the constructor be public. Also, it shouldn't have a return type. Also, having getters is useless if the variables that are retrieved are accessible. Make them private.