Results 1 to 10 of 10
  1. #1
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default i need to sort these runners into time order shortest first

    i need to sort these runners into time order shortest first, the classes both work i just cant get the method sortRunnerList() to work
    please help me alleviate my stupidity, what am i doing wrong
    and guys, thanks for the help



    Java Code:
    public class Runner implements Comparable <Runner>
    
    {
     /* static variables */
       private static int nextNumber;
     // To be added by students in Question 3, part (i)(a) and part(iv)(a)
    
     /* instance variables */
         
       private int number;       // runner's number
       private String name;      // runner's name
       private String ageGroup;  // standard, junior or senior
       private int time;         // runner's marathon time in minutes
     
       
       /**
        * Default constructor for objects of class Runner
        */
       public Runner()
       {
          super();
          this.name = "";
          this.ageGroup = "standard";
          this.time = 0;
          this.number = nextNumber ++;
        // additional code to be added by students in Q3, part (i)(b)
        }
    
     /* instance methods */
    
        //Only those accessor methods that you will need have been included
            
       /**
        * Returns the receiver's number
        */
       public int getNumber()
       {
          return this.number;
       }
       
       
       /**
        * Sets the receiver's name
        */
       public void setName(String name)
       {
          this.name = name;
       }
       
       
       /**
        * Returns the receiver's name
        */
       public String getName()
       {
          return this.name;
       }
       
       
       /**
        * Sets the receiver's ageGroup
        */
       public void setAgeGroup(String group)
       {
          this.ageGroup = group;
       }
    
       /**
        * Returns the receiver's ageGroup
        */
       public String getAgeGroup()
       {
          return this.ageGroup;
       }
        
       
       /**
        * Sets the receiver's time
        */
       public void setTime(int time)
       {
          this.time = time;
       }
       
       
       /**
        * Returns the receiver's time
        */
       public int getTime()
       {
          return this.time;
       }
       
        /**
        * Compares the receiver with the argument and returns:
        * 
        * 
        */
       public int compareTo(Runner anotherRunner)
       {
       int a = this.getTime();
       int b = anotherRunner.getTime();
       int c, d;
       if(a <= b)
       {
          System.out.println(a);
          return a;
       }
       else
       {
          System.out.println(b);
          return b; 
       }
     }
    }
    Java Code:
    public class MarathonAdmin 
    {
       // instance variables - replace the example below with your own
       public ArrayList<Runner> runners = new ArrayList<Runner>();
       private Runner entrant = new Runner();
       private Random randomNumber;
       /**
        * Constructor for objects of class MarathonAdmin
        */
       public MarathonAdmin()
       {
          // initialise instance variables
         ArrayList<Runner> runners = new ArrayList<Runner>();
         randomNumber = new Random();
       }
       
       /**
        * reads runners from a file
        */
       public void readInRunners()
       {
          String pathname = OUFileChooser.getFilename();
          File aFile = new File(pathname);
          BufferedReader bufferedFileReader = null;
          Scanner bufferedScanner = null;
          
          try
          {
           
           String name;
           int age;
           int time = 0;
           String Group;
           Scanner lineScanner;
           String currentLine;
           bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
           while(bufferedScanner.hasNextLine())
           {
              currentLine = bufferedScanner.nextLine();
              lineScanner = new Scanner(currentLine);
              lineScanner.useDelimiter(",");
              name = lineScanner.next();
              age = lineScanner.nextInt();
              Runner entrant = new Runner();
              runners.add(entrant);
              entrant.setName(name);
              entrant.getNumber();
              entrant.setTime(0);
              if(age <= 18)
              {
                 Group = "Junior";
              }
              else if (age >= 55)
              {
                 Group = "Senior";
              }
              else
              {
                 Group = "Standard";
              }
              entrant.setAgeGroup(Group);
           }
        }
         catch(Exception anException)
        {
            System.out.println("ERROR: " + anException);
        }
       }
       /**
        * Returns a random integer value specifying a sprint length 
        * between 90 and 180 inclusive.
        */
       public int getRandomNumber()
       {
          return (this.randomNumber.nextInt(90) + 90);
       }      
       /**
        * itterates through an array and generates a random time for runninmg of marathon
        */
       public void runMarathon()
       {
          for(Runner entrant : runners)
          {
            entrant.setTime(getRandomNumber());
          }
       }
       /**
        * sorts the list into time order assending to the longest time
        */
       public void sortRunnerList()
       {
           int a = 0;
           int b = 1;
           for(Runner entrant : runners)
          {
           runners.get(a);
           runners.get(b);
           runners.get(a).compareTo(runners.get(b));
          
           a++;
           b++;
           
       
          }
          
       }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: i need to sort these runners into time order shortest first

    Can you post some output that shows what the code is doing and add some comments showing what you want different.

    Can you explain the logic for the code that is in sortRunnerList()? What are each of those statements supposed to do?
    Last edited by Norm; 05-05-2014 at 07:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: i need to sort these runners into time order shortest first

    the sort runner list is meant to use the compareTo method in Runner class, to get a return value and then hopefully sort the list into time order fastest first,
    the method iterates through the list and then returns me the smallest of the two numbers being compared, until it throws a wobbler at the end of the list.
    the list of numbers is not helpful to me and i have tried the collections.sort to iterate my list but it wont go into my list ma.runners.time to use the comparable method in Runners.

    output from window
    141
    97
    97
    112
    120
    120
    117
    117
    119
    129
    129
    129
    121
    121
    java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    in java.util.ArrayList.rangeCheck(ArrayList.java:604)
    in java.util.ArrayList.get(ArrayList.java:382)
    in MarathonAdmin.sortRunnerList(MarathonAdmin.java:10 8)
    in (OUWorkspace:1)

  4. #4
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: i need to sort these runners into time order shortest first

    Java Code:
     public void sortRunnerList()
       {
           int a = 0;
           int b = 1;
           for(Runner entrant : runners)
          {
           runners.get(a);
           runners.get(b);
           runners.get(a).compareTo(runners.get(b));
           
           a++;
           b++;
    a and b are meant to be local variables that should facilitate iteration through the list
    runners.get(a) and runners.get(b) are runners from ma.runners with a and b being their position numbers, it was my way allowing it to go down the list

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: i need to sort these runners into time order shortest first

    Your Comparable interface is not implemented correctly. Have you read the API to see how to fulfill the contract?
    It is essential to read and understand the API's to program in Java. Once you read it, if you have questions, we can help.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: i need to sort these runners into time order shortest first

    The three method calls inside the sortRunnerList() method all return a value that the code is not storing or using.

    The Collections class has methods for sorting ArrayLists. Can you use one of them?
    Or do you need to write all the code for the sort?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: i need to sort these runners into time order shortest first

    i have tried to use it, it doesn't let me compile Collections.sort(runner); compiles but does nothing if i try Collections.sort(runner.time()); it doesn't compile.
    Collections.sort(runner.getTime()); doesn't compile. Collections.sort(entrant etc etc doesn't compile. i don't care if i am meant to use it or not at this point i would just like it to work, so i can move onto the next bit

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: i need to sort these runners into time order shortest first

    it doesn't let me compile
    If there are compiler errors you need help with, copy the full text and paste it here.


    Collections.sort(runner); compiles but does nothing
    See jim829's post re the method definition.
    Add some println statements to the compareTo() method to see if it is called.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: i need to sort these runners into time order shortest first

    If you are doing Collection.sort(runner), then that is wrong. You want to sort a list and your list is "runners" not "runner." And get rid of the ArrayList runners variable in your constructor. It is not used and doesn't do anything. Your class variable, runners it the one you want to keep.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: i need to sort these runners into time order shortest first

    GUYS you are Dynomite, thank you very much,

Similar Threads

  1. Application relevant sort order of results
    By siddarth in forum Lucene
    Replies: 4
    Last Post: 01-21-2014, 04:21 PM
  2. I want to learn J2EE in the shortest time possible
    By sam36666 in forum New To Java
    Replies: 5
    Last Post: 01-04-2014, 04:43 PM
  3. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 12:28 PM
  4. Shortest Seek-Time First with java.util.concurrent
    By SAiNT_JiMMiE in forum Threads and Synchronization
    Replies: 0
    Last Post: 05-11-2010, 10:18 AM
  5. Sort in Cyrilic order
    By cselic in forum Advanced Java
    Replies: 4
    Last Post: 04-21-2010, 02:03 PM

Posting Permissions

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