Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Calling and changing strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling and changing strings" Watch "Calling and changing strings" New topic
Author

Calling and changing strings

Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
Hello, I just joined this forum and it looks good. I've had some experience with C++ and as part of my class I'm learning programming through Java.

The assignment: I need to create a basic stoplight.
Then create a string with identifier "CurrentColor".
Then create methods that when used turn one of the lights on and the others off.
Then I need to create and Accessor and a Mutator method for the CurrentColor field. I'm told that they should have the following signatures: "void setCurrentColor( String NewColor)" and "String getCurrentColor()".
Finally, add a line of code to the methods that will set the state of the CurrentColor field to "red", "yellow", or "green".

I have the physical stoplight created and I have the methods for turning the lights to the appropriate state and I believe I have correctly made a string but I'm still unsure. I need some help with syntax and what to actually write. I'm not sure if I should put in print statement to achieve this or not. Also, I'm not sure how get the methods to communicate with the string so I can print and tell what color the light is.

I'm using Bluej for programming.

I don't know if this helps but here's the code. I've edited some out because its not necessary for the question.



If you have any other comments that you would like to add, please feel free. And keep in mind: I don't want you to write the code, I want to learn how to program and if you write it then we haven't achieved anything.

[edit]Add code tags. CR[/edit]
[ December 21, 2008: Message edited by: Campbell Ritchie ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Travis Hagan:
...//Here's is the string I was told to create. Not sure if I did it right
public static void main ( String[] CurrentColor )...

Welcome to JavaRanch!

The brackets [] symbolize an array, so String[] is an array that holds references to Strings. Note that a Java String is its own type of object -- is not interchangeable with an array of chars, for example.

You can create a new String in Java just like any other object: By calling its constructor...

String myString = new String("This is it.");

However, there is also a shorthand approach of assigning a String literal directly to the variable...

String myString = "This is it.";

String literals do receive special treatment in Java, and the implications of one approach over the other are described in Strings Literally, which should also provide you some illustrations on creating Strings.

Also note that String objects are immutable, so you cannot alter one after it's been created. But you can reassign different String objects to the same variable.
[ December 21, 2008: Message edited by: marc weber ]

"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
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
thanks Marc. I now understand strings a lot better. I'm currently trying to figure out the accessor and mutator. Please correct me if I'm wrong but I think I need to place a statement changing the string at the end of each code that turns the light to the appropriate color. Now that I have that I am trying to recall the information from the string. I am having a few syntax errors but I've so far been able to resolve them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling and changing strings
 
Similar Threads
Need help with Strings
changing colors in an if statement
Creating a working traffic light!!
Creating a working traffic light!
Creating a working trafficlight!