• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Help me Debug the Code

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friend
Please Help me Debug the code.I have a Clock.java in current directory which contains the methods getHours,getminutes etc.
i m trying to sort clock objects in ascending order while implementing the Comparator interface.
thanx
bapi


[This message has been edited by bapi dhar (edited March 13, 2001).]
pl use the [ code ] tags without the spaces when posting source code, please!
- satya

[This message has been edited by Madhav Lakkapragada (edited March 14, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following issues with your code:

  • In order to use BufferedReader, you will have to also import java.io.*;
  • In your compare method you have cast o1 to a Clock object only for its first use, and you have never cast o2 to a Clock object.
  • In the compare method, you are comparing c2 to its self every other line. I think you intended to compare c2 to c1.
  • In your getTime method, you are making use of a BufferedReader that is undefined.
  • In your getTime method, you are catching Throwable, which is acceptable, but I personally would catch IOException, since that is what will be thrown from your try block.
  • Finally, you have not included a definition for the Clock class. Obviously this will be key, and I am sure you have that coded and just didn't include it in your post.

  • I would try it like this:
    <PRE>
    import java.util.*;
    import java.io.*;
    class ClockComparator implements Comparator
    {
    public int compare( Object o1, Object o2 )
    {
    Clock c1, c2;
    c1 = (Clock)o1; // cast back to Clock
    c2 = (Clock)o2; // cast back to Clock
    if( c1.getHours() > c2.getHours() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getHours() > c1.getHours() )
    {
    return -1; // Clock two is greater
    }
    // Hours are equal, compare minutes
    else if ( c1.getMinutes() > c2.getMinutes() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getMinutes() > c1.getMinutes() )
    {
    return -1; // Clock two is greater
    }
    // Minutes are equal, compare seconds
    else if ( c1.getSeconds() > c2.getSeconds() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getSeconds() > c1.getSeconds() )
    {
    return -1; // Clock two is greater
    }
    // Clocks are equal
    else
    {
    return 0;
    }
    }
    }

    public class Sort
    {
    static Clock clock[];
    static Object temp;
    public static void sort( Object[] c, ClockComparator cc )
    {
    for ( int i = 0; i < c.length; i++ )
    {
    for( int j = i + 1; j < c.length; j++ )
    {
    if ( cc.compare( c[j], c[i] ) < 1 )
    {
    temp = c[j];
    c[j] = c[i];
    c[i] = temp;
    }
    }
    }
    }

    public static void main( String args[] )
    {
    if ( args.length != 1 )
    {
    System.out.println( "Please include number of Clock times in Command Line ... Thank You" );
    System.exit( 0 );
    }
    int nc = Integer.parseInt( args[0] );
    clock = new Clock[nc];
    for ( int i = 0; i < nc; i++ )
    {
    System.out.println( "Enter Clock " + ( i + 1 ) );
    clock[i] = getTime( String.valueOf( i + 1 ) );
    }
    sort( clock, new ClockComparator() );
    } // main
    private static Clock getTime( String timeType )
    {
    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    Clock t;
    while( true )
    {
    System.out.print( "Enter the " + timeType + " time in HH:MM:SS, 24-hr format: " );
    try
    {
    t = Clock.valueOf( br.readLine() );
    break;
    }
    catch ( IOException e )
    {
    System.out.println( e );
    System.out.println( "Problem, try again!" );
    }
    } // while
    System.out.println( "The " + timeType + " time is " + t );
    return t;
    }
    } // class
    </PRE>
    HTH
    [This message has been edited by Bodie Minster (edited March 14, 2001).]
 
bapi dhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx
bapi
 
reply
    Bookmark Topic Watch Topic
  • New Topic