aspose file tools*
The moose likes Beginning Java and the fly likes Confused on how to create a rectangle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused on how to create a rectangle" Watch "Confused on how to create a rectangle" New topic
Author

Confused on how to create a rectangle

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
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".

my code so far:


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

And where exactly are you stuck?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

the display(graphic g) method.

Is that just another form of paint() or is it a different type of method?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

It doesn't really have anything to do with the paint() method. You just have to create a method named "display" that takes a Graphics object as its parameter, as the instructions say.

By the way, in your code above the constructor that you're supposed to create is also still missing.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

great! I know but I just wanted to get my question answered.

Thanks for the help
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

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
visible
- upperLeftY cannot be resolved to a
variable
- upperLeftX cannot be resolved to a
variable
- The field Component.width is not
visible

here is the new code:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12


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"
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

sorry 'bout that.

I edited the statements like you said but when I select Getters and Setters do I make them private or public?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused on how to create a rectangle
 
Similar Threads
public vs private instance variables
paintDragPreview(...) method needs work
How to fix error in Rectangle Class problem in Java?
Rectangle Class Assignment-need help
Beginner Question: Class Capabilities