Jump to content

A simple Car Game (Teaches many things to new developers)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Jhost

Jhost

    Newbie

  • Members
  • PipPip
  • 11 posts
Welcome to my Java Tutorial about creating a simple car game that teaches many things about Java Programming to new developers ready to learn the language. If you think this is useless because its simple, its not for you it's for those wanting to learn. If you like it or it helped you please comment, it makes my day

What this tutorial covers:
* Multiple Methods in a single class console program
* Using if and else if statements to make decisions
* Getting input from the user and using it
* Using and comparing Strings
* Outputting text onto the console

The code:
/* 

 * Code by Jhost /*

 */

import java.util.*;


public class cargame {

	static int state = 0; // 0 = off, 1 = on

	static int place = 0; // California = 1, Texas = 2, Illinois = 3, Florida = 4

	static Scanner sc = new Scanner(System.in);

	static String answer;

	

	public static void main(String[] args) {

			System.out.println("Do you want to to turn the car on? (Yes/No)");

			answer = sc.next();

			

				if(answer.equalsIgnoreCase("yes")) {

					changeState();

				}

				else {

					terminate();

				}

				

			System.out.println("Where do you want to drive to? (California, Texas, Illinois, Florida)");

			answer = sc.next();

				

				if(answer.equalsIgnoreCase("california")) {

					place = 1;

					System.out.println("You drive across the United States to California.");

				}

				else if(answer.equalsIgnoreCase("texas")) {

					place = 2;

					System.out.println("You drive to the Southern United States and enter Texas.");

				}

				else if(answer.equalsIgnoreCase("illinois")) {

					place = 3;

					System.out.println("You drive to the Northern Untied States and evter Illinois.");

				}

				else if(answer.equalsIgnoreCase("florida")) {

					place = 4;

					System.out.println("You drive to the Gulf of Mexico and enter Florida.");

				}

				else {

					terminate();

					}

				}

	

	public static void changeState() {

		if(state == 0) {

			state = 1;

			System.out.println("The car has been turned on.");

		}

		else {

			state = 0;

			System.out.println("The car has been turned off.");

		}

	}

	

	public static void terminate() {

		System.out.println("The program has ended due to a choice you have made or an invalid choice you have made");

		System.exit(0);

	}

}

Now, to explain the code.
/* 

 * Code by Jhost /*

 */

First we have comments, which do not impact the code.

import java.util.*;
Here we Import the Utilities class for use of the Scanner.

public class cargame {

	static int state = 0; // 0 = off, 1 = on

	static int place = 0; // California = 1, Texas = 2, Illinois = 3, Florida = 4

	static Scanner sc = new Scanner(System.in);

	static String answer;

Here we tell java the name of our class and create some static variables that can be used anywhere, and hold the state of the car (whether it is on or off), the place of the car, and we create a scanner object to get input from the user, and finally we create a String to use the data we collected from the user.


public static void main(String[] args) { 
This begins our main method, which is the first the java looks for when running a program.

System.out.println("Do you want to to turn the car on? (Yes/No)");

			answer = sc.next();

			

				if(answer.equalsIgnoreCase("yes")) {

					changeState();

				}

				else {

					terminate();

				}

In here we prompt the user whether to turn on the car or not, we make it so we can compare the string regardless of the case. If they say no we call the terminate method, which I will explain later on.

System.out.println("Where do you want to drive to? (California, Texas, Illinois, Florida)");

			answer = sc.next();

				

				if(answer.equalsIgnoreCase("california")) {

					place = 1;

					System.out.println("You drive across the United States to California.");

				}

				else if(answer.equalsIgnoreCase("texas")) {

					place = 2;

					System.out.println("You drive to the Southern United States and enter Texas.");

				}

				else if(answer.equalsIgnoreCase("illinois")) {

					place = 3;

					System.out.println("You drive to the Northern Untied States and evter Illinois.");

				}

				else if(answer.equalsIgnoreCase("florida")) {

					place = 4;

					System.out.println("You drive to the Gulf of Mexico and enter Florida.");

				}

				else {

					terminate();

					}

				}
Here we ask the user to input one of our choices of places to go, and then we use the if statement to display a custom message and set our place variable to that specific number for that place. If the user enters and invalid option we call the terminate method again. The last brace of this section of the code concludes our main method.

	public static void changeState() {

		if(state == 0) {

			state = 1;

			System.out.println("The car has been turned on.");

		}

		else {

			state = 0;

			System.out.println("The car has been turned off.");

		}

	}
This section of the code is our changeState method as we used in the main method, it is not built in, we had to build it and here is what it does. First of all it checks if the state is 0 and if it is it changes it to 1 and displays a message saying that the car has been turned on. Otherwise, if the state is at 1 or anything else(which is not possible here) it changes the state to 0 and displays a message that says the car is off.

	public static void terminate() {

		System.out.println("The program has ended due to you ending the program or an invalid choice you have made");

		System.exit(0);

	}
This is our terminate method. What this does is it displays a message saying that you have either chosen to end the program or entered an invalid choice. Then it simply shuts down the program.

Well that's all for now! I hope you enjoyed my tutorial and learned something. Please post if you liked it!

Edited by Jhost, 13 August 2010 - 09:54 AM.

Posted Image
Posted Image

#2
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
This really helped me in my Java class, thank you very much.

#3
pyaephyokyaw1992

pyaephyokyaw1992

    Newbie

  • Members
  • Pip
  • 7 posts
thanks a lot

#4
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Nice tutorial. Thank you.