Daniel Forhan
Computer Science Teacher and Java Programmer

Working with Arrays

Sunday, November 20, 2005 12:37 PM

 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


Feedback

# re: Working with Arrays

Nice little project. Are any of the students playing with sorting other types of values on their own? 11/20/2005 1:39 PM | Alfred Thompson

# re: Working with Arrays

By the way, how did you set up your command buttons with the nice images? 11/20/2005 1:40 PM | Alfred Thompson

# re: Working with Arrays

About the images, it is real easy. Just select the button (or whatever control you want to set a background image for). Then in the control properties find "BackgroundImage". You will see a button with "..." (three dots). click on that, then you will be in a new window that will allow you to import a "project resource file" (radio button). Then simply click on the import button and navigate to the pic file that you want to use as a backgroud.

Back to your first question, we have just started discussions about arrays and haven't done much with sorting yet so I don't think so. 11/20/2005 3:27 PM | Daniel Forhan

# re: Working with Arrays

Your class is a couple of days ahead of mine so your example will come in handy when I introduce arrays after the Thanksgiving holdiday. Dressing up the windows form like you did is a lot more appealing to students than a boring console window!! 11/20/2005 5:21 PM | Brian Scarbeau

# re: Working with Arrays

This is a great clear cut example of instructions for teachers. Have you thought of doing a short presentation of it in action? 11/28/2005 10:32 AM | Pat Phillips

# re: Working with Arrays

Thank you Pat. Regarding a short presentation, that is definitely an option that I am considering for this example, as well as other examples that I am working on. 11/28/2005 11:31 AM | Daniel Forhan

# re: Working with Arrays

Let me know when you have this or others complete and I will use as tip of the week in MF. Also consider submitting this type of thing as a word doc to add to the resources section of MF. 11/30/2005 10:03 AM | Pat Phillips

# re: Working with Arrays

Thank you for your great example! I'm about to port it to Borland C++ Builder 2/15/2006 7:35 AM | Max

Post a comment