Hi I have an assignment to do where I have to make a race between two cars (this assignment is a continuation of a previos 'mini' assignment where I had to do the 'race' with one car).

The commands that I implemented for the cars is foward (increases distance travelled), reverse (decreases the distance travelled), left and right (left and right doesn't increase or decrease the distance). Now what I need to do is make a race between two cars where what is currently displayed needs to be accompanied by the commands of the other car.

Console example of the layout.
To make the two columns you can use \t.

Car1. . . . . . . . . . . . . . . . .Car2

Forward 1km. (total). . . . . . Right.
Left. . . . . . . . . . . . . . . . . Reverse -1km. (total)
Forward 4km. (total). . . . . . Left.
Reverse 3km. (total). . . . . . Forward 3km. (total)

Oh and i'm using two classes because that is what was required by my lecturer.

Simulator.java

import java.util.Random;
class Simulator{
    public static void main(String args[]){
        
        vehicleCMD car = new vehicleCMD();
        Random rand = new Random();
        
        int km, km_rev;
        int do_what;
        
        car.start();
        while (car.cmd_start == true){
            km = 1+rand.nextInt(3);
            km_rev = 1+rand.nextInt(1);

            do_what = rand.nextInt(4);

            switch(do_what) {
                case 0: km = car.forward(km); break;
                case 1: car.left(); break;
                case 2: car.right(); break;
                case 3: km_rev = car.reverse(km_rev); break;
            }
            
            if (car.total_km >= 70){
                car.stop();
            }
        }
        
    }
}

Methods class.

public class vehicleCMD{

boolean cmd_start = false;
boolean cmd_stop = false;
boolean cmd_forward = false;
boolean cmd_reverse = false;
boolean cmd_right = false;
boolean cmd_left = false;
int total_km = 0;
    
    public void start(){
        cmd_start = true;
        cmd_stop = false;
        System.out.println("Power ON.");
    }
    
    public void stop(){
        cmd_stop = true;
        cmd_start = false;
        System.out.print("\n\nGas Tank empty. Car STALLED");
    }
    
    public int forward(int km){
        cmd_reverse = false;
        cmd_forward = true;
        total_km = total_km + km;
        System.out.println("Forward " +total_km+ "km. (total)");
        return km;
    }
    
    public int reverse(int km_rev){
        cmd_forward = false;
        cmd_reverse = true;
        total_km = total_km - km_rev;
        System.out.println("Reverse " +total_km+ "km. (total)");
        return km_rev;
    }
    
    public void left(){
        cmd_left = true;
        cmd_right = false;
        System.out.println("Left.");
    }
    
    public void right(){
        cmd_right = true;
        cmd_left = false;
        System.out.println("Right.");
    }
    
}

The code is in working order and is totally open source.

Comments
For using code tags in your first post which contains code :)
jeanfrg
Deleted Member
boolean cmd_start = false;
boolean cmd_stop = false;
boolean cmd_forward = false;
boolean cmd_reverse = false;
boolean cmd_right = false;
boolean cmd_left = false;

Wouldn't it be a better idea to make these private instead of using the default access specifier?

What difference would it make? =S

What difference would it make? =S

Found on line 12 of your Simulator.java source file:

while ( car.cmd_start == true ) {
  /*
   * Other code here
   */
}

Your code is correct, but it will not protect you from making typos like:

while ( car.cmd_start = true ) {  // typo, only one '='
  /*
   * Other code here
   */
}

If you use an accessor method instead, such as getStartFlag() , you had to write the loop like this:

while ( car.getStartFlag() ) {
  /*
   * Other code here
   */
}

Now you don't risk your program to turn into an endless loop if you make such a typo.

Edited 6 Years Ago by mvmalderen: fix typo

Comments
Great method of avoiding typos. :D

Oh ok thanks =D
I will certainly be using that method from now on.

Can you help me with my original problem though?
Thanks again.

I would make a RaceSimulator class with (at least) these abilities:

public interface:

  • an addCar() method, which takes a car as argument, and adds it to an ArrayList of cars for the race.
  • a startRace() method, which simulates a car race, by iterating over the whole ArrayList containing the cars for the race. Then you could use a while loop on a per car basis, like this pseudocode illustrates:
    while ( car has not finished the race ) {
      generate actions for this car
    }

Won't this method make the moves car1 made on top of car2 and not near?

car1
*moves*
car2
*moves*

I want to find out how to make the moves in different columns...

car1 [\t here] car2
*moves* [\t here] *moves*

Won't this method make the moves car1 made on top of car2 and not near?

Oh, now I get what you mean, I first didn't get your example output, now I do. Yes, you're right.

You could use the printf() method for printing the tables, then you don't need to use the tedious '\t' approach. Anyhow, you might want to create a moveCar() method which takes a car as argument, and computes a next move for the car passed to it.
Or, you could first build a string containing the next table entry, by using the format() method from the String class.

Edited 6 Years Ago by mvmalderen: n/a

Oh, now I get what you mean, I first didn't get your example output, now I do. Yes, you're right.

You could use the printf() method for printing the tables, then you don't need to use the tedious '\t' approach. Anyhow, you might want to create a moveCar() method which takes a car as argument, and computes a next move for the car passed to it.
Or, you could first build a string containing the next table entry, by using the format() method from the String class.

Thanks for your help :)

I actually found a simpler way... using two objects. I'm so stupid :D
I managed to finish up the program.
Here is a screenshot of the console.

Anyways, Thanks again.
Jean.

Attachments Java_-_Race_[Solved].JPG 206.68 KB
This article has been dead for over six months. Start a new discussion instead.