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 set the time with a setTime method for each object then sort them by time 1st to last 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 gratefully 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 = newRunner("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 = newRunner("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;
	    	
	    }
	 
	   
}

To start, I would create a method that returns a random time between 9 and 10 secs as a string. Then in a for loop, you could set the time of each runner. ex: race.get(i).setTime(getRandomTime());

cheers, i'll look into this in the morning, i was had this all day and it was driving me crazy, any thing that helps me move forward is a great help.

To start, I would create a method that returns a random time between 9 and 10 secs as a string. Then in a for loop, you could set the time of each runner. ex: race.get(i).setTime(getRandomTime());

Hi,

i have been working on this and have come up with the following way i think to ceate the random time, but im having trouble getting the genRandomTime method recognised, im also still stuck on trying tp undertand how to search and sort by the times once they are randomised.

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


public class RaceSim{ 

    public static void main(String[] args){
        Random ranGen = new Random();
            Runner p1 = new Runner("Mark", "Brady", genRandomTime());
        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");

            // Generates random 'times' between 9 and 10 seconds
        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(double time){
            return time;
        }

        public void setTime(double time){
            this.time = time;

        }


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

To start, I would create a method that returns a random time between 9 and 10 secs as a string. Then in a for loop, you could set the time of each runner. ex: race.get(i).setTime(getRandomTime());

Hi,

i have been working on this and have come up with the following way i think to ceate the random time, but im having trouble getting the genRandomTime method recognised, im also still stuck on trying tp undertand how to search and sort by the times once they are randomised.
Sorry forgot to put it in code quotes!

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


public class RaceSim{ 
	
	public static void main(String[] args){
		Random ranGen = new Random();
	        Runner p1 = new Runner("Mark", "Brady", genRandomTime());
		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");
				       
	        // Generates random 'times' between 9 and 10 seconds
		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(double time){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }
	 
	   
}

Move genRandomTime() outside of main(). You can't declare methods inside methods. You will also need to move the declaration of 'ranGen' out of main() so your function can access it.

You can read a bit about sorting a collection here: http://download.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting

Thanks,
I've done this but i had to change the ranGen declaration to static to get it to run but i get a strange 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]

static Random ranGen = new Random();	       	 	    	       

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

That is the toString() output of your ArrayList. You could override toString() on your Runner class to provide a different string value for the Runner object or you can loop the list and print whatever you want from each Runner object.

That is the toString() output of your ArrayList. You could override toString() on your Runner class to provide a different string value for the Runner object or you can loop the list and print whatever you want from each Runner object.

I've set this up to what i think is right but getting an error on the getTime call to the method in runner saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getTime(double) in the type Runner is not applicable for the arguments()
at RaceSim.main(RaceSim.java:57)

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", 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");
				       
        for (int i = 0; i < race.size(); i++) {
System.out.printf("%s = %2.2f seconds\n", race.get(i).getfName(),race.get(i).getsName(), race.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(double time){
	    	return time;
	    }
	 
	    public void setTime(double time){
	    	this.time = time;
	    	
	    }

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

	
	 
	   
}

getTime() is just returning the time. It does not need a parameter declared. Check your declaration in the Runner class.

getTime() is just returning the time. It does not need a parameter declared. Check your declaration in the Runner class.

Thanks, missed that, i've added some new stuff now and i've got it to randomise the times and to print out all the details of the race and no errors now.

If i was going to sort the array by time and move the 3 fastest runners to a new arraylist do you kow what would be the best way of doing this.?

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;
		}

	
	 
	   
}

If you don't need to maintain the original sorted list, you could just search for and move the fastest runner to a new List three times in succession.

If you do want to maintain the sorted results, you can use Collections.sort(). I posted the brief tutorial link for that a few posts back.

If you don't need to maintain the original sorted list, you could just search for and move the fastest runner to a new List three times in succession.

If you do want to maintain the sorted results, you can use Collections.sort(). I posted the brief tutorial link for that a few posts back.

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 advice 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";
	}

}

You've made quite a bit of progress with it. I would recommend one slight design modification that may help. I would move the run(Heat) method into the Heat class and put getWinners(int n) there as well, returning the top n Runners from the Heat.

Your Round class then just manages a collection of Heats. After you have run the Heats, you can select the winners from them however you wish and use those to create a new Round from those results.

Perhaps creating a Round constructor that accepted a List of Runners would be helpful. Your Round class can divide them into Heats however it wants, such as by last race time.

You've made quite a bit of progress with it. I would recommend one slight design modification that may help. I would move the run(Heat) method into the Heat class and put getWinners(int n) there as well, returning the top n Runners from the Heat.

Your Round class then just manages a collection of Heats. After you have run the Heats, you can select the winners from them however you wish and use those to create a new Round from those results.

Perhaps creating a Round constructor that accepted a List of Runners would be helpful. Your Round class can divide them into Heats however it wants, such as by last race time.

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

This article has been dead for over six months. Start a new discussion instead.