This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm trying to create a rectangle for my project but I'm stuck and confused about creating the code.
here is the project: "Box.java should have these instance variables (all of type int): upperLeftX, upperLeftY, height, width, and one more instance variable of type Color named boxColor. Make them all private and provide Getters and Setters for each instance variable. Tip: Eclipse will build getters and setters for you. Go to the Menu bar in Eclipse and select Source -> Generate Getters and Setters.
Provide the body of the following Constructor that sets the values of the instance variables:
public Box(int upperX, int upperY, int h, int w, Color myColor)
Create a method called display(Graphics g) that uses setColor(boxColor); and fillRect(upperLeftX,upplerLeftY,width, height); to fill our box".
great! I know but I just wanted to get my question answered.
Thanks for the help
Joined: Mar 12, 2012
I finished up the code but I'm still getting quite a bit of errors.
Lines 6-9 state that "=" is an invalid variabledeclarator
Line 26: the method setColor(Color) is undefined for the type Box
27: Multiple markers at this line
- The field Component.height is not
- upperLeftY cannot be resolved to a
- upperLeftX cannot be resolved to a
- The field Component.width is not
Can you really follow an assignment operator by a semicolon like that? I would have thought was a syntax error. You also need to precede the name of a variable by its type. You got it right with the colour. You should simply declare the fields, and set them all up in the constructor (remembering to omit types in the constructor).
The fields you describe as not visible are obviously in another class.
kenny gill wrote:Create a method called display(Graphics g) that uses setColor(boxColor); and fillRect(upperLeftX,upplerLeftY,width, height); to fill our box".
That sentence is incomplete. What the assignment meant to say is that it should call these two methods on the Graphics object. You should not create your own fillRect method and call that, and calling setColor(boxColor) will do nothing as it will change boxColor to boxColor.
kenny gill wrote:
I don't really get why you have the declaration of boxColor right but can't get the declaration of the other four fields to work. You've even been told the type they should get (int).
this is not valid. you don't have a type for any of these variables. The compiler therefore thinks the name is the type, and the '=' is the name. you need something like
and then the appropriate constructors.
More generally, I'd say you have written WAY too much code before you compiled. NEVER write more than 2-3 lines at MOST before you attempt to recompile. It lets you focus in on where the problem is and not get overwhelmed with "quite a bit of errors"
Joined: Mar 12, 2012
sorry 'bout that.
I edited the statements like you said but when I select Getters and Setters do I make them private or public?
do you understand what it means to make a method public vs private?
a public method can be called by other objects. you made your variables private so they can't be directly accessed by other classes, and now you need some way for other classes to be able to set the variables. If you make the setters private, how will you accomplish this?
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com