Results 1 to 6 of 6
  1. #1
    Chygoz is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Simulating a car race

    I'm still new to java and i've been working on a program that simulates a car race. I input 5 different race car names from the command prompt as well as the number of laps in the race. I created a class that extends Thread. Within the run() method of this class, i generate a random number between 1 and 6000 to serve as the delay in miliseconds that a thread will sleep, corresponding to delays that each race car will encounter. The race is simulated on the command prompt by displaying the name of the race cars and what lap each of them currently is. Everything is working properly. But the challenge which i haven't been able to accomplish is that i want to create a text file which records the winner of the race. Each time the program is run, a new winner should be added to the text file. Here are the codes to simulate the car race:

    For the thread class:
    public class RaceCar extends Thread
    {
    private int finish;
    private String name;

    public RaceCar(int finish, String name)
    {
    this.finish = finish;
    this.name = name;
    }

    public void run()
    {
    for(int i = 1; i <= finish; i++)
    {
    int time = (int) (Math.random() * 6000);
    try
    {
    Thread.sleep(time);
    }catch(InterruptedException m)
    {}
    System.out.println(name + " " + i);
    }
    System.out.println(name + " finished!");
    }
    }

    for the main program
    public class Race
    {
    public static void main(String [] args)
    {
    Thread [] cars = new Thread[5];
    int laps = Integer.parseInt(args[5]);

    for(int i = 0; i < cars.length; i++)
    {
    cars[i] = new RaceCar(laps, args[i]);
    }

    for(int i = 0; i < cars.length; i++)
    {
    cars[i].start();
    }
    }
    }

    Any help is appreciated.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Simulating a car race

    i want to create a text file which records the winner
    What have you done to write the file?
    There are many code samples on the forum that write to files. Search for FileWriter for example

  3. #3
    Chygoz is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Simulating a car race

    Quote Originally Posted by Norm View Post
    What have you done to write the file?
    There are many code samples on the forum that write to files. Search for FileWriter for example
    I've used FileWriter. I included it within the run() method of the RaceCar class. The problem is that it creates the text file alright or rather 5 text files with the same name which leads to the last file overwriting the previously created ones if you understand what i mean. So in the end the name that gets written to the text file becomes that of the race car that finished last which isn't what i need. I just need someone to give me an idea of how to go about it so that the overwriting doesn't occur.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Simulating a car race

    so that the overwriting doesn't occur.
    Look at the API doc for the FileWriter class. Its constructor has a parameter for appending to the end of the file.

  5. #5
    Chygoz is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Simulating a car race

    Quote Originally Posted by Norm View Post
    Look at the API doc for the FileWriter class. Its constructor has a parameter for appending to the end of the file.
    Thanks a lot. I've looked it up and it's working just how i want it to. I'm grateful for your help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

Similar Threads

  1. simulating mouseRelease for JLabel
    By Michitoshi in forum AWT / Swing
    Replies: 14
    Last Post: 01-02-2012, 11:20 PM
  2. Simulating an elevator
    By intrepid604 in forum New To Java
    Replies: 4
    Last Post: 06-05-2011, 10:43 AM
  3. Security hole by Race condition
    By simorgh in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-24-2010, 04:10 AM
  4. Simple Text race game
    By Inf1nity in forum New To Java
    Replies: 2
    Last Post: 03-16-2009, 03:46 PM
  5. simulating netstat
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-14-2009, 07:41 AM

Posting Permissions

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