CSC 012

Introduction to Computer Science

Summary of 11/15 Lecture

More on classes and arrays.

Value versus Reference Parameters

There are essentially two kinds of data types in Java, primitive data types and the rest.  The primitive data types include int, double, char, and boolean.   The distinction between these two categories of data types is especially important in the context of passing parameters to functions or methods.

Example.

//File:    ParameterPassing.java
public class ParameterPassing
{
    static void change (int [] a,      //a reference (-->)parameter
                                   int      b     //a value parameter
                                   )
    {
        //a --> x[0] x[1] x[2] in main()
        //b = 2 has its own memory location
        //     and is initialized to have
        //    the value currently residing in y
       
        a[0] =1;    //changes x[0] from 10 to 1
        b=3;         //does NOT change y
    }
   
   public static void main (String [] args)
    {
        int [] x = {10, 20, 30};
        int y = 2;
       
        change (x, y);
       
        NumberList.display(x);     //displays the list on the screen
        System.out.println (y);
    }
}

>java ParameterPassing
1
20
30
2

Note that, as expected, the first element of the x array, x[0], has changed from 10 to 1, while the value of y remains unchanged.  To summarize, primitive data types are passed by value while non-primitive data types are passed by reference (-->).  Primitive data types can therefore not be changed or updated by another method, but non-primitive data types can.  It would seem that caution is in order when passing non-primitive data types to another method.  They might be inadvertently updated.

More on Class Methods vs Instance Methods

The following example illustrates once again the distinction between class methods and instance methods.

//File:    Arithmetic.java
public class Arithmetic
{
    public         int add (int a, int b)     //Without the access modifier static, this method will be
    {                                                     //an instance method
        int c=a + b;   
        return c;
        //equivalent to: return a+b;
    }
   
    public static int subtract (int a, int b)
    {
        return a - b;
    }
   
    public static int multiply (int a, int b)
    {
        return a * b;
    }
   
    public static int divide (int a, int b)
    {
        return a / b;
    }
}

//File:    ArithmeticDriver.java
public class ArithmeticDriver
{
        public static void main (String [] args)
    {
        int x = 7, y = 3;
       
        //Since add() was NOT declared static in Arithmetic,
        //it is an instance method and so requires that we
        //instantiate an object of the Arithmetic class here.
       
        Arithmetic s = new Arithmetic();        //Pretty ridiculous and cumbersome, isn't it?
        System.out.println (x + " + " + y + " = " + s.add(x, y));
       
        //Since subtract(), multiply(), and divide() WERE declared static
        //in Arithmetic, they may be accessed as class methods as follows.

        System.out.println (x + " - " + y + " = " + Arithmetic.subtract(x, y));    //This is exactly the way in which Math class
        System.out.println (x + " X " + y + " = " + Arithmetic.multiply(x, y));    //methods are accessed; e.g.,
        System.out.println (x + " / " + y + " = " + Arithmetic.divide(x, y));         ////Math.sqrt(), Math.abs(), etc.
    }
}

More About Arrays

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;
    }
   
Finally, we encapsulated the code necessary to display our number list as follows:

    //The following method outputs to the screen a list of integers
    public static void display (int [] list)
    {
        for (int k=0; k<list.length; k++)
        {
            System.out.println (list[k]);
        }
    }

Since the above two methods should be generally useful when processing an array or list of numbers, we'll add them to the NumberList class.

The following driver program test drives our NumberList class and illustrates some of the mechanics of creating and using arrays.

//File:    TestNumberList.java
import iostuff.*;
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

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

>java TestNumberList
The list of numbers is:
10
20
30
The smallest number in the list is: 10
The average of the list of numbers is: 20

Finally, we attempted to simulate the New York State pick six drawing.

//File:    PickSix.java
public class PickSix
{
    public static void main (String [] args)
    {
        int [] pickSix = new int [6];
        for (int k=0; k<6; k++)
        {
            pickSix [k] = (int)(53*Math.random());
        }
       
        System.out.println ("The list of pickSixs is: " );
        NumberList.display(pickSix);
    }
}

>java PickSix
The list of pickSixs is:
11
20
16
6
21
18

Note that the line

            pickSix [k] = (int)(53*Math.random());

has a peculiar parenthetical (int) just before the random number generator.  This is called a cast and is required here since the random number generator gives a double, not an int, as required by the array.


At Home Exercise.  Modify the PickSix program above so that duplicate picks cannot occur.


Adapted from Bill Steinmetz's course summary by H. J. Bernstein, 12 Nov 01

Back to CSC 012 Home Page