aspose file tools*
The moose likes Beginning Java and the fly likes Need help with Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with Strings" Watch "Need help with Strings" New topic
Author

Need help with Strings

Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
I hope someone will take the time to read all this. I really need help and I have no one around to ask except for JavaRanch. I need some help with questions 2, 4 and 5. I created a string but I'm not sure if I'm supposed to do anything with it. Question 4. I just get an error message when I try to compile. And number 5 I don't know if I'm doing right before I can't do number 4 right.

Here are the questions:

1) A. Now open the “picture” project in BlueJ. Right-click on the “picture” class and select open editor. Scroll down and read through the “draw” method. Next, compile all of the classes in this project (look under the “tools” menu). Right-click on the picture class and select “new picture()” to create an instance of a picture. Right-click on the picture INSTANCE and select “draw()” to show the picture.

B. Go back to the editor and modify the “draw” method so that it will now draw a picture of a “stop-light” rather than a house. The instruction sequence that you wrote down for Lab #1 will give you the basic sequence of what the code should look like; the code shown in the “draw” method for the house picture will show you what the syntax should look like for each of the instructions. You will also need to change the fields in this class to include three circle objects and three square objects. Delete the other unused fields from the original code.

2) Create a “String” field with an identifier of “currentColor”. Also, you should already have three circle fields, something like redLight, yellowLight, greenLight if you have done question #1 correctly, along with three square fields for the boxes for the three lights.

3) Create three simple methods for your picture class with the following signatures:

void redLight( )
void yellowLight( )
void greenLight( )

Each method should turn the other two lights to black, and turn the appropriate circle light to its color. These methods now allow you to turn your stop light from green to yellow to red states. Test each of these to make sure they work correctly! Randomly execute them several times in different orders from the object workbench to do your testing.

4) Create an ACCESSOR method and a MUTATOR method for the currentColor field. These should have the following signatures:

void setCurrentColor( String newColor)

String getCurrentColor( )

Write the complete methods (signature and body). Refer to the section on accessors and mutators in the text for help with these methods.

5) Add a line of code to your methods redLight(), yellowLight(), and greenLight() that will set the state of the currentColor field to “red”, “yellow” or “green” as appropriate for each method.
CALL THE MUTATOR METHOD you wrote in #4 to accomplish this.


---And my code which is supposed to do what the questions say...




Please any help, comments, suggestions, or just solving the problem will be greatly appreciated.
Travis
[edit]Add code tags. CR[/edit]
Janus Engstrøm
Ranch Hand

Joined: Nov 01, 2006
Posts: 44
Hi Travis,

First of all you should use the Code-tag for your code, it makes it more readable.
Second, you need to post the compiler message.
Third, what kind of objects are Square and Circle? Does the error message from the compiler refer to these objects?
Fourth, you have a syntactic error in the method getCurrentColor(). You write:



It should be



Too, you haven't defined the member variable CurrentColor, so you're trying to return something that doesn't exist. You only have your 3 Square and 3 Circle. You need



Too, your constructor doesn't use the passed parameter. If could very well be:




At last, variables shouldn't begin with a capitalized letter, you should rename the CurrentColor to currentColor.

Hope this will help you a bit.

Kind regards,
Janus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Please always quote code with ctrl-c ctrl-v and the CODE button. I have edited your original post and you can see how much better it looks.

Your getCurrentColor method should readDelete the additional word "string".

Where are your redLight() greenLight() and yellowLight() methods?
What do you think the correct way to set currentColor would be? We never simply answer that sort of question: see this FAQ.
And if you are going to do good design of your classes, can you think of how to write a single changeLights method which will change the lights depending on whether you pass "red" "yellow" or "green"?
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
I appreciate all the help so far. Sorry about the code. Mostly new to programming and new to JavaRanch altogether. I fixed the getcurrentColor() method. I don't have any more syntax errors right now but I still need to do one or two more things. The frustrating thing was that all the compiler said was either ; or ) expected and neither helped. I tried running through what I know to find out why there should be one of the two.

I understand that there are certainly more elegant and less time consuming ways to write a program that does the exact same as this one, but since I am new to programming and so far this is along the lines of the code that I've already seen, I chose this way.

I completely understand that you don't want to just throw the answers at me. You just tell me something you already knew. I turn in my homework (if I were actually enrolled) and everybody is happy until next week. I'm here to learn not to just make a simple program once.

In my program the lights are named light, light2, and light3 for red, yellow, and green respectively. I can repost a copy of my code with changes if necessary.

For the accessor it should return the value of the string currentColor. I think I have it setup right. For the mutator, should it change the string currentColor to a color (within the limits of Blue J) of my choosing? or is this where the program is told to actually change the string so we get the same color in the string as the color being displayed? Either way I'm not really sure how to code them.

But I'll give it a try:

from the methods redlight()... to change the value of the string:


but for the mutator I'm not sure what to really put here. I tried the code in the program. Although there are no syntax errors it doesn't work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
[Pedantic mode]Call the method setCurrentColor with a capital letter for each word except the first.[/pedantic mode]

Look closely at the body of the method, and remembering that the assignment operator takes what is on its right and applies it to what is on its left . . . .
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
I think I fixed all the capitals that you mentioned. I can now use the mutator to change the state of the string but I haven't yet figured out how to get the program to do it automatically. The assignment is to call the mutator but how do you call a field? Here's what I did but it has no affect.

Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
I figured it out. I had a few extra words in there and some stuff out of place and a backward assignment. Thanks!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You're welcome

Please explain, for the benefit of future readers, why writing String in that last method you quoted caused problems.
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
I think it is because I was assigning the color to "string newColor" and not just the variable "newColor". The variable was already defined is not necessary within the method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Agree.

The official nomenclature is that by writing String you are declaring a local variable; when you reach the end of the method that variable goes out of scope and vanishes. If you miss out the type "String" you get the field.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Strings