Colorado State University

Lab 14

CS160: Foundations in Programming

Java Methods


The purpose of this lab is to introduce methods in Java.
Instructions: Your GTA will lead you through the following:

Consider the following Java program. It has a number of methods with empty implementations. Your task is to complete the methods so that they perform the functions described by their names. For example, the method getAverage(int[] a) must calculate the average of the elements of the array a. The program also calls the method printArray(int[] a). After completing the method bodies write statements to call those methods on your array.

public class ArrayMethods {
    public static void main (String [] args) {
	ArrayMethods arrayMethods = new ArrayMethods();
	int[] a = arrayMethods.createArray();
	arrayMethods.printArray(a);
	    
    }
    
    public /* What type goes here? */ createArray() {

    }

    public void printArray(int[] a) {
	    
    }
    
    public int getSum(int[] a) {
	    
    }
    
    public double getAverage(int[] a) {
	    
    }
    
    public int getMax(int[] a) {
	    
    }
}
	

© 2009 CSU