Results 1 to 5 of 5

Thread: Ca program

  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default Ca program

    I was seeing if someone can take a look at my code for my program as I have two issues...

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

    Java Code:
    		//if statments
    		//if(nova == 1)
    		//{
    		myCar.start(nova);
    		//}
    
    		if(nova == 2)
    		{
    			myCar.drive(nova);
    		}
    		if(nova == 3)
    		{
    			myCar.stop(nova);
    		}
    Here is the rest of my code...
    Java Code:
    /*
    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;
    	}
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Ca program

    Quote Originally Posted by jimJohnson View Post
    if I hit 2 or 3 to start the program it returns an error
    Whenever seeking help with an error, post the entire error text or stack trace.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default Re: Ca program

    I apologize as I did not interpret it correctly. It does not make an error but it just doesn't return an error message when it should.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Ca program

    Clear as mud. Errors are thrown, not returned.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default Re: Ca program

    I don't get error messages but here are my problems. When I start the program I am trying to set it up to where if I don't enter a 1 it will return an error and this part is correct. However, after I do hit the 1 it pretty much just loops my program and starts over again when I want it to test what the car would do next and it just starts over. The other thing it does When i hit like 2 to start the program and then hit 1 it stores both numbers which is not what I want it to do either. It is due tomorrow but if there is anything you can see please let me know.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •