CSC 012

Introduction to Computer Science

 

Summary of 4/10 Lecture

 

We simulated the rolling of a die as follows:

//File:    TestDie.java
public class TestDie
{
    public static void main (String [] args)
    {
           int      k1=0,
                    k2=0,
                    k3=0,
                    k4=0,
                    k5=0,
                    k6=0;
       
           for (int k=0; k<10000; k++)
            {
                    int x=1+ (int)(6*Math.random());
           
                    //The following counts the number of 1's, 2's, 3's, 4's, 5's, 6's
                    switch(x)
                    {
                            case 1: k1++; break;
                            case 2: k2++; break;
                            case 3: k3++; break;
                            case 4: k4++; break;
                            case 5: k5++; break;
                            case 6: k6++;
                    }
            }
           
            System.out.println ("ones:\t" + k1 +
                                    "\ntwos:\t" + k2 +
                                    "\nthrees:\t" + k3 +
                                    "\nfours:\t" + k4 +
                                    "\nfives:\t" + k5 +
                                    "\nsixes:\t" + k6);
        }   
}

The output from this program when I ran it was:

ones:     1620
twos:     1676
threes:   1634
fours:     1710
fives:     1675
sixes:     1685

What do think?  In 10,000 tosses of the die, we got the above distribution of 1's, 2's, 3's, 4's, 5's, and 6's.  Is the computer's die fair?


Example.  Next, we examined a simple algorithm for finding the smallest number in a list of numbers.

    //The following method finds the smallest item in a list of integers
    public static int smallest (int [] list)
    {
        int smallestSoFar;
       
        //The following focuses on smallestSoFar,
        //a variable that is compared with each item
        //in the list in succession. As smaller items
        //are encountered, they are assigned to the variable
        //smallestSoFar.

        smallestSoFar = list[0];
        for (int k=1; k<list.length; k++)
        {
            if (list[k] < smallestSoFar)
            {
                smallestSoFar = list[k];
            }
        }
        return smallestSoFar;
    }

We added this method to our NumberList class.  The following driver program tests this and other methods from NumberList, and illustrates some of the mechanics of creating and using arrays.

//File:    TestNumberList.java
import CSLib.*;
public class TestNumberList
{
    public static void main (String [] args)
    {
        int [] number = {10, 20, 30};     //Reserves exactly 3 memory locations
                                                        //AND assigns: number[0]=10, number[1]=20, number[2]=30

        OutputBox out = new OutputBox();
        out.setSize(350, 150);

        out.println ("The list of numbers is: " );
        NumberList.display(number, out);
       
        out.println ("The smallest number in the list is: "
                            + NumberList.smallest (number));
                           
        out.println ("The average of the list of numbers is: "
                            + NumberList.average (number));
    }
}

wpe9.jpg (9686 bytes)


Lab Exercise.  Write a program to create an array of ints and then find the largest item in the list.  HINT:  Declare an int variable called largest.  Then cycle through the members of the list, comparing the current largest with the next item, and updating if necessary.


Lab Exercise.  Write a method that returns the location at which a number first occurs in a list of integers.  Return -1 if it does not occur at all.  The signature of the method (or function) looks as follows:

public static int find (int number, int [] list)

Write a driver class that tests your function by looking for the value 24 in the list {12, 5, 39, 24, 59, 29, 18, 24}.  It should give the following output:

wpe12E.jpg (5151 bytes)    wpe12F.jpg (9977 bytes)

Test it also by requesting a number that is NOT on the list.


Along the way, we added the following method to our NumberList class to accommodate the input of lists.

    public static int input (InputBox box, int [] list)
    {
        //Prompt the user for how many items in list
        box.setPrompt ("How many items in the list?");
        int n = box.readInt();
       
        //Get each number from the user, one at a time
        for (int k=0; k<n; k++)
        {
            //Prompt the user for next number
            box.setPrompt ("Next number:");
            list [k] = box.readInt();
        }
        return n;
    }

All of our variations and additions are included in the updated NumberList class and can be tested using TestNumberList.

Back to CSC 012 Home Page