Results 1 to 3 of 3
  1. #1
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default Problems with sort swimmers

    I'm in an intro to comp sci class and I came across a problem I can't figure out.

    I have to write a program which will take the names and times for three swimmers during a 50-m free style.

    The program has to sort out their times from fastest to slowest. Its in the chapter for if statements so I need to sort out the times using if statements, but I can't seem to figure out how.

    My problem is I just can't figure out the logic, what to use for the if statements.

    Java Code:
    import java.util.Scanner;
    
    public class SwimmerTimes
    {
        public static void main(String[] args)
    	 {
    	     String swimmer1_name;
    		  String swimmer2_name;
    		  String swimmer3_name;
    		  
    		  String first;
    		  String second;
    		  String third;
    		  
    		  float swimmer1_time;
    		  float swimmer2_time;
    		  float swimmer3_time;
    		  
    		  Scanner keyboard = new Scanner(System.in);
    		  
    		  // swimmer 1
    		  System.out.println("Please enter the first swimmer's name:");
    		  swimmer1_name = keyboard.nextLine();
    		  System.out.println("please enter the first swimmer's time:");
    		  swimmer1_time = keyboard.nextFloat();
    		  
    		  // swimmer 2
    		  System.out.println("Please enter the second swimmer's name:");
    		  swimmer2_name = keyboard.nextLine();
    		  System.out.println("please enter the second swimmer's time:");
    		  swimmer2_time = keyboard.nextFloat();
    		  
    		  // swimmer 3
    		  System.out.println("Please enter the third swimmer's name:");
    		  swimmer3_name = keyboard.nextLine();
    		  System.out.println("please enter the third swimmer's time:");
    		  swimmer3_time = keyboard.nextFloat();
    		  
    		  if (swimmer1_time < swimmer2_time && swimmer2_time < swimmer3_time)
    		  {
    		      first = swimmer1_name;
    				second = swimmer2_name;
    				third = swimmer3_name;
    		  }
    		  else if (swimmer2_time < swimmer1_time && swimmer1_time < swimmer3_time)
    		  {
    		      first = swimmer2_name;
    				second = swimmer3_name;
    				third = swimmer1_name;
    		  }
    		  else if (
    	 }
    }
    Thanks

    Felissa

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    Will the input always be three swimmers? If it's always three swimmers then you can do any number of sorts the brute force way.

    Or you could just do a mass comparison, figuring out which spot goes where.

    Java Code:
     if(swim1 > swim2)
        if(swim1 > swim3) // swim1 = biggest
          if(swim2 > swim3) // swim3 smallest, swim2 middle, swim1 last
          else // swim3 middle, swim2 smallest, swim 1 last
        else  //swim 1 = second, swim3 largest, swim2 smallest
      else
        if(swim 1 > swim3) //swim 1 second, swim 3 first, swim2 last
        else  // swim1 = first
          if(swim 2 > swim3) // swim1 first, swim2 last swim3 second
          else  // swim1 first, swim2 second, swim3 last
    Or something like that. I have NO clue if that is completely correct, but you could debug through the logic yourself and figure it out.

    Greetings

    Marcus:cool:

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    The following solution might be an advanced solution for you...but since you are learning Java, I am sure that sooner or later...you will come across Classes, Arrays, etc.

    The following is a class that defines Simmer and should be saved in "Simmer.java" file

    Java Code:
    public class Simmer {
        
        private String name; // simmer name
        private float time; // simmer age
        
        // default constructor
        public Simmer() {
        }
        
        public Simmer( final String name, final float time ) {
            this.setName( name );
            this.setTime( time );
        }
        
        public void setName( final String name ) {
            if( "".equals( name ) ) {
                // throw exception or try and ask user to enter name agin
            } else {
                this.name = name;
            } //-- ends else
        } //-- ends setName
        
        public String getName() {
            return this.name;
        } //-- ends getName
        
        public void setTime( final float time ) {
            if( time <= 0 ) {
                // throw exception...or ask user to enter time again
            } else {
                this.time = time;
            } //-- ends else
        } //-- ends setTime
        
        public float getTime() {
            return this.time;
        } //-- ends instance method getTime
         
        public String toString() {
            return "Name: " + this.name + "\tTime: " + this.time;
        } //-- ends toString
            
    } //-- ends class definition
    The following is a class that has a main method, which glue everything together. Save this file in SimmerRunner.java

    Java Code:
    public class SimmerRunner {
        public static void main( String [] args ) {
            
            // get the input from user as you have done in your sample
            // and then populate the array as shown below
            Simmer [] simmerCollection = new Simmer[3];
            simmerCollection[0] = new Simmer( "John Smith", 145.11f );
            simmerCollection[1] = new Simmer( "Mark DoLittle", 138.28f );
            simmerCollection[2] = new Simmer( "John Summer", 145.45f );
            
            // print all simmer as they have been entered
            System.out.println("-------- Simmers Entered Competition -------");
            for( int i = 0; i < simmerCollection.length; i++ ) {
                System.out.println(simmerCollection[i].toString());
            } //-- ends for loop
            
            // now, we have to loop through the array twice using if statement
            // to establish who finished first
            for( int i = 0; i < simmerCollection.length; i++ ) {
                for( int j = 0; j < simmerCollection.length - 1; j++ ) {
                    // we use if statements to check the finishing times
                    if( simmerCollection[j].getTime() > simmerCollection[j+1].getTime() ) {
                        // if someone stored in posisiotn zero in the array finished
                        // after someone in position 1, we swap the reference in the array
                        Simmer temp = simmerCollection[j];
                        simmerCollection[j] = simmerCollection[j+1];
                        simmerCollection[j+1] = temp;
                        temp = null;
                    } //-- ends if 
                } //-- ends inner for loop
            } //-- ends for loop
            
            // print all simmer as they have been entered
            System.out.println("\n---- Simmers Ordered by Finishing Time ----");
            for( int i = 0; i < simmerCollection.length; i++ ) {
                System.out.println(simmerCollection[i].toString());
            } //-- ends for loop
        } //-- ends class method main
    } //-- ends class definition
    once you have compiled the files, and executed SimmerRunner, the output should match the following:
    Java Code:
    -------- Simmers Entered Competition -------
    Name: John Smith        Time: 145.11
    Name: Mark DoLittle     Time: 138.28
    Name: John Summer       Time: 145.45
    
    ---- Simmers Ordered by Finishing Time ----
    Name: Mark DoLittle     Time: 138.28
    Name: John Smith        Time: 145.11
    Name: John Summer       Time: 145.45
    Greetings

    Eric

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  2. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM
  3. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  4. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM
  5. how to sort 2 tables
    By valery in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:30 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
  •