?
Solved

Sorting question

Posted on 2003-03-14
8
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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