PDA

View Full Version : Very basic java question.?


Dennis R
10-09-2007, 06:00 AM
I am slowly working my way through the Java tutorial, I've only got as far as object oriented concepts and I already have a question.

It states that " By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6. "

What isn't clear though is where I get to state that the bicycle only has 6 gears. It don't think it's the state as that just says what gear it is now?

Am I missing something or jumping ahead?

Thanks for your help

binaryFusion
10-09-2007, 10:37 AM
Hi,

U asked.
1>
"What isn't clear though is where I get to state that the bicycle only has 6 gears."

Ans:
You (the coder) define that while writing the class Bicycle.
A class is a user defined data type. So whenever u define a class, u code all its attributes. Obviously u can inherit them. But those u shall learn a little later.

Simply add the following 2 lines of code to the setGear method sited above by our friend mox 1315 and everything will be clear 2 u.



2>"It don't think it's the state as that just says what gear it is now?"

Ans:
Yes it's definitely that state.
State = instance variables
Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's State.

3> "Am I missing something or jumping ahead?"

Ans:
Yes. U r missing the point that they r trying to explain about the "methods" which are the only means to change the state of
an object or geting info about the state of an object or whatever u do in your code.

When a coder creates a class, he creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

You are not jumping ahead. But I think u should jump a little ahead instead of reading those NOT SO INTERESTING things. Start writing codes or see some code examples Everything will be clear after u write about 5 meaningful programs.

Hope it helps.

mox1315
10-09-2007, 11:18 AM
It is a variable.

int gear;

public void setGear(int gear)
{
this.gear = gear;
}

public int getGear()
{
return this.gear;
}