I need some guidance with my program. My problem is I ask the car what it would like to do(start, drive, or stop).

You canít drive a car if it is not started (send an error message to the console).
You canít stop a car if it is not started (send an error message to the console).
You canít start a car if it is already started (send an error message to the console).

I got the program to where I can hit enter 1,2, or 3 for each

I also have to show the state of the car but at the time I am not worried about that. My initial issue right now is setting up rules for the three lines I provided. I am not looking for answers as I am trying to teach myself to develop but any help in what someone would recommend. I am adding my code as well as the initial instructions I received.

Create a Java application consisting of two classes. Class one will be your application class. Class two will be a class called Car. Your application will create an instance of Car, called nova, and drive it.

Rules for the car:
You can’t drive a car if it is not started (send an error message to the console).
You can’t stop a car if it is not started (send an error message to the console).
You can’t start a car if it is already started (send an error message to the console).

Once you tell the car to drive, the only thing you can do is stop (Send a message to the console)

Once you call stop, the car will return to the initial state and the user must start the car before attempting to do any other functions. (Send a message to the console)

The purpose of the showState method is provide a way to inspect the state of the car. It should build a message, which can then be sent to the console. The message should look like this:

Well I think I kinda messed it up but my question is I want to start the program to where the user must enter 1 and if it doesnt to return an error. Also return an error if i hit the number for stop when the car hasnt started and the same thing for drive. I guess what I am trying to ask is without showing my like the code just offer suggestions how someone would go about this as the way I am doing it may run into some problems

September 26th, 2012, 10:24 PM

Norm

Re: car program

Do the code one part at a time. Write the code for the first part, compile it, fix the errors and test it.
The steps for the first part:
ask for input
get input
if input not a 1 return an error.

When that is working move to the next part.
Make a list of the steps the code needs to do, then write the code to do them, compile, fix and test

Continue until finished.

September 26th, 2012, 11:03 PM

bkruep

Re: car program

All right I think I am getting some progress but I do have a specific question now. I got the code to make sure 1 is selected first and if it isn't it will return an error and loops until 1 is entered. Now my issue is after I hit 1 to ask the next question it still thinks its a 1 for start instead of a 2 for drive. I am pretty sure it has to do with passing a variable incorrectly or my if statement in the last method.....

Add a println() statement to print out the value of nova so you can see what the computer sees.
Put the println at the place where you think the code thinks its a 1.

Where is the value supposed to be changed?
You may be having a problem with an argument to a method having the same name as a variable defined outside the method. The method can not change the value of any its args source variables.
The value of the variable is passed to the method. The terminology is args are "passed by value".

September 27th, 2012, 11:55 PM

bkruep

Re: car program

I think I am making some progress but having a couple issues. When I run the program I need to hit 1 first to start the car and when I do it acts like it works fine. When I hit 2 or 3 to start it returns an error. However, for some reason it stores the incorrect value. Also since I started messing around I no longer can hit -1 to exit the program. Any help would be appreciated...

Can you copy the console window's contents and paste it here showing your input the the program's output?

On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

September 30th, 2012, 09:35 AM

bkruep

Re: car program

Ok I have made a few changes so now the -1 to exit works and it also works for me to start the program with -1. My remaining issues I believe are if I hit 2 or 3 to start the program it returns an error but also still stores the variable as you can see here...

Start the app
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3: 2
You are driving the car!
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3:

My other issue is even after I hit 1 to start the program it starts over so the only thing I can type is 1 to start the car
Start the app
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3: 2
You are driving the car!
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3:

Here is my code I believe the problem is in the following code and will put the rest of my code after...

You have a scope problem setting the value of nova inside of a method when nova is defined as a local variable or argument to the method. When the method exits, all of the local variables are discarded.