?
Solved

Sorting question

Posted on 2003-03-14
8
Medium Priority
?
171 Views
Last Modified: 2012-05-04
I am creating a sorting program where an operator insert a limited number of values into the textfield. The Program also consist of two buttons (Stop and start). I enter one value, click on "Start", and I enter another number, and click "Start" again. The "stop" button means that I exit the entire program. When I enter a special value, let say -1, the program will display a set of values that I entered from the textfield and the "sorted" set of values right underneath it.

I feel like replacing the value -1 to something else, like in word that will do the sorting command; for instance, typing "sort" will display the numbers on the screen. I need to change something from the "public void Sorter() throws IOException" section. How can I do that? Here are the codes that I've been working on so far:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class ScreenManager extends Canvas
{
    ScreenManager()
    {
        setSize(600, 500);
        setBackground(Color.yellow);
    }
   
    public void print( String text, int x, int y )
    {
     
     Color cvar = Color.blue;
     
     Graphics g = getGraphics();
        g.setColor( cvar );
        g.drawString(text,x,y);
    }
   
    public void clear()
    {
        Graphics g = getGraphics();
        g.clearRect( 0,0, getSize().width, getSize().height );
    }
}

public class Sort1 extends Frame implements ActionListener
{

    ScreenManager s;
   
    Button startButton, stopButton;
    Label prm;
    TextField tf;
    int a[] = new int[101];
    int n, j, y = 30;
    int ni = 0;
    int temp;
    int flag;
   
    int number;

    public Sort1 ()
    {
   
        super( "Sort numbers" );
        setLayout( new FlowLayout(FlowLayout.CENTER) );
       
        startButton = new Button( "Start" );
        stopButton = new Button( "Stop" );
        prm = new Label("Enter a number, -1 to quit: ");
        tf = new TextField(10);
        startButton.addActionListener( this );
        stopButton.addActionListener( this );
       
        add( startButton );
        add( stopButton );
        add( prm );
        add( tf );
       
        s = new ScreenManager();
       
        add( s );
       
        pack();
        show();
    }
   
    public static void main( String args[] )
    {
        new Sort1();
    }
   
    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == stopButton )
        {
            setVisible( false );
            System.exit(0);
        }
        else if ( e.getSource() == startButton )
        {
            try
            {
               Sorter();
            }
            catch (IOException ioe)
            {
                setVisible( false );
                System.exit(0);
            }
        }
    }
   
   
   public void Sorter() throws IOException
    {
     
      number = Integer.parseInt( tf.getText() ) ;
      if ( number != -1 )
      {
         ni++;
         a[ni] = number;
         tf.setText("");
         return;
      }
     
      s.clear();
      y = 130;
      for ( j = 1; j <=ni; j++ )
      {
         s.print( j + ":  " + a[j], 50, y );
         y = y + 15;
      }
     
      n = ni;
     
      do
      {
          flag = 1; n--;
         
          for ( j = 1; j <= n; j++ )
          {
             if ( a[j] > a[j+1] )
             {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
                flag = 0;
             }
          }
        }while( flag == 0 );
       
        y = y + 20;
        s.print("Here they are in sorted order: ", 50, y );
        y = y + 20;
        for ( j = 1; j <= ni; j++ )
        {
           s.print( j + ":  " + a[j], 50, y );
           y = y + 15;
        }
    }
}


0
Comment
Question by:Daving
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 8140199
Simply add a case to your actionPerformed method that deals with a third button (e.g. "Show").

Change the Sorter method to only contain the part for number!=-1, and add a new method (e.g. showSort) that contains the other part of Sorter.

Finally, of course, you will have to add the new button to the Sort1 constructor.
0
 

Author Comment

by:Daving
ID: 8142704
Can you show me how to do that?

I already set the third button, but i not too sure about the last two procedures.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 120 total points
ID: 8149650
Try something of this sort:

public void Sorter()
  throws IOException
{
  String str = tf.getText () ;

  if ( ! str.equals ( "sort" ) )
  {
    number = Integer.parseInt( tf.getText() ) ;

    if ( number != -1 )
    {
      ni ++ ;
      a[ni] = number ;
      tf.setText ("" ) ;
      return ;

    } // end nested if

  } // end outer if

  else // str equals "sort" - sort and display
  {

   // your sorting and display code

  } // end else

} // end of Sorter ()


Hope that helps!

Mayank.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Daving
ID: 8152213
Thanks Mayank. It really works.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8156756
If you're satisfied, then please rate the question so that it can be PAQed :-)

Thanks!

Mayank.
0
 

Author Comment

by:Daving
ID: 8158307
I don't know how to do that. I'm new around this site.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8158355
There'll be link similar to "Accept Comment as Answer" on the header-line of all the comments. If you click on the one which is on the header of my comment, you'll be asked what grade you want to give to my answer, and whether you want to increase the points. You can give my answer a grade of A, B, or C - whatever you like (if it worked well, then an A, if its not so good an answer, then a B, .... whatever) - better grade will get more expert points to me, and if you want, you can increase the points you gave to this question, or else you can let it stay as it is.

Hope that helps you!

Mayank.

PS: If that still doesn't help, then you can visit the help site for Experts-Exchange:

http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

0
 

Author Comment

by:Daving
ID: 8163889
Really like your answer to my problem. You've given great advice to me, I appreciate it alot.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question