Sprint Race simulation

Trying to create a simulation of a sprint race

Page 1 of 1

9 Replies - 1437 Views - Last Post: 08 November 2010 - 03:06 PM Rate Topic: -----

#1 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Sprint Race simulation

Posted 19 October 2010 - 08:43 AM

Hi,

Im new to java, mostly done networking in the past, im trying to create a program that will run a basic simulation of a number of heats for a 100m sprint. It mostly about the use of collections so nothing to complex but im completly lost.

I've created a number of runner objects and stored them in 2 arraylists called race and race2, I've also set the times to zero for each runner. What im trying to do right now is run through the objects in the arraylist and randomise the time for each runner within 9 to 10 secs in order to give a sort of realistic time for the race. Then I want to sort them by time first to last and pull the top 3 out of the arraylists and store them in another arraylist call winners. I've been thinking I have to use a loop and them set the time with a setTime method for each object for sorting them by time but im completly lost and have basically started the whole thing again just with the objects stored in the arraylist.

Any help or suggestions would be very gratfully recieved
Thanks
This is my main class and runner class

import java.util.ArrayList;
import java.io.*;

public class RaceSim{ 
	
	public static void main(String[] args){
				
        Runner p1 = new Runner("Mark", "Brady", 00.00);
	Runner p2 = new Runner("Karl", "Collins", 00.00);
	Runner p3 = new Runner("Brian","Morgan", 00.00);
	Runner p4 = new Runner("Stephen","Fryer", 00.00);
	Runner p5 = new Runner("Stephen","Taylor", 00.00);
	Runner p6 = new Runner("Gavin","Burke", 00.00);
	Runner p7 = new Runner("John","McGlynn", 00.00);
	Runner p8 = new Runner("Robert","Banks", 00.00);
						
	ArrayList<Runner> race = new ArrayList<Runner>(};
        race.add(p1);
	race.add(p2};
        race.add(p3);
	race.add(p4);
	race.add(p5);
	race.add(p6);
	race.add(p7);
	race.add(p8);
				        
	Runner p9 = new Runner("Aaron", "McNutt", 00.00);
        Runner p10 = new Runner("Kevin", "McCafferty", 00.00);
	Runner p11 = new Runner("Harold","McCarron", 00.00);
	Runner p12 = new Runner("Declan","McCormack", 00.00);
	Runner p13 = new Runner("Micheal","McGoldrick", 00.00);
	Runner p14 = new Runner("David","Hegerty", 00.00);
	Runner p15 = new Runner("Ryan","Lynce", 00.00);
	Runner p16 = new Runner("Conal","Murphy", 00.00);
						
	ArrayList<Runner> race2 = new ArrayList<Runner>();
	race2.add(p9);
	race2.add(p10);
	race2.add(p11);
	race2.add(p12);
	race2.add(p13);
	race2.add(p14);
	race2.add(p15);
	race2.add(p16);
	System.out.println(race + "\n");
	System.out.println(race2 + "\n");
			
				  
	}
}

public class Runner {
	    private String fName;
	    private String sName;
	    private double time;
	 
	    public Runner(){
	    }
	    
	    public Runner(String fName, String sName, double time){
	        this.fName = fName;
	        this.sName = sName;
	        this.time = time;
	    }
	 
	    public double getTime(double time){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }
	 
	   
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sprint Race simulation

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Sprint Race simulation

Posted 19 October 2010 - 11:50 AM

Well, here's one way to add a random number generator to your project:

...
public class RaceSim {

    private static Random ranGen = new Random();

    public static void main(String[] args) {
...
        Runner p1 = new Runner("Mark", "Brady", genRandomTime());
...
    }

// Generates random 'times' between 9 and 10 seconds
    public static double genRandomTime() {
        return ranGen.nextDouble() + 9;
    }
...



See Class Random

In your Runner Class you don't want to pass a time value to the getTime() method:

public double getTime() {
    return time;
}


What you currently have ignores the time assigned to that runner.

This post has been edited by n8wxs: 19 October 2010 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Re: Sprint Race simulation

Posted 19 October 2010 - 03:05 PM

Hi,

Thanks for the tip on that, it was driving me nuts, i'll add this to what I was doing today, just got to figure out how to sreah and sort be the number.

cheers
Was This Post Helpful? 0
  • +
  • -

#4 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Re: Sprint Race simulation

Posted 20 October 2010 - 12:00 PM

View Postn8wxs, on 19 October 2010 - 10:50 AM, said:

Well, here's one way to add a random number generator to your project:

...
public class RaceSim {

    private static Random ranGen = new Random();

    public static void main(String[] args) {
...
        Runner p1 = new Runner("Mark", "Brady", genRandomTime());
...
    }

// Generates random 'times' between 9 and 10 seconds
    public static double genRandomTime() {
        return ranGen.nextDouble() + 9;
    }
...



See Class Random

In your Runner Class you don't want to pass a time value to the getTime() method:

public double getTime() {
    return time;
}


What you currently have ignores the time assigned to that runner.


Hi,

I've added this to my code now, and im getting output like this

[Runner@c3c749, Runner@150bd4d, Runner@1bc4459, Runner@12b6651, Runner@4a5ab2, Runner@1888759, Runner@6e1408, Runner@e53108]

[Runner@f62373, Runner@19189e1, Runner@1f33675, Runner@7c6768, Runner@1690726, Runner@5483cd, Runner@9931f5, Runner@19ee1ac]
Any ideas?
Im also working on the searching and sorting and wil post later
cheers
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Sprint Race simulation

Posted 20 October 2010 - 12:13 PM

That's because you are printing the Runner objects contained in the ArrayList. The Runner object inherits the base class Object's toString() method if your class does not override it. The object's address is printed by default.

Here's one way to output the contents:

...
System.out.println(race + "\n");
System.out.println(race2 + "\n");

for (int i = 0; i < race.size(); i++) {
    System.out.printf("%s = %2.2f seconds\n", race.get(i).getName(), race.get(i).getTime());
}
...



Output:

run:
[racesim.Runner@42e816, racesim.Runner@9304b1, racesim.Runner@190d11, racesim.Runner@a90653, racesim.Runner@de6ced, racesim.Runner@c17164, racesim.Runner@1fb8ee3, racesim.Runner@61de33]

[racesim.Runner@14318bb, racesim.Runner@ca0b6, racesim.Runner@10b30a7, racesim.Runner@1a758cb, racesim.Runner@1b67f74, racesim.Runner@69b332, racesim.Runner@173a10f, racesim.Runner@530daa]

Mark Brady = 9.95 seconds
Karl Collins = 9.01 seconds
Brian Morgan = 9.97 seconds
Stephen Fryer = 9.51 seconds
Stephen Taylor = 9.35 seconds
Gavin Burke = 9.80 seconds
John McGlynn = 9.53 seconds
Robert Banks = 9.42 seconds
BUILD SUCCESSFUL (total time: 0 seconds)


This post has been edited by n8wxs: 20 October 2010 - 12:18 PM

Was This Post Helpful? 1
  • +
  • -

#6 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Re: Sprint Race simulation

Posted 20 October 2010 - 04:46 PM

View Postn8wxs, on 20 October 2010 - 11:13 AM, said:

That's because you are printing the Runner objects contained in the ArrayList. The Runner object inherits the base class Object's toString() method if your class does not override it. The object's address is printed by default.

Here's one way to output the contents:

...
System.out.println(race + "\n");
System.out.println(race2 + "\n");

for (int i = 0; i < race.size(); i++) {
    System.out.printf("%s = %2.2f seconds\n", race.get(i).getName(), race.get(i).getTime());
}
...



Output:

run:
[racesim.Runner@42e816, racesim.Runner@9304b1, racesim.Runner@190d11, racesim.Runner@a90653, racesim.Runner@de6ced, racesim.Runner@c17164, racesim.Runner@1fb8ee3, racesim.Runner@61de33]

[racesim.Runner@14318bb, racesim.Runner@ca0b6, racesim.Runner@10b30a7, racesim.Runner@1a758cb, racesim.Runner@1b67f74, racesim.Runner@69b332, racesim.Runner@173a10f, racesim.Runner@530daa]

Mark Brady = 9.95 seconds
Karl Collins = 9.01 seconds
Brian Morgan = 9.97 seconds
Stephen Fryer = 9.51 seconds
Stephen Taylor = 9.35 seconds
Gavin Burke = 9.80 seconds
John McGlynn = 9.53 seconds
Robert Banks = 9.42 seconds
BUILD SUCCESSFUL (total time: 0 seconds)



Thanks

i've implemented your changes and added a little code for the print statement and for my methods in the runner class and it is now printing out all the names with the random times.

next im going to try and sort them by time so i can pick out the 3 fastest runners and store them in another array. cheers
import java.util.ArrayList;
import java.util.Random;
import java.io.*;


public class RaceSim{ 

	

	public static void main(String[] args){
						
        Runner p1 = new Runner("Mark", "Brady", genRandomTime());
	Runner p2 = new Runner("Karl", "Collins", genRandomTime());
	Runner p3 = new Runner("Brian","Morgan", genRandomTime());
	Runner p4 = new Runner("Stephen","Fryer", genRandomTime());
	Runner p5 = new Runner("Stephen","Taylor", genRandomTime());
	Runner p6 = new Runner("Gavin","Burke", genRandomTime());
	Runner p7 = new Runner("John","McGlynn", genRandomTime());
	Runner p8 = new Runner("Robert","Banks", genRandomTime());
						
	ArrayList<Runner> race = new ArrayList<Runner>();
	race.add(p1);
	race.add(p2);
	race.add(p3);
	race.add(p4);
	race.add(p5);
	race.add(p6);
	race.add(p7);
        race.add(p8);
				        
	Runner p9 = new Runner("Aaron", "McNutt", genRandomTime());
	Runner p10 = new Runner("Kevin", "McCafferty", genRandomTime());
	Runner p11 = new Runner("Harold","McCarron", genRandomTime());
	Runner p12 = new Runner("Declan","McCormack", genRandomTime());
	Runner p13 = new Runner("Micheal","McGoldrick", genRandomTime());
	Runner p14 = new Runner("David","Hegerty", genRandomTime());
	Runner p15 = new Runner("Ryan","Lynce", genRandomTime());
	Runner p16 = new Runner("Conal","Murphy", genRandomTime());
						
	ArrayList<Runner> race2 = new ArrayList<Runner>();
	race2.add(p9);
	race2.add(p10);
	race2.add(p11);
	race2.add(p12);
	race2.add(p13);
	race2.add(p14);
	race2.add(p15);
	race2.add(p16);
				        
				               
				        
				        
	System.out.println(race + "\n");
       System.out.println(race2 + "\n");
				       
	for (int i = 0; i < race.size(); i++) {
		System.out.printf("%s %s %2.2f seconds\n", race.get(i).getfName(), race.get(i).getsName(), race.get(i).getTime());
}
				       
for (int i = 0; i < race2.size(); i++) {
System.out.printf("%s %s %2.2f seconds\n", race2.get(i).getfName(), race2.get(i).getsName(), race2.get(i).getTime()); 
}
			   
}

static Random ranGen = new Random();	       	 	    	       
public static double genRandomTime() {
	
	 return ranGen.nextDouble() + 9;
}
}

public class Runner {
	    private String fName;
	    private String sName;
	    private double time;
	 
	    public Runner(){
	    }
	    
	    public Runner(String fName, String sName, double time){
	        this.fName = fName;
	        this.sName = sName;
	        this.time = time;
	    }
	 
	    public double getTime(){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }

		public String getfName() {
			
			return fName;
		}
		
	public String getsName() {
			
			return sName;
		}

	
	 
	   
}
 

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sprint Race simulation

Posted 20 October 2010 - 10:08 PM

About time you learn about arrays... will save you a lot of coding lines

public class RaceSim{ 

	static String[] firstName = {"Mark", "Karl", "Brian", "Stephen", "Stephen"};
	static String[] lastName = {"Brady", "Collins", "Morgan", "Fryer", "Burke"};

	static String[] fName2 = {"Aaron", "Kevin", "Harold"};
	static String[] lName2 = {"McNutt", "McCafferty", "McCarron"};
	
	public static void main(String[] args){
		Runner[] r = new Runner[firstName.length];
		ArrayList<Runner> race = new ArrayList<Runner>();
		for(int i = 0; i < firstName.length; i++) {
			r[i] = new Runner(firstName[i], lastName[i], genRandomTime());
			race.add(r[i]);
		}
		ArrayList<Runner> race2 = new ArrayList<Runner>();
		Runner[] p = new Runner[fName2.length];
		for(int i = 0; i < fName2.length; i++) {
			p[i] = new Runner(fName2[i], lName2[i], genRandomTime());
			race2.add(p[i]);
		}


Was This Post Helpful? 1
  • +
  • -

#8 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Re: Sprint Race simulation

Posted 26 October 2010 - 10:18 AM

I've been working on the problem for the last few days and have come up with the following code, i can now fill 2 heats with runners and pick out the 4 fastest times generated randomly, but i am having trouble understanding how I can now move these runners into the next round and heat of the race and put them into the correct lanes based on their times. example: the fastest goes into lane 3 next fastest in lane 4, next fastest in lane 2 etc. I will evenually add more heats to the first round to create an entire race from first round till final.

Any help would be great
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import java.io.*;

public class RaceSim {

	// static String[] firstName = {"Mark", "Karl", "Brian", "Stephen",
	// "Stephen", "Gavin", "John", "Robert"};
	// static String[] lastName = {"Brady", "Collins", "Morgan", "Fryer",
	// "Taylor", "Burke", "McGlynn", "Banks"};

	// static String[] fName2 = {"Aaron", "Kevin", "Harold", "Declan",
	// "Micheal", "David", "Ryan", "Conal" };
	// static String[] lName2 = {"McNutt", "McCafferty", "McCarron",
	// "McCormack", "Rutherford", "Hegerty", "Semple", "Murphy"};

	// static ArrayList<Runner> race = new ArrayList<Runner>();

	public static void main(String[] args) {

		Runner runnerOne = new Runner("mark", "gallagher", 0.00);
		Runner runnerTwo = new Runner("andrew", "creighton", 0.00);
		Runner runnerThree = new Runner("karl", "gallagher", 0.00);
		Runner runnerFour = new Runner("louise", "creighton", 0.00);
		Runner runnerFive = new Runner("Ian", "Morgan", 0.00);
		Runner runnerSix = new Runner("Kevin", "McCormack", 0.00);
		Runner runnerSeven = new Runner("Aaron", "McNutt", 0.00);
		Runner runnerEight = new Runner("Stephen", "Fryer", 0.00);

		Heat heat1 = new Heat1();

		heat1.addRunner(runnerOne);
		heat1.addRunner(runnerTwo);
		heat1.addRunner(runnerThree);
		heat1.addRunner(runnerFour);
		heat1.addRunner(runnerFive);
		heat1.addRunner(runnerSix);
		heat1.addRunner(runnerSeven);
		heat1.addRunner(runnerEight);

		Heat heat2 = new Heat2();

		runnerOne = new Runner("1", "2", 0.00);
		runnerTwo = new Runner("2", "2", 0.00);
		runnerThree = new Runner("3", "2", 0.00);
		runnerFour = new Runner("4", "2", 0.00);
		runnerFive = new Runner("5", "2", 0.00);
		runnerSix = new Runner("6", "2", 0.00);
		runnerSeven = new Runner("7", "2", 0.00);
		runnerEight = new Runner("8", "2", 0.00);

		heat2.addRunner(runnerOne);
		heat2.addRunner(runnerTwo);
		heat2.addRunner(runnerThree);
		heat2.addRunner(runnerFour);
		heat2.addRunner(runnerFive);
		heat2.addRunner(runnerSix);
		heat2.addRunner(runnerSeven);
		heat2.addRunner(runnerEight);

		Round round1 = new Round1();
		round1.run(heat1);
		round1.run(heat2);
		round1.addWinners(heat1, heat2);

		System.out.println(round1.displayWinners());

		/*
		 * Runner[] r = new Runner[firstName.length];
		 * 
		 * for(int i = 0; i < firstName.length; i++) { r[i] = new
		 * Runner(firstName[i], lastName[i], genRandomTime()); race.add(r[i]); }
		 * 
		 * 
		 * 
		 * ArrayList<Runner> race2 = new ArrayList<Runner>(); Runner[] p = new
		 * Runner[fName2.length]; for(int i = 0; i < fName2.length; i++) { p[i] =
		 * new Runner(fName2[i], lName2[i], genRandomTime()); race2.add(p[i]); }
		 * 
		 * Collections.sort(race);
		 * 
		 * int count=0; Iterator iter =race.iterator();
		 * 
		 * while(iter.hasNext()&&count!=3){
		 * 
		 * Runner r2 =(Runner)iter.next();
		 * 
		 * System.out.println(r2);
		 * 
		 * count+=1; }
		 * 
		 * for (int i = 0; i < race.size(); i++) { //System.out.printf("%s %s
		 * %2.2f seconds\n", race.get(i).getfName(), race.get(i).getsName(),
		 * race.get(i).getTime()); } // System.out.println("\n"); // for (int i =
		 * 0; i < race2.size(); i++) { //System.out.printf("%s %s %2.2f
		 * seconds\n", race2.get(i).getfName(), race2.get(i).getsName(),
		 * race2.get(i).getTime()); //}
		 * 
		 *  // getBestRunners();
		 *  }
		 * 
		 * static Random ranGen = new Random(); public static double
		 * genRandomTime() {
		 * 
		 * return ranGen.nextDouble() + 9; }
		 */

	}
}

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;

public abstract class Round {

	private ArrayList winners = new ArrayList();

	public ArrayList getWinners() {

		return winners;

	}

	protected String name;

	public String getName() {
		return name;
	}

	public Round() {

	}

	
	public void addWinners(Heat heat1, Heat heat2) {

		// sort collection

		Collections.sort(heat1.getRunners());
		Collections.sort(heat2.getRunners());

		int count = 0;

		Iterator iter = heat1.getRunners().iterator();

		while (iter.hasNext() && count != 4) {

			Runner runner = (Runner) iter.next();

			getWinners().add(runner);
			
			count += 1;
		}
		 count = 0;
		 
		iter = heat2.getRunners().iterator();

		while (iter.hasNext() && count != 4) {

			Runner runner = (Runner) iter.next();

			getWinners().add(runner);
			
			count += 1;
		}

	}
	
	public  void run(Heat heat) {

		Iterator iter = heat.getRunners().iterator();

		while (iter.hasNext()) {

			Runner runner = (Runner) iter.next();

			runner.setTime(createRandomTime());
		}
	}
	public String displayWinners(){
		
		System.out.println(name);
		Iterator iter = winners.iterator();
		String result="";
		while(iter.hasNext()){
			
			Runner runner =(Runner)iter.next();
			
			
			result+=runner+"\n";
			
		}
		
		return result;
	}

	public static double createRandomTime() {

		Random ranGen = new Random();
		return ranGen.nextDouble() + 9;
	}

	

	public String toString(){
		
		return name;
	}
}
import java.util.ArrayList;
import java.util.Iterator;


public abstract class Heat {

	private ArrayList runners = new ArrayList();
	
	public ArrayList getRunners(){
		
		return runners;
	}
	
	protected  String name;
	
	public String getName(){
		
		return name;
	}

	public Heat(){
		
		
	}
		
	public void addRunner(Runner runner){
		
		runners.add(runner);
		
		
	}
	
	
    public String displayRunners(){
		
		Iterator iter = runners.iterator();
		
		String result="";
		while(iter.hasNext()){
			
			Runner runner =(Runner)iter.next();
			
			
			result+=runner+"\n";
			
		}
		return result;
	}
	
	
	
	public String toString(){
		
		return name;
	}
	
}

public class Runner  implements Comparable{
	
	    private String fName;
	    private String sName;
		private double time;
	 
	    public Runner(){
	    	
	    }
	    
	    public Runner(String fName, String sName, double time){
	        this.fName = fName;
	        this.sName = sName;
	        this.time = time;
	    }
	 
	    public double getTime(){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }

		public String getfName() {
			
			return fName;
		}
		
	public String getsName() {
			
			return sName;
		}

	
public String toString(){
	
	return fName+" "+sName+" "+time;
}
	
	 public int compareTo(Object obj) {
			// TODO Auto-generated method stub
	    	
	    	Runner r = (Runner)obj;
	    	
	    	
	    	if(this.time<r.getTime()){
	    		return 0;
	    	}else if(this.time>r.getTime()){
	    		
	    		return 1;
	    	}else{
	    		
	    		return -1;
	    	}
	    	
		}
	   
}
import java.util.Collections;
import java.util.Iterator;


public final class Round1 extends Round{

	
	public Round1(){
		
		this.name="Round1";
	}
	
	
}


public final class Heat1 extends Heat{

	public Heat1() {
		this.name="Heat1";
		// TODO Auto-generated constructor stub
	}

}

public final class Heat2 extends Heat{
	
	public Heat2(){
		
		this.name="Heat2";
	}

}



Was This Post Helpful? 0
  • +
  • -

#9 gall1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-October 10

Re: Sprint Race simulation

Posted 08 November 2010 - 12:56 PM

Hi,

I now have the race running from the first round till the final with all the heats, but im looking to find a more elegant way of firstly entering all the runners without having to hard code them at the beginning and also to find a way to place the winners into specific lanes before running the race unlik now where they are placed in any lane,

Any help would be great

Cheers
public class RaceSim {

	// static String[] firstName = {"Mark", "Karl", "Brian", "Stephen",
	// "Stephen", "Gavin", "John", "Robert"};
	// static String[] lastName = {"Brady", "Collins", "Morgan", "Fryer",
	// "Taylor", "Burke", "McGlynn", "Banks"};

	// static String[] fName2 = {"Aaron", "Kevin", "Harold", "Declan",
	// "Micheal", "David", "Ryan", "Conal" };
	// static String[] lName2 = {"McNutt", "McCafferty", "McCarron",
	// "McCormack", "Rutherford", "Hegerty", "Semple", "Murphy"};

	// static ArrayList<Runner> race = new ArrayList<Runner>();

	public static void main(String[] args) {
        //Runners for the first heats
		Runner runner1 = new Runner("Mark", "Gallagher", 0.00);
		Runner runner2 = new Runner("Andrew", "Creighton ", 0.00);
		Runner runner3 = new Runner("Karl", "Gallagher", 0.00);
		Runner runner4 = new Runner("David", "Simpson", 0.00);
		Runner runner5 = new Runner("Ian", "Morgan", 0.00);
		Runner runner6 = new Runner("Kevin", "McCormack", 0.00);
		Runner runner7 = new Runner("Aaron", "McNutt", 0.00);
		Runner runner8 = new Runner("Stephen", "Fryer", 0.00);
		
		
		Runner runner9 = new Runner("James", "Lewis", 0.00);
		Runner runner10 = new Runner("Colin", "Spence", 0.00);
		Runner runner11 = new Runner("Paul", "McCallion", 0.00);
		Runner runner12 = new Runner("Ryan", "McAllister", 0.00);
		Runner runner13 = new Runner("Richard", "Stevens", 0.00);
		Runner runner14 = new Runner("Richard", "Banks", 0.00);
		Runner runner15 = new Runner("Robert", "Grant", 0.00);
		Runner runner16 = new Runner("Steven", "Winchester", 0.00);
		
		Runner runner17 = new Runner("1", "1", 0.00);
		Runner runner18 = new Runner("1", "2", 0.00);
		Runner runner19 = new Runner("1", "3", 0.00);
		Runner runner20 = new Runner("1", "4", 0.00);
		Runner runner21 = new Runner("1", "5", 0.00);
		Runner runner22 = new Runner("1", "6", 0.00);
		Runner runner23 = new Runner("1", "7", 0.00);
		Runner runner24 = new Runner("1", "8", 0.00);
		
		Runner runner25 = new Runner("1", "9", 0.00);
		Runner runner26 = new Runner("1", "10", 0.00);
		Runner runner27 = new Runner("1", "11", 0.00);
		Runner runner28 = new Runner("1", "12", 0.00);
		Runner runner29 = new Runner("1", "13", 0.00);
		Runner runner30 = new Runner("1", "14", 0.00);
		Runner runner31 = new Runner("1", "15", 0.00);
		Runner runner32 = new Runner("1", "16", 0.00);
		
		Runner runner33 = new Runner("1", "17", 0.00);
		Runner runner34 = new Runner("1", "18", 0.00);
		Runner runner35 = new Runner("1", "19", 0.00);
		Runner runner36 = new Runner("1", "20", 0.00);
		Runner runner37 = new Runner("1", "21", 0.00);
		Runner runner38 = new Runner("1", "22", 0.00);
		Runner runner39 = new Runner("1", "23", 0.00);
		Runner runner40 = new Runner("1", "24", 0.00);
		
		Runner runner41 = new Runner("1", "25", 0.00);
		Runner runner42 = new Runner("1", "26", 0.00);
		Runner runner43 = new Runner("1", "27", 0.00);
		Runner runner44 = new Runner("1", "28", 0.00);
		Runner runner45 = new Runner("1", "29", 0.00);
		Runner runner46 = new Runner("1", "30", 0.00);
		Runner runner47 = new Runner("1", "31", 0.00);
		Runner runner48 = new Runner("1", "32", 0.00);
		
		Runner runner49 = new Runner("1", "33", 0.00);
		Runner runner50 = new Runner("1", "34", 0.00);
		Runner runner51 = new Runner("1", "35", 0.00);
		Runner runner52 = new Runner("1", "36", 0.00);
		Runner runner53 = new Runner("1", "37", 0.00);
		Runner runner54 = new Runner("1", "38", 0.00);
		Runner runner55 = new Runner("1", "39", 0.00);
		Runner runner56 = new Runner("1", "40", 0.00);
		
		Runner runner57 = new Runner("1", "41", 0.00);
		Runner runner58 = new Runner("1", "42", 0.00);
		Runner runner59 = new Runner("1", "43", 0.00);
		Runner runner60 = new Runner("1", "44", 0.00);
		Runner runner61 = new Runner("1", "45", 0.00);
		Runner runner62 = new Runner("1", "46", 0.00);
		Runner runner63 = new Runner("1", "47", 0.00);
		Runner runner64 = new Runner("1", "48", 0.00);
		
		
        //Creating 8 heats
		Heat heat1Round1 = new Heat1();
		Heat heat2Round1 = new Heat2();
		Heat heat3Round1 = new Heat3();
		Heat heat4Round1 = new Heat4();
		Heat heat5Round1 = new Heat5();
		Heat heat6Round1 = new Heat6();
		Heat heat7Round1 = new Heat7();
		Heat heat8Round1 = new Heat8();
		
		//adding runners to first 2 heats
		heat1Round1.addRunner(runner1);
		heat1Round1.addRunner(runner2);
		heat1Round1.addRunner(runner3);
		heat1Round1.addRunner(runner4);
		heat1Round1.addRunner(runner5);
		heat1Round1.addRunner(runner6);
		heat1Round1.addRunner(runner7);
		heat1Round1.addRunner(runner8);
		
		heat2Round1.addRunner(runner9);
		heat2Round1.addRunner(runner10);
		heat2Round1.addRunner(runner11);
		heat2Round1.addRunner(runner12);
		heat2Round1.addRunner(runner13);
		heat2Round1.addRunner(runner14);
		heat2Round1.addRunner(runner15);
		heat2Round1.addRunner(runner16);
		
		heat3Round1.addRunner(runner17);
		heat3Round1.addRunner(runner18);
		heat3Round1.addRunner(runner19);
		heat3Round1.addRunner(runner20);
		heat3Round1.addRunner(runner21);
		heat3Round1.addRunner(runner22);
		heat3Round1.addRunner(runner23);
		heat3Round1.addRunner(runner24);
		
		heat4Round1.addRunner(runner25);
		heat4Round1.addRunner(runner26);
		heat4Round1.addRunner(runner27);
		heat4Round1.addRunner(runner28);
		heat4Round1.addRunner(runner29);
		heat4Round1.addRunner(runner30);
		heat4Round1.addRunner(runner31);
		heat4Round1.addRunner(runner32);
		
		heat5Round1.addRunner(runner33);
		heat5Round1.addRunner(runner34);
		heat5Round1.addRunner(runner35);
		heat5Round1.addRunner(runner36);
		heat5Round1.addRunner(runner37);
		heat5Round1.addRunner(runner38);
		heat5Round1.addRunner(runner39);
		heat5Round1.addRunner(runner40);
		
		heat6Round1.addRunner(runner41);
		heat6Round1.addRunner(runner42);
		heat6Round1.addRunner(runner43);
		heat6Round1.addRunner(runner44);
		heat6Round1.addRunner(runner45);
		heat6Round1.addRunner(runner46);
		heat6Round1.addRunner(runner47);
		heat6Round1.addRunner(runner48);
		
		heat7Round1.addRunner(runner49);
		heat7Round1.addRunner(runner50);
		heat7Round1.addRunner(runner51);
		heat7Round1.addRunner(runner52);
		heat7Round1.addRunner(runner53);
		heat7Round1.addRunner(runner54);
		heat7Round1.addRunner(runner55);
		heat7Round1.addRunner(runner56);
		
		heat8Round1.addRunner(runner57);
		heat8Round1.addRunner(runner58);
		heat8Round1.addRunner(runner59);
		heat8Round1.addRunner(runner60);
		heat8Round1.addRunner(runner61);
		heat8Round1.addRunner(runner62);
		heat8Round1.addRunner(runner63);
		heat8Round1.addRunner(runner64);
		
        
		//running first 2 heats
		heat1Round1.runRace();
		heat2Round1.runRace();
		heat3Round1.runRace();
		heat4Round1.runRace();
		heat5Round1.runRace();
		heat6Round1.runRace();
		heat7Round1.runRace();
		heat8Round1.runRace();
		
		
		//Finding winners for heats
		heat1Round1.getWinnersFromHeat();
		heat2Round1.getWinnersFromHeat();
		heat3Round1.getWinnersFromHeat();
		heat4Round1.getWinnersFromHeat();
		heat5Round1.getWinnersFromHeat();
		heat6Round1.getWinnersFromHeat();
		heat7Round1.getWinnersFromHeat();
		heat8Round1.getWinnersFromHeat();
		
		//creating first round
		Round round1 = new Round1();
		//add heats to round
		round1.addHeat(heat1Round1);
		round1.addHeat(heat2Round1);
		round1.addHeat(heat3Round1);
		round1.addHeat(heat4Round1);
		round1.addHeat(heat5Round1);
		round1.addHeat(heat6Round1);
		round1.addHeat(heat7Round1);
		round1.addHeat(heat8Round1);
		
		round1.displayHeats();

		
		//creating heats for round2
		Heat heat1Round2 =new Heat1();
		Heat heat2Round2 =new Heat2();
		Heat heat3Round2 =new Heat3();
		Heat heat4Round2 =new Heat4();
		//getting previous winners form heats from round 1	
		heat1Round2.getWinnersFromPreviousRound(heat1Round1);
		heat1Round2.getWinnersFromPreviousRound(heat2Round1);
		heat2Round2.getWinnersFromPreviousRound(heat3Round1);
		heat2Round2.getWinnersFromPreviousRound(heat4Round1);
		heat3Round2.getWinnersFromPreviousRound(heat5Round1);
		heat3Round2.getWinnersFromPreviousRound(heat6Round1);
		heat4Round2.getWinnersFromPreviousRound(heat7Round1);
		heat4Round2.getWinnersFromPreviousRound(heat8Round1);
		
		//running race with combined winners of first heats from round 1
		heat1Round2.runRace();
		heat2Round2.runRace();
		heat3Round2.runRace();
		heat4Round2.runRace();
		
		//getting winners
		heat1Round2.getWinnersFromHeat();
		heat2Round2.getWinnersFromHeat();
		heat3Round2.getWinnersFromHeat();
		heat4Round2.getWinnersFromHeat();
		
		//creating round 2
		Round round2 = new Round2();
		//adding heat 1 to round 2
		round2.addHeat(heat1Round2);
		round2.addHeat(heat2Round2);
		round2.addHeat(heat3Round2);
		round2.addHeat(heat4Round2);
		//displaying heat 1 round 2 
		round2.displayHeats();

		Heat semiFinal1 =new Heat1();
		Heat semiFinal2 =new Heat2();
		
		semiFinal1.getWinnersFromPreviousRound(heat1Round2);
		semiFinal1.getWinnersFromPreviousRound(heat2Round2);
		semiFinal2.getWinnersFromPreviousRound(heat3Round2);
		semiFinal2.getWinnersFromPreviousRound(heat4Round2);
	
		semiFinal1.runRace();
		semiFinal2.runRace();
		
		semiFinal1.getWinnersFromHeat();
		semiFinal2.getWinnersFromHeat();
		
		Round semiFinal = new semiFinal();
	
		semiFinal.addHeat(semiFinal1);
		semiFinal.addHeat(semiFinal2);
		
		
		semiFinal.displayHeats();
		
		
		Heat Final =new Heat1();
	
		
		Final.getWinnersFromPreviousRound(semiFinal1);
		Final.getWinnersFromPreviousRound(semiFinal2);
		
		Final.runRace();
		
		
		Final.getWinnersFromFinal();
		
		Round round4 = new Round4();
		
		round4.addHeat(Final);
				
		round4.displayHeats();
		
		/*
		 * Runner[] r = new Runner[firstName.length];
		 * 
		 * for(int i = 0; i < firstName.length; i++) { r[i] = new
		 * Runner(firstName[i], lastName[i], genRandomTime()); race.add(r[i]); }
		 * 
		 * 
		 * 
		 * ArrayList<Runner> race2 = new ArrayList<Runner>(); Runner[] p = new
		 * Runner[fName2.length]; for(int i = 0; i < fName2.length; i++) { p[i] =
		 * new Runner(fName2[i], lName2[i], genRandomTime()); race2.add(p[i]); }
		 * 
		 * Collections.sort(race);
		 * 
		 * int count=0; Iterator iter =race.iterator();
		 * 
		 * while(iter.hasNext()&&count!=3){
		 * 
		 * Runner r2 =(Runner)iter.next();
		 * 
		 * System.out.println(r2);
		 * 
		 * count+=1; }
		 * 
		 * for (int i = 0; i < race.size(); i++) { //System.out.printf("%s %s
		 * %2.2f seconds\n", race.get(i).getfName(), race.get(i).getsName(),
		 * race.get(i).getTime()); } // System.out.println("\n"); // for (int i =
		 * 0; i < race2.size(); i++) { //System.out.printf("%s %s %2.2f
		 * seconds\n", race2.get(i).getfName(), race2.get(i).getsName(),
		 * race2.get(i).getTime()); //}
		 *  // getBestRunners(); }
		 * 
		 * static Random ranGen = new Random(); public static double
		 * genRandomTime() {
		 * 
		 * return ranGen.nextDouble() + 9; }
		 */

	}
}
public class Runner  implements Comparable{
	
	    private String fName;
	    private String sName;
		private double time;
	 
	    public Runner(){
	    	
	    }
	    
	    public Runner(String fName, String sName, double time){
	        this.fName = fName;
	        this.sName = sName;
	        this.time = time;
	    }
	 
	    public double getTime(){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }

		public String getfName() {
			
			return fName;
		}
		
	public String getsName() {
			
			return sName;
		}

	
public String toString(){
	
	return fName+" "+sName+" "+time;
}
	
	 public int compareTo(Object obj) {
			// TODO Auto-generated method stub
	    	
	    	Runner r = (Runner)obj;
	    	
	    	
	    	if(this.time<r.getTime()){
	    		return 0;
	    	}else if(this.time>r.getTime()){
	    		
	    		return 1;
	    	}else{
	    		
	    		return -1;
	    	}
	    	
		}
	   
}
import java.util.ArrayList;
import java.util.Iterator;


public abstract class Round {

	private ArrayList heats = new ArrayList();

	protected String name;

	public String getName() {
		return name;
	}
	
	public Round() {}
	
	public void addHeat(Heat heat) {
		heats.add(heat);
	}

	public String displayHeats(){
		
		Iterator iter =heats.iterator();
		
		String result="";
		
		while(iter.hasNext()){
			
			Heat aHeat=(Heat)iter.next();
			
		
			System.out.println(aHeat.name +this.name +"\n"+ aHeat.getRunners().toString());


			System.out.println(aHeat.name+this.name +"\nWinners"+ aHeat.getWinners());
		}
		return result;
	}

	
	public String toString(){
		return name;
	}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;

public abstract class Heat {

	private ArrayList runners = new ArrayList();
	private  ArrayList  winners = new ArrayList();
	
	
	public  ArrayList getRunners(){
		
		return runners;
	}
	public ArrayList getWinners(){
		
		return winners;
	}
	
	protected  String name;
	
	public String getName(){
		
		return name;
	}

	public Heat(){}
		
	public void addRunner(Runner runner){
		
		runners.add(runner);
	}
		
    public String displayRunners(){
		
		Iterator iter = runners.iterator();
		
		String result="";
		while(iter.hasNext()){
			
			Runner runner =(Runner)iter.next();
			
			
			result+=runner+"\n";
		}
		return result;
	}
		
	public String toString(){
		return name;
	}
	
	public static double createRandomTime() {
		Random ranGen = new Random();
		return ranGen.nextDouble() + 9;
	}
	
	public void runRace() {
		Iterator iter = runners.iterator();
		
		while(iter.hasNext()){
		
			Runner aRunner =(Runner)iter.next();
			
			aRunner.setTime(createRandomTime());
		}
	}
	
public void getWinnersFromHeat(){
				
		int count=0;
		Collections.sort(runners);
		
		Iterator iter = runners.iterator();
             while(iter.hasNext()){
            	 Runner  aRunner= (Runner)iter.next();
            	 
            	 if(count!=4){
            	 winners.add(aRunner);
            	 }else
            	 {
            		 break;
            	 }
	count+=1;
}
}             
             
public void getWinnersFromFinal(){
 				
    int count=0;
    Collections.sort(runners);
         		
    Iterator iter = runners.iterator();
            while(iter.hasNext()){
                Runner  aRunner= (Runner)iter.next();
                     	 
                if(count!=1){
                   winners.add(aRunner);
                 }else
                 {
                  break;
                 }
  count+=1;
         }             
}
public void getWinnersFromPreviousRound(Heat oldHeat){
		
	this.addRunner((Runner) oldHeat.getWinners().get(0));
	this.addRunner((Runner) oldHeat.getWinners().get(1));
	this.addRunner((Runner) oldHeat.getWinners().get(2));
	this.addRunner((Runner) oldHeat.getWinners().get(3));
	
}
	
}
public final class Round1 extends Round{

	
	public Round1(){
		
		this.name="Round1";
	}
	
	
}
public final class Heat1 extends Heat{

	public Heat1() {
		this.name="Heat1";
		// TODO Auto-generated constructor stub
	}

}



the heat and round extentions are repeated 7 more times for th heats and 3 more tims for the round
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Sprint Race simulation

Posted 08 November 2010 - 03:06 PM

Ow...have you ever heard of arrays? It will make your code much more condensed and easier to manage with for loops. You have individually written each Runner out. Make an array of Runners instead and manipulate the whole array. It makes it much easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1