Geeks With Blogs
Daniel Forhan Computer Science Teacher and Java Programmer

 This past week we have been studying arrays and ArrayLists in my AP class. It didn’t take long for students to become excited about using them once they saw how valuable of a tool they are. One of the issues I try get across with them is how arrays/ArrayLists are a key part to use of sorting/searching algorithms. Since sorting and searching doesn’t come up officially until later in the course I take this opportunity to discuss it a little with them now saving the in-depth discussion on various sorting algorithms for later.

Although I don’t get into the actual algorithms right now, I do show them how they can use existing methods to do a sort using arrays. It also seemed like a good opportunity to show how easy it is to build an application using J#. So this lesson accomplished two major things; It familiarized them with the benefit of using arrays, and gave them some exposure to a nice programming tool (J#). So here is the example.

Let's start with the set up of J#. We used the express version.

Go to file-> new Project-> windows Application.


Once the application files are created we can now build a simple application that will sort the values that are entered to it.

In my example we added a text-box for entering the values, a text-box for displaying the sorted values and button to trigger the sort. Most of the action occurs in the SortButton_Click method of our Form class (Form1). The easiest way to get to that method is to double click on the button in the “design mode” and that will take you to the method automatically (see code example below).

The code example below illustrates how the sorting was done. Keeping in mind that we are just using the sort method to illustrate the benefit of arrays, not to learn how sorting algorithms work (at least not yet). This example also illustrates how one can use the StringTokenizer class to break down a string. Note: This method requires as few extra imports (java.util.Arrays, and java.util.StringTokenizer).

private void SortButton_Click(Object sender, System.EventArgs e)

{

     answerBox.Clear(); //reset in case text already there

     String values = numberTextBox.get_Text(); //get string


     //class that allows one to break down a string.

     StringTokenizer tokens = new StringTokenizer(values);

     int size = tokens.countTokens(); //to get token count

     double[] doubleNums = new double[size];

     int tokenCount = 0;


     //grabs each token, parses to double and stores in array.

     while (tokens.hasMoreTokens())

    {

     doubleNums[tokenCount] = Double.parseDouble(tokens.nextToken());

     tokenCount++;

     }


     Arrays.sort(doubleNums); //sort array

     //Display array elements in text box

     for (int x = 0; x < doubleNums.length; x++)

     {

     if (x < doubleNums.length-1)

     answerBox.AppendText(doubleNums[x]+ ", ");

     else

     answerBox.AppendText(doubleNums[x]+ "");

     }

}


private void button1_Click(Object sender, System.EventArgs e)

   {

     System.exit(0); //Exits application

   }


}//end of class

The students in my class seemed to really like the simplicity of using J# and were anxious to try it out. I gave them a few simple programs to practice using J# for their weekend homework. Who says homework has to be boring.

You can download the whole program here if you wish

Posted on Sunday, November 20, 2005 12:37 PM | Back to top

Copyright © Daniel Forhan | Powered by: GeeksWithBlogs.net