This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Variables assigned to objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variables assigned to objects" Watch "Variables assigned to objects" New topic
Author

Variables assigned to objects

Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13

I need to know how to create an object of Dice with Die1 and Die2.
would I just start with
Dice Die1 = new Dice();
Dice Die2 = new Dice();

Here is where I would need to assign the value to Die1 and Die2 for the setUpperFace and Im not sure how to do that.

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Your class is called Die2. There is no object defined for Dice, so your statements:


are syntactically correct but not correct for the situation you've shown us. With the object Die2, you would instead create Die2 objects, maybe Dice1 and Dice2, as in:


OR, you could rename the class you've written to "Dice." That might be simpler to implement and less confusing in the long run.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Tom Orr wrote:...

I don't think setUpperFace and getUpperFace are doing quite what you intend. I see two different variables called "UpperFace." One is local to the setUpperFace method, and is only being used as a temporary variable for the value returned by Roll(). The method is not setting any class or instance variable (which don't appear to exist). The other "UpperFace" variable is local to the main method, and doesn't appear to be used for anything. The getUpperFace method won't be able to return anything, because there doesn't appear to be an "UpperFace" variable defined within that scope.

Also, be careful with your syntax. Java is case sensitive, so "GetUpperFace" is not the same as "getUpperFace" -- both of which appear in line 7. But neither of these will work, because a method call also needs parentheses. That is, getUpperFace(). And by convention, Java variable and method names begin with lowercase characters.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13
Ok thanks for the help. I have made some changes and here is what I have.


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Let's take a step back. When you think about defining a class, think about what the object should HAVE (properties) and what it should DO (methods).

Your class is modeling a die. As you've already determined, an "upperFace" is a property it should HAVE (which defines its "state"), and "roll()" is something it can DO. These suggest something like this...

Notice that the object's state (upperFace) is private, which prevents it from being changed by some other object. But other objects might need to know the value of upperFace, and that's where the accessor (or "getter") method comes in. It simply returns that value.

A mutator (or "setter") method provides a way to set the value. Typically it would work like this...

But I'm not sure you want to be able to set a particular value passed in from outside, so you might not need a setter at all. Instead, upperFace should probably be determined only by calling roll(), which would just assign a new value to upperFace. In other words, roll() changes the state of the die.

Work with these ideas a bit, and see if that helps.
Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13
I really do want to understand this stuff. Here is what I am trying now. I think I keep trying to rethink everything too much. Im not sure when I would use static and not use static. I dont understand where to put the {} brackets on a class vs a method and how a class and a method differ. Couldnt I just make a method that represents a die roll and it would work the same as a class that represented a die roll? Is the class just a seperate program that can be accessed by another program where a method could only be accessed from within a class? I am feeling so dumb and confused. This looks like it should be so simple and basic that I am just not getting some basic flow of the programming. UGH



Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Im not sure when I would use static and not use static.

Static (either variables or methods) members belong to the class which defines them. You don't need an instance of the class to access them. On the other hand non-static members belong to the each instance of the class.

I dont understand where to put the {} brackets on a class vs a method and how a class and a method differ

Well think of a class as a container of both properties (variables) and methods. And the brackets ({}) are used the same way for a class and a method i.e.: To wrap their contents.
Is the class just a seperate program that can be accessed by another program where a method could only be accessed from within a class?

Your program may consists of one or more classes, they actually define your program. And no, a method can be accessed from other class instances (depending on their visibility) as well as from the same class which defines it. Being static or non-static applies here as you can't access an instance (non-static) method from a static context.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

I have the feeling you made everything static because you're calling things from the main clause.
Actually you want to create an object in Main and work from there.


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variables assigned to objects