CSC 012

Introduction to Computer Science

 

Summary of 4/8 Lecture

Value versus Reference Parameters

As we mentioned earlier this semester, 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
import CSLib.*;
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);
       
        OutputBox out = new OutputBox("Parameter Passing");
        out.setSize(100,100);
       
        NumberList.display(out, x);     //displays the list on the screen
        out.println (y);
    }
}

wpe7.jpg (2737 bytes)

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)
    {
        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
import CSLib.*;
public class ArithmeticDriver
{
    public static void main (String [] args)
    {
        OutputBox out = new OutputBox("Arithmetic");
        out.setSize(150,100);
       
        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. Pretty ridiculous
        //and cumbersome, isn't it?
        Arithmetic s = new Arithmetic();
        out.println (x + " + " + y + " = " + s.add(x, y));
       
        //Since subtract(), multiply(), and divide() are declared static
        //in Arithmetic, they may be accessed as class methods as follows.
        //This is exactly the way in which Math class methods are accessed; e.g.,
        //Math.sqrt(), Math.abs(), etc.
        out.println (x + " - " + y + " = " + Arithmetic.subtract(x, y));
        out.println (x + " X " + y + " = " + Arithmetic.multiply(x, y));
        out.println (x + " / " + y + " = " + Arithmetic.divide(x, y));
    }
}

wpe8.jpg (4131 bytes)

More About Arrays

We attempted to simulate the New York State pick six drawing.  The details can be found at New York Lottery.

//File:    PickSix.java
import CSLib.*;
public class PickSix
{
    public static void main (String [] args)
    {
        int [] pickSix = new int [6];
        for (int k=0; k<6; k++)
        {
            pickSix [k] = 1 + (int)(52*Math.random());
        }
       
        OutputBox out = new OutputBox("Pick Six");
        out.setSize(200, 150);
        out.println ("The Pick Six numbers are: " );
        NumberList.display(pickSix, out);
    }
}

wpeA.jpg (4974 bytes)

Note that the line

            pickSix [k] = 1 + (int)(52*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.


Lab Exercise.  Modify the PickSix program above so that duplicate picks cannot occur.  If you're having difficulty coming up with a strategy, take a look at the revised driver, PickSix.java.   The problem then becomes one of implementing the isInList() method.  When totally exasperated, check the updated NumberList class.


Back to CSC 012 Home Page