Thursday July 20, 2000


Quiz 26


1. What is the syntax used for declaring a new array?

2. What is the syntax used to initialize an array when we declare it?

3. When the data type in an array is an object what is actually stored in the array?

4. What two peices of information do we need in order to access an element in an array?
































We have been discussing problems related to arrays.

  • Create an array consisting of 5 ints each read from the keyboard. Then find the sum of the elements in the array.

    import java.io.*;
    
    public class Ex1 {
    
       public static void main(String[] args) throws IOException {
          InputStreamReader stream = new InputStreamReader(System.in);
          BufferedReader keyboard = new BufferedReader(stream);
          int[] numbers = new int[5];
    
          int sum = 0;
    
          for (int counter=0;counterɝcounter++) {
             System.out.println("Please enter an int");
             numbers[counter] = new Integer(keyboard.readLine()).intValue();
             sum += numbers[counter];
          }
    
          System.out.println("The sum of your numbers is " + sum + ".");
       }
    
    }
    
  • Create an array consisting of 10 doubles each read from the keyboard. Then find the maximum value read in and the minimum value read in.

    import java.io.*;
    
    public class Ex2 {
    
       public static void main(String[] args) throws IOException {
          InputStreamReader stream = new InputStreamReader(System.in);
          BufferedReader keyboard = new BufferedReader(stream);
    
          double[] numbers = new double[10];
    
          for (int counter=0;counter᝺counter++) {
             System.out.println("Please enter a double");
             numbers[counter] = new Double(keyboard.readLine()).doubleValue();
          }
    
          double maximum = numbers[0];
          double minimum = numbers[0];
    
          for (int counter=1;counter᝺counter++) {
             if (numbers[counter] < minimum)
                minimum = numbers[counter];
             if (numbers[counter] > maximum)
                maximum = numbers[counter];
          }
    
          System.out.println("The smallest value you entered is " + minimum + ".");
          System.out.println("The largest value you entered is " + maximum + ".");
       }
    
    }
    
  • Read a line in from a file, tokenize that line, and place the tokens you find into a String array.

    import java.util.*;
    
    public class Tokens {
       int numberOfTokens;
       String[] tokens;
    
       public Tokens(String s) {
          StringTokenizer myStringTokenizer = new StringTokenizer(s);
          numberOfTokens = myStringTokenizer.countTokens();
    
          tokens = new String[numberOfTokens];
    
          for (int counter=0;counter<numberOfTokens;counter++)
             tokens[counter] = myStringTokenizer.nextToken();
       }
    
       public String toString() {
          String output = "\n";
          output += "There are " + numberOfTokens + " tokens in your string\n\n";
          for (int counter=0;counter<numberOfTokens;counter++)
             output += "Token " + (counter+1) + " is " + tokens[counter] + "\n";
          return(output);
       }
    
       public static void main(String[] args) {
          Tokens myTokens = new Tokens("Hello World");
    
          System.out.println(myTokens);
       }
    
    }
    
    The output of this program is
    
    There are 2 tokens in your string
    
    Token 1 is Hello
    Token 2 is World
    
  • Create an array whose elements are 5-minute blocks of time from 7:00 am to 11:00 pm. For times after Noon we will use military time.

    For example the array should contain the following.

    times[0]  = 700;
    times[1]  = 705;
    times[2]  = 710;
    times[3]  = 715;
    times[4]  = 720;
    times[5]  = 725;
    times[6]  = 730;
    times[7]  = 735;
    times[8]  = 740;
    times[9]  = 745;
    times[10] = 750;
    times[11] = 755;
    times[12] = 800;
    ...
    
    public class Times {
    
       public static void main(String[] args) {
          int position = 0;
          int[] times = new int[192];
    
          for (int start=700;start&360=2200;start += 100)
             for (int counter=0;counter<=11;counter++)
                times[position++] = start+counter*5;
    
          for (int counter=0;countercounter++)
             System.out.println("times[" + counter + "] = " + times[counter]);
       }
    
    }
    
    We have discussed several issues related to arrays. We have seen how to declare a new array and to initialize the array when we declare it.

    Another topic that is useful when discussing arrays is sorting. In many instances we need to sort data in order to obtain useful information. Another reason to sort an array is to make finding an element in the array easier.

    Before we talk about finding an element in an array, let's discuss how we would go about sorting the elements in an array.

    Consider the following array.

    numbers[0] =  5;
    numbers[1] = 10;
    numbers[2] =  7;
    numbers[3] =  8;
    numbers[4] =  2;
    
    We would like to rearrange the elements in this array so that they are in ascending order. That is we want our array to be

    numbers[0] =  2;
    numbers[1] =  5;
    numbers[2] =  7;
    numbers[3] =  8;
    numbers[4] = 10;
    
    There is a classic technique that can be used to accomplish this.

    Let's start with the first element in the original array. We want to replace it with the smallest element in the array. One way to do this would be to compare that element with every other element and if one is found that is smaller then we could swap them.

    So if we begin at element 1 and stop at element 4 and compare each of them to element 0, then if we find one that is smaller than element 0 we could swap them. So after making all the comparisons we would have 2 as the first element in the array.

    So now the contents of the array are

    numbers[0] =  2;
    numbers[1] =  5;
    numbers[2] =  7;
    numbers[3] =  8;
    numbers[4] = 10;
    
    Now that position 0 holds the correct number, let's look at position 1. We want the next largest number in position 1. So we could compare it to all the remaining numbers and if we find one that is smaller we could swap them. So after making all the comparisons we would find that 5 is in position 1.

    We could then continue this process until we reach position 3. After comparing position 3 to position 4 if position 4 is smaller than position 3, then we could swap them. After this swap we know that position 3 and position 4 are correct.

    Let us look at what happens to the array after each step.

    number[0] = 2
    number[1] = 10
    number[2] = 7
    number[3] = 8
    number[4] = 5
    
    number[0] = 2
    number[1] = 5
    number[2] = 10
    number[3] = 8
    number[4] = 7
    
    number[0] = 2
    number[1] = 5
    number[2] = 7
    number[3] = 10
    number[4] = 8
    
    number[0] = 2
    number[1] = 5
    number[2] = 7
    number[3] = 8
    number[4] = 10
    

    We can also create two-dimensional arrays. That is, arrays in which the elements are indexed by three things: the array name, a row, and a column.

    Your programming assignment

    Your final program will involve two parts. You will prompt the user for which part they want to execute.

    The first option that you will give the user is to have you sort an array of randomly generated ints between 0 and a certain number. They will input how many numbers you should generate and what the maximum number to be generated is. For example, they might ask you to produce 20 random ints between 0 and 500. You will sort the array and determine the following information.

    The minimum number generated.

    The maximum number generated.

    The number of prime numbers generated.

    The number of even numbers generated.

    The number of odd numbers generated.

    The minimum prime number generated.

    The maximum prime number generated.

    The sum of all prime numbers generated.

    You may produce random numbers using the Math.random() method. It returns a double between 0.0 and 1.0. So to produce a random number between 0 and a certain number, you would multiply the result of Math.random() by that number and cast it as an int.

    The second option that you will present to the user will be for you to produce a table of logarithms for them. The table will be stored in a two-dimensional array.

    The rows in the array will represent tenths of numbers, that is, the first row will represent 1.0, the second row will represent 1.1, the third row will represent 1.2, etc. The last row in the array will represent 10.0.

    The columns in the array will represent hundreths of numbers up to 0.09. That is the columns will represent the numbers 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, and 0.09.

    The user will input what base to use, and each entry in the array will be the logarithm to that base of the sum of the numbers represented by the row and column.

    For example, the first entry in the table will the logarithm to the base of the input number of 1.0 + 0.00.

    The logarithms can be computed using the Math.log() method and the change of base formula



    When the logarithms are produced, truncate them to 3 decimal places.

    Then prompt the user for a filename to use and write the table to that file.