Sign in to follow this  
velentry

class

Recommended Posts

Ok i have made my first class in Java i seem to find this more easy to understand not sure why :S but anyway i was just wandering if someone could check over my class i wrote it is not supposed to do anything special apart from get me used to the whole concept of Objects having a state and a behavior so ermm here it is and please it is my first thing aside making a hello world appear in the console so don't go on about how thick i am or something i am kinda just hoping i am doing things correctly :) public class Psp { int state = 0; int volume = 50; int playingGame = 0; void changeState(int newValue) { state = newValue; } void volumeUp(int increment) { volume = volume + increment; } void volumedown(int decrement) { volume = volume - decrement; } void playingGameon(int newValue) { playingGame = state + 1; } void playimgGameoff(int newValue) { playingGame = state - 1; } void printStates() { System.out.println("state:"+state+" volume:"+volume+" playingGame:"+playingGame); } } Regards Vel

Share this post


Link to post
Share on other sites
The functions playingGameon and playimgGameoff (which contains a typo by the way) both take a parameter that is never used: "newValue". In addition, the variables "state" and "playingGame" are poorly named: I cannot understand their purpose from the name alone. For example, if state had the value 3 - what would that mean? I would expect the type of "playingGame" to be a boolean, yet it is of type int. If playingGame has the value 42 - what would that mean?

I would suggest you write a small program that might have classes in it - rather than just trying to write a class on its own, devoid of context.

Share this post


Link to post
Share on other sites
that is what i am working towards and as i am going through this tutorial so to speak it will end up that way as a program, i believe it just wanted to get me used to making a class since it keeps giving me questions to answer and one was pick a real life object and make a class for it

But the state was supposed to be the PSP being on or off and 0 meaning off so i thought that if i wanted to play a game i would have to write actually i think i made a mistake lol

if i wanted it to play a game but only once the object is On i would have to write:

void playingGameOn(int NewValue) {
playingGame = (state = 1) + 1;
}

or am i incorrect in thinking so i have only just learnt about different variables so i presume i could put that into it as well

Oh an thanks for pointing out the typo i never realized and what would i put instead of NewValue i'm sure i will learn soon in the tutorial but kinda excited now lol

Regards
Vel

Share this post


Link to post
Share on other sites
and i have just learnt about booleans so actually alot of that class is not needed such as i would use a boolean for the state and gameOn,gameOff and a simple byte for the volume or is that incorrect sorry to keep badgering you i am just curious to make sure i am on the correct track and understanding everything properly :)

Vel

Share this post


Link to post
Share on other sites
Thats a nice looking class.

General naming conventions seem right.

Class names beginning with Capital letter, methods beginning with small.
Only thing you might want to do in the future would be defining visibility for your class components.

"public" for accessors
"private" for internal methods
"protected" for visibility within current package.

As you have three int- variables describing various things, you might want to make those private:

private int state = 0;

Leaving out the identifier causes them to be "protected.

Otherwise the variables can be tampered from the outside, causing all kinds of nasty things in different situations.

You have a bunch of "setters" there already, but making the variables private requires you to add some "getters" there too.

public void changeState(int newValue)
{
state = newValue;
}

public int getVolume(){return this.volume;}

Using reserved word "this" refers to the variable belonging to the class, instead of the possibly identical name of the parameter.

public void setVolume(int volume)
{
this.volume = volume;
}


Oh, and you could make the expressions: volume = volume+ increment shorter simply by:

volume += increment;

Works with all operands.

But Im sure you will run intu this stuff soon enough. Learning a "safe" and productive ways from the beginning is a good thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:

You have a bunch of "setters" there already, but making the variables private requires you to add some "getters" there too.


This is most certainly not true.


It's been a while since I last used Java but IIRC it doesn't support friend classes or methods. Having private variables is arguably better OO practice but how do you access them without a getter if they're private?

I'm sure there is a way but I forgot it. Obviously in C++ with friend support this isn't a problem, but the OP is using Java.

Anyway, @The OP: if this is a first attempt then I'd definitely call it a good one. The class you've written is looking good, there's a few improvements you could make here and there but otherwise it's a good start [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
It's been a while since I last used Java but IIRC it doesn't support friend classes or methods. Having private variables is arguably better OO practice but how do you access them without a getter if they're private?


How am I supposed to check your e-mail for you without your password?

Share this post


Link to post
Share on other sites
I believe the protected keyword in Java actually makes the variables available to the package, not just inheritance hierarchies like some other languages. That aside, there is no reason to add "getters" unless there is some *real need*. You shouldn't add them just because there are private variables. Classes are generally not dumb data holders, and should not be treated as such by default.

Share this post


Link to post
Share on other sites
heya,

thanks for all the input i am still learning a fair amount at the moment i am learning about arrays and how to input data that can be recalled using different elements of the array and then printing them out in the console.

I am actually finding it interesting and I'm not sure if i am learning the best language for my first one apart from a dabble in Python but i find Java somehow easier to grasp.

Thanks for all the input once again

vel

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by ukdeveloper
It's been a while since I last used Java but IIRC it doesn't support friend classes or methods. Having private variables is arguably better OO practice but how do you access them without a getter if they're private?


How am I supposed to check your e-mail for you without your password?


It's just the impression I got from the way rip-off said it, in that he said you didn't always need getters; I misunderstood him and assumed that there was some crazy way to access private variables without them in Java even though it doesn't support friend functionality.

But anyway, I think rip-off might be right about the protected keyword giving a whole package access to a variable but I really have better things to do than to check stuff like that.

Crappy geek chat-up line #378923789479^e = "I lost my password, can I have yours?"

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:

You have a bunch of "setters" there already, but making the variables private requires you to add some "getters" there too.


This is most certainly not true.



There is no way of accessing private class members directly from outside the class.

Thats the whole point of it.
If you had a variable, that can have values from 0 to 12 and you declared it public, or even protected, there is nothing preventing code from other classes from changing the value into something invalid.

By using accessors, you can make sure the variable can only be given decent values.


public void setArraySize(int newSize)
{
if(newSize < 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Defensive programming is the way to go.

[Edited by - Stoo on July 7, 2008 8:50:19 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Stoo
public void setArraySize(int newSize)
{
if(newSize == 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Aye... If newSize == 1, then newSize = 1? [wink]

As for what rip-off was saying, it makes perfect sense. Just because you have a private variable doesn't mean you need a setter and/or getter. Usually when it's private, it's meant to be used by the class itself, not by some outsider. Otherwise you'd make it public. Just as Zahlman said, your email password is private such that you're the one using it - if everyone was meant to use your email, your password would be public. A getter/setter here wouldn't make your password any more secure [wink].

Share this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
Quote:
Original post by Stoo
public void setArraySize(int newSize)
{
if(newSize == 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Aye... If newSize == 1, then newSize = 1? [wink]

As for what rip-off was saying, it makes perfect sense. Just because you have a private variable doesn't mean you need a setter and/or getter. Usually when it's private, it's meant to be used by the class itself, not by some outsider. Otherwise you'd make it public. Just as Zahlman said, your email password is private such that you're the one using it - if everyone was meant to use your email, your password would be public. A getter/setter here wouldn't make your password any more secure [wink].



Heh. That was a hilarious one.. if 1 then 1.
Im not even sure, why I changed the value of the parameter instead of a different approach.

if(value < 1){result = 1}
else {result = value}

If that arraysize from my example was meant for public use, how would you guarantee it could never be set for invalid value? If some value can be used in a wrong way, it sooner or later will be.

Garbage in Garbage out paradigm just doesn't work well these days.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this