The assignment in lab is to design and implement four methods. 

Last time: methods

We have defined, discussed static methods:

public class Utilities {
  public static double halve(double number) {
    double answer; // local variable 
    answer = number / 2;
    return answer; 
  } 
  public static boolean isPrime(int number) {
    for (int i = 2; i < number; i=i+1) {
      if (number % i == 0) {
        return false; 
      }
    } 
    return true;
  }  
  public static void main(String[] args) {
    System.out.println( Utilities.halve( 3 ) ); 
    System.out.println( Utilities.isPrime( 3 ) ); 
    System.out.println( Utilities.isPrime( 4 ) ); 
    System.out.println( Utilities.isPrime( 5 ) ); 
    
  }
}

Next we modified the code to add a method that uses one of the 
existing methods and the entire class became: 

public class Utilities {
  public static double halve(double number) {
    double answer; // local variable 
    answer = number / 2;
    return answer; 
  } 
  public static boolean isPrime(int number) {
    for (int i = 2; i < number; i=i+1) {
      if (number % i == 0) {
        return false; 
      }
    } 
    return true;
  }  
  public static void showPrimes(int number) {
    for (int i = 1; i <= number; i=i+1) {
      if (Utilities.isPrime(i)) {
        System.out.println( i + " is prime.");
      } else {
        System.out.println( i + " is NOT prime.");
      }
    }
  }
  public static void main(String[] args) {
    System.out.println( Utilities.halve( 3 ) ); 
    System.out.println( Utilities.isPrime( 3 ) ); 
    System.out.println( Utilities.isPrime( 4 ) ); 
    System.out.println( Utilities.isPrime( 5 ) ); 
    Utilities.showPrimes(13);    
  }
}

Now let's talk about arrays.

You know about local variables, such as: 

  int n; // declaration, n has no value in it yet
  n = 4; // initialization, I can use n now 

  Scanner duke; // declaration, duke is just a name, no dog yet
  duke = new Scanner(System.in); // initialization 

  String a; // declaration, no value yet
  a = "whatever"; // a is initialized and can be used
  System.out.println( a.length() ); // prints 8
  System.out.println( a.substring(1, 4) ); // prints "hat"
  a = new String("whassup"); 
  System.out.println( a.substring(1, 4) ); // prints "has"

  int[] numbers; // declaration of a name, numbers
                 // it will refer to an array of int values
                 
  numbers = new int[10]; // allocates the memory for the array 
                         // I will have 10 slots for ten values
                         // those slots currently contain zeroes

  System.out.println( numbers );

Let's start experimenting with arrays: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] numbers;
> numbers
null
> numbers = new int[10]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> numbers[3] = 12;
> numbers
{ 0, 0, 0, 12, 0, 0, 0, 0, 0, 0 }
> int values[];
> values
null
> values = new int[4]
{ 0, 0, 0, 0 }
>

So encouraged by this I am going to try this in Dr.Java:

public class Utilities {
  public static void main(String[] args) {
     int[] numbers; 
     numbers = new int[10];
     System.out.println( numbers ); 
     int values[];
     values = new int[4];
     System.out.println( values ); 
  }
}

I compile and run and I get this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Utilities
[I@9c4b8a
[I@111771e


So I am disappointed and need to use tools to get the same output:

import java.util.Arrays;

public class Utilities {
  public static void main(String[] args) {
     int[] numbers; 
     numbers = new int[10];
     System.out.println( Arrays.toString( numbers )); 
  }
}

And now things are working:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Utilities
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


Here's another way to initialize an array:

import java.util.Arrays;

public class Utilities {
  public static void main(String[] args) {
    int[] numbers = { 3, 4, -2, 5}; 
    System.out.println( Arrays.toString( numbers )); 
  }
}

This runs and produces:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Utilities
[3, 4, -2, 5]


I would like to define a method now that generates an array
of given size (user-specified) of random values. The values
will be ints and the range will also be specified by the user. 

Here's my implementation: 

import java.util.Arrays; 

public class Utilities {
  public static void main(String[] args) {
    System.out.println(Arrays.toString( Utilities.generate( 6, -50, 50) ));
  }
  public static int[] generate(int size, int low, int high) {
    int[] answer; // declaration 
    answer = new int[size]; // allocation
    for (int i = 0; i < answer.length; i = i + 1) {
      answer[i] = (int) ((high - low) * Math.random() + low); 
    }
    return answer;
  }
}

We add two more methods now:

import java.util.Arrays; 

public class Utilities {
  public static void main(String[] args) {
    System.out.println(Arrays.toString( Utilities.generate( 6, -50, 50) ));
    int[] a = Utilities.generate(10, -30, 40); 
    System.out.println(Arrays.toString( a )); 
    int[] b = Utilities.reverse(a); 
    System.out.println(Arrays.toString( b )); 
    System.out.println( Utilities.sum( a ) );
    System.out.println( Utilities.sum( b ) );
  }
  public static int[] generate(int size, int low, int high) {
    int[] answer; // declaration 
    answer = new int[size]; // allocation
    for (int i = 0; i < answer.length; i = i + 1) {
      answer[i] = (int) ((high - low) * Math.random() + low); 
    }
    return answer;
  }
  public static int[] reverse(int[] values) {
    int[] answer;
    answer = new int[values.length];
    for (int i = 0; i < values.length; i = i + 1) {
      answer[i] = values[values.length - 1 - i] ; 
    }
    return answer;
  }
  public static int sum(int[] n) {
    int sum = 0;
    for (int i = 0; i < n.length; i++) {
      sum += n[i]; 
    }
    return sum;
  }
  
}

This is how the code works if we compile and run it:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Utilities
[-31, 22, 33, -8, 30, 33]
[11, -21, 9, 38, -21, 34, -8, 33, 4, -4]
[-4, 4, 33, -8, 34, -21, 38, 9, -21, 11]
75
75
> run Utilities
[-13, -5, 28, 6, -34, 14]
[-18, -9, 3, 32, 21, -12, 31, 3, -10, 23]
[23, -10, 3, 31, -12, 21, 32, 3, -9, -18]
64
64


See you in lab.

--