Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: car program

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default car program

    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.

     
    import java.util.Scanner;
    public class MyApp
    {
    	public static void main(String [] args)
    	{
    		System.out.println("Start the app");
     
    		Car myCar = new Car();
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.print("Enter a selection by typing 1, 2, or 3: ");
    		int nova = keyboard.nextInt();
     
    		while ((nova != 1) && (nova != 2) && (nova != 3))
    		{
    			System.out.println("Error!");
    			System.out.println("You must enter a valid selection!");
    			System.out.print("Enter a selection by typing 1, 2, or 3: ");
    			nova = keyboard.nextInt();
    		}
    		myCar.start(nova);
    	}
    }
     
     
    //class car
    class Car
    {
    	//public int watts = 100;
    	private boolean isStarted = true;
     
    	public void start(int nova)
    	{
    		//set rules for code
    	}
    	public void drive(int nova)
    	{
    		//set rules for code
    	}
    	public void stop(int nova)
    	{
    		//set rules for code
    	}
     
    	public String showState(int nova)
    	{
    		if(nova == 1)
    		{
    			System.out.println("You have started the car!");
    		}
    		else if (nova == 2)
    		{
    			System.out.println("You are driving the car!");
    		}
    		else if (nova == 3)
    		{
    			System.out.println("You have stopped the car!");
    		}
    		return ;
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: car program

    Here are my instructions as well...

    Programming Assignment 3 Due: Next Class

    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:







    Object Diagram










    Scenario 1:

    1) Create instance of car
    2) Start the car
    3) Send status message to the console
    4) Drive the car
    5) Stop the car
    6) Send status message to the console

    Scenario 2:

    1) Create instance of car
    2) Send status message to the console
    3) Drive the car
    4) Stop the
    5) Send status message to the console

    Scenario 3:
    1) Create instance of car
    2) Send status message to the console
    3) Start the car
    4) Send status message to the console
    5) Stop the car
    6) Send status message to the console
    7) Drive the car
    8) Stop the car

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,601
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: car program

    What specific questions do you have?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: car program

    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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,601
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.....

    import java.util.Scanner;
    public class MyApp
    {
    	public static void main(String [] args)
    	{
    		System.out.println("Start the app");
     
    		Car myCar = new Car();
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.print("Enter a selection by typing 1, 2, or 3: ");
    		int nova = keyboard.nextInt();
     
    		while ((nova != 1) && (nova != 2) && (nova != 3))
    		{
    			System.out.println("Error!");
    			System.out.println("You must enter a valid selection!");
    			System.out.print("Enter a selection by typing 1, 2, or 3: ");
    			nova = keyboard.nextInt();
    		}
    		myCar.start(nova);
    		myCar.drive(nova);
     
    	}
    }
     
     
    //class car
    class Car
    {
    	//public int watts = 100;
    	private boolean isStarted = true;
     
    	public void start(int nova)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		while(nova != 1)
    		{
    			//Scanner keyboard = new Scanner(System.in);
    			System.out.println("You must first start the car to drive or stop it!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
    		showState(nova);
    		System.out.print("What would you like the car to do next: ");
    		nova = keyboard.nextInt();
     
    	}
    	public void drive(int nova)
    	{
    		showState(nova);
    	}
    	public void stop(int nova)
    	{
    		//set rules for code
    	}
     
    	public String showState(int nova)
    	{
     
    		if(nova == 1)
    		{
    			System.out.println("You have started the car!");
    		}
    		else if (nova == 2)
    		{
    			System.out.println("You are driving the car!");
    		}
    		else if (nova == 3)
    		{
    			System.out.println("You have stopped the car!");
    		}
    		return "" + nova;
    	}
     
    }
    Last edited by bkruep; September 26th, 2012 at 10:08 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,601
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: car program

    it still thinks its a 1
    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".
    Last edited by Norm; September 26th, 2012 at 10:19 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

    /*
    Programmer: Bryan Kruep
    Date: 9/11/2012
    Assignment: ProgrammingAssignment3
    */
     
    import java.util.Scanner;
    public class MyApp
    {
    	public static void main(String [] args)
    	{
    		int nova;
     
    		System.out.println("Start the app");
     
     
     
    		Car myCar = new Car();
     
    		do
    		{
     
    		Scanner keyboard = new Scanner(System.in);
     
     
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.println("-1: Ends the program");
    		System.out.print("Enter a selection by typing 1, 2, or 3: ");
    		nova = keyboard.nextInt();
     
    		//if(nova == 1)
    		//{
    			myCar.start(nova);
    		//}
     
    		//if(nova == 2)
    		//{
    			myCar.drive(nova);
    		//}
    		//if(nova == 3)
    		//{
    			//myCar.stop(nova);
    		//}
     
    		}while(nova != -1);
     
    	}
    }
     
     
    //class car
    class Car
    {
    	//public int watts = 100;
    	private boolean isStarted = true;
     
    	public void start(int nova)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		if(nova != 1) isStarted = false;
    		while(nova != 1)
    		{
    			System.out.println("You must first start the car to drive or stop it!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
     
    		showState(nova);
     
     
     
    	}
    	public void drive(int nova)
    	{
    		/*Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please make your decision on what you want the car to do next!");
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		S/tem.out.println("3: Stop the car");
    		System.out.print("What would you like the car to do next: ");
    		nova = keyboard.nextInt();*/
    		Scanner keyboard = new Scanner(System.in);
    		while ((nova == 1) && (nova != 2) && (nova != 3))
    		{
    			System.out.println("This is not a valid selection!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
    		showState(nova);
     
    	}
    	public void stop(int nova)
    	{
    		/*Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please make your decision on what you want the car to do next!");
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.print("What would you like the car to do next: ");
    		Scanner keyboard = new Scanner(System.in);
    		nova = keyboard.nextInt();*/
    		Scanner keyboard = new Scanner(System.in);
    		while(nova == 1)
    		{
    			System.out.println("This is not a valid selection!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
    		showState(nova);
     
     
    	}
     
    	public String showState(int nova)
    	{
     
    		switch(nova)
    		{
    			case 1:
    				System.out.println("You have started the car!");
    				break;
    			case 2:
    				System.out.println("You are driving the car!");
    				break;
    			case 3:
    				System.out.println("You have stopped the car!");
    				break;
    		}
    		return "" + nova;
    	}
     
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,601
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: car program

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

     
    		myCar.start(nova);
    		//}
     
    		if(nova == 2)
    		{
    			myCar.drive(nova);
    		}
    		if(nova == 3)
    		{
    			myCar.stop(nova);
    		}

    /*
    Programmer: Bryan Kruep
    Date: 9/11/2012
    Assignment: ProgrammingAssignment3
    */
     
    import java.util.Scanner;
    public class MyApp
    {
    	public static void main(String [] args)
    	{
    		int nova;
     
    		System.out.println("Start the app");
     
     
     
    		Car myCar = new Car();
     
    		do
    		{
     
    		Scanner keyboard = new Scanner(System.in);
     
     
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.println("-1: Ends the program");
    		System.out.print("Enter a selection by typing 1, 2, or 3: ");
    		nova = keyboard.nextInt();
     
    		while((nova != 1) && (nova != 2) && (nova != 3) && (nova != -1))
    		{
    			System.out.println("This is not a valid selection!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
    		//if statments
    		//if(nova == 1)
    		//{
    		myCar.start(nova);
    		//}
     
    		if(nova == 2)
    		{
    			myCar.drive(nova);
    		}
    		if(nova == 3)
    		{
    			myCar.stop(nova);
    		}
     
    		}while(nova != -1);
     
    	}
    }
     
     
    //class car
    class Car
    {
    	//public int watts = 100;
    	private boolean isStarted = true;
     
    	public void start(int nova)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		if(nova != 1) isStarted = false;
    		while((nova != 1) && (nova != -1))
    		{
    			System.out.println("You must first start the car to drive or stop it!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
     
    		showState(nova);
     
     
     
    	}
    	public void drive(int nova)
    	{
    		/*Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please make your decision on what you want the car to do next!");
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		S/tem.out.println("3: Stop the car");
    		System.out.print("What would you like the car to do next: ");
    		nova = keyboard.nextInt();*/
    		Scanner keyboard = new Scanner(System.in);
    		while ((nova == 1) && (nova != 2) && (nova != 3) && (nova != -1))
    		{
    			System.out.println("This is not a valid selection!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
    		showState(nova);
     
    	}
    	public void stop(int nova)
    	{
    		/*Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please make your decision on what you want the car to do next!");
    		System.out.println("1: Start the car");
    		System.out.println("2: Drive the car");
    		System.out.println("3: Stop the car");
    		System.out.print("What would you like the car to do next: ");
    		Scanner keyboard = new Scanner(System.in);
    		nova = keyboard.nextInt();*/
    		Scanner keyboard = new Scanner(System.in);
    		while(nova == 1)
    		{
    			System.out.println("This is not a valid selection!");
    			System.out.print("Make a selection again: ");
    			nova = keyboard.nextInt();
    		}
     
    		showState(nova);
     
     
    	}
     
    	public String showState(int nova)
    	{
     
    		switch(nova)
    		{
    			case 1:
    				System.out.println("You have started the car!");
    				break;
    			case 2:
    				System.out.println("You are driving the car!");
    				break;
    			case 3:
    				System.out.println("You have stopped the car!");
    				break;
    		}
    		return "" + nova;
    	}
     
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,601
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: car program

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  2. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 8th, 2012, 11:47 PM
  3. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM