Jump to content
  • Advertisement
Sign in to follow this  
velentry

class

This topic is 3759 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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:

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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!