CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Car program

  1. #1
    Join Date
    Dec 2008
    Posts
    13

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

    /*
    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
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Car program

    Please use code tags when posting code.
    Also please show the full error message and stack trace so we can see what and where the problem is.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2008
    Posts
    13

    Re: Car program

    Code:
    Here is my 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;
    }
    
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    13

    Re: Car 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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Car program

    The reason you need to use code tags is because it preserves the formatting of the code but you haven't used any so the code is still hard to read. When writing code please format it in a sensible and consistent way.

    I don't get error messages
    Sorry I misunderstood your first post when you said "if I hit 2 or 3 to start the program it returns an error".

    I'm not convinced about your design. The car class should not have any UI stuff in it, it should just be concerned with car related things. The UI stuff should all be in the MyApp class. Actually this is not the way to solve this sort of problem, you should really use the State design pattern as this is a state machine type problem but I guess you are restrained by your homework question.

    Why have you commented out the check for nova == 1?
    Why is your car initialised in an isStarted state?
    Why are you using a boolean to record the car's state when there are 3 states ie stopped, started, driving?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured