Instructions

The day after these are assigned we will cover solutions in class. After that, I will post the solutions online.

Assignments

  1. Minimum & Maximum Element of an Array What we want to do in this assignment is to find the minimum and maximum elements of an array. The array we'll be using will be an array containing all of your names, as seen in the example here: Arrays: Some Examples.

    What we want to do is find the student's name who would appear first in an alphabetical listing, and also the students name who would appear last in the alphabetical listing. See the applet below for what you must do.

    Specifically, you should do the following...
    1. Create a new Java project called MinMaxArray
    2. Add a new Java class to that project called MinMaxArray
    3. Copy the code from the GenericWithScrolling program into this new file, and rename the class to MinMaxArray.
    4. Remove the input label and field.
    5. Change the button name to say "Find Min & Max Names".
    6. Then do the following: Copy the array declaration from the example referenced above into your file, near the top where you declare your JTextArea and JButton. In your init(), at the very end, display all of the elements in the array, in the order they appear, as done in the applet below.
    7. Once you are finished with that part, and only once you are finished with that part, setup the button action so that it scans through the array and find both the least element, and the maximum element (see applet below). Then append this information to your output, as is done in the applet below.
    Install Java to see the solution!



  2. Sorting an Array Now what we want to do is actually sort the array of names. That is, we want to rearrane all of the elements in the array such that the names appear in alphabetical ordering. It turns out we can use what we did above to accomplish this quite easily.

    The general method you will use will be discussed in class, and we'll go through it together.

    You must do the following...
    1. Create a new Java project called SortArray
    2. Add a new Java class to this project called SortArray
    3. Copy-n-paste your code from the previous assignment into this file, and use this as a starting point.
    4. Change the button name to say "Sort Them!"
    5. When the button is clicked, you must sort the array and then display the sorted array.
    The following bits of code will be usefull, and you are free to copy then to your liking. We'll discuss them heavily in class.
       // Method for "swapping" two elements of an array, given their locations
       void swapElements(String[] a, int i, int j) {
          String hold = a[i];
          a[i] = a[j];
          a[j] = hold;
       }
    
       // Method for finding the index of the minimum element of a subset array.
       // Specifically, this method should scan the array a starting
       // at position startPosition, and find the minimum element.
       // It should return the index of that element.
      int minIndex(String[] a, int startPosition) {
    
          // This is for you to do!
          // Do NOT hesitate to ask for help!
      }
          
    Install Java to see the solution!