Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 08:38 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 08:40 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 08:43 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
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

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

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:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
need help with bubble sort lowpro New To Java 3 12-17-2007 07:27 PM
sort Camden New To Java 7 11-28-2007 03:11 AM
how to sort Feng New To Java 1 11-20-2007 08:56 AM
how to sort 2 tables valery AWT / Swing 1 08-06-2007 10:30 PM


All times are GMT +3. The time now is 04:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org