one of my goals for this year is to learn java, and I found an excuse to get started lately. So over hte last couple of hours I have been reading tutorials and the likes, and started following the advice from this post http://www.daniweb.com/software-development/java/threads/99132/starting-java-java-tutorials-resources-faq

As such I am up to this point http://docs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html

it suggested I lookeda around and made note of the state/ behaviours of objects surrounding me. As such I noted the only things that jumped out at me , my books and hte tv, which seemed sufficient for the exercise. However, for the tv I am trying to use an enum to select between tv mode i.e hdmi, tv, component etc. Just seemed like an excuse to check out the enums! However I have compile errors and don't know the appropriate way to use enums, advice to get me on the right track would be appreciated.

private enum TvMode{ TV, HDMI, HDMI1, Component;} 
TvMode mode = TvMode.TV; // default mode



public static TvMode getTvMode(){
        // return current state of mode
        return mode;
    }

public static void setTvMode(mode){
        // set new value of mode
        mode = TvMode.mode;
    }

I may be a beginner but I can tell im doing it totally wrong. Thanks in advance~

Recommended Answers

All 4 Replies

saying what the errors are might help us out a bit.
personally, I wouldn't see them as enums, rather as different implementations of a shared interface.

Line 11: You forgot to declare the type of the parameter.
Line 13: That's not how to fix the <parameter with the same name as a variable> problem. Use this to refer to the variable, eg

this.mode = mode;

For what you are doing so far, I think you're right to use an enum. If you start to add a load of different data or behaviours to each of those states then there will come a time when making them classes (as Stutltuske suggests) will be the best thing to do.

Thank you for your help, I onl intend to set which mode the tv is on and return it to be printed. Thanks to the help you've given I can now compile my file, but I'm not sure how to set the value of the mode. I tried messing around with it but i'm still not able to work it out.

myTv.setTvMode(TvMode.HDMI);
myTv.printState();  // now set to HDMI

the method printState just computes a string to print based on all the value of the fields related to the tv, such as channel, on/off, powered or not powered, etc. I get the following error when compiling:

cannot find symbol
and it says it points to TvMode, but when i just put in HDMI or "HDMI" i get errors as well, so im not sure what to do.

How is the set method declared? should be something like

void setTVMode(TvMode newMode) { 
   mode = newMode;
}

... in which case your myTv.setTvMode(TvMode.HDMI); should be OK

If you still have a problem be sure to post the complete error message and the exact line of code it refers to.

ps There's no point making the set method public if the parameter type is private!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.