V22.0002 - ASSIGNMENT 5


Write a program that simulates and stores the qualities of a car. The qualities (or state variables) you should store are the following:

Color ('R'=red; 'G'=green; 'B'=blue; 'W'=white; 'S'=Silver)
Ignition (false=off; true=on)
Position (any X, Y coordinate in a 20 X 20 grid)

Each of these qualities will be represented by a state variable in main(). You should write a method for each of the following:

  • color assignment: Randomly picks one of the five colors (represented by a letter) and returns the corresponding char to main(). This method should be called once at the beginning of the program. The color will not change during the execution of the program.

  • move car horizontally: This method will need the current X coordinate and the number of spaces you would like to move the car (a negative value will move the car left; positive value will move the car right). It will return the new position of the car after it is moved.

    If the car's ignition is not on, this method tells the user that s/he must turn the ignition on first and returns the current X position.

    If the user tries to move the car beyond the border of the 20 X 20 grid, this method should not move the car; instead you should report an error message and return the current X position

    Hints: You will also have to pass this method the ignition status.

  • move car vertically: This method will need the current Y coordinate and the number of spaces you would like to move the car (a negative value will move the car up; positive value will move the car down).

    If the car's ignition is not on, this method tells the user that s/he must turn the ignition on first and returns the current Y position.

    If the user tries to move the car beyond the border of the 20 X 20 grid, this method should not move the car; instead you should report an error message and return the current Y position

    Hints: You will also have to pass this method the ignition status.

  • change ignition status: This method will turn the car off when it is on or on when it is off. It takes the current ignition value as a parameter and returns the new ignition value for storage in main().

  • report the state of the car: After each action, you should print the "state" of the car. This method will take all the car's characteristics as parameters and report the status of each (including a copy of the cars position in the grid).
    For example:

    Car Stats:
    Color:Red
    Ignition:On
    Location:5,17
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    ----R---------------
    (the R corresponds to the color of the car)
    --------------------
    --------------------
    --------------------

    The initial position of the car should be assigned randomly.

    After calling the method which assigns a color to the car and assigning the car a position, your program should have an infinite loop. Within the loop, ask the user what s/he would like to do next. For example:

    What would you like to do next (1 - change ignition; 2 - change position of car)?

    If the user wants to change the car's position, main will prompt the user for a direction (horizontal or vertical) and the distance to move in that direction. Then the appropriate method would be called with suitable arguments.

    You cannot use any class variables.

    Note: The only error checking in main() verifies that you have a correct choice. Once the user chooses to move the car, the check for the ignition and for boundaries is executed in the called method.