CSC 160 Lab 4-1: Arrays and Methods

1. An Example Using both Methods and Arrays

Let us say we want to read in a sequence of integers and store them in an array. We need to know how large to make the array, so we will expect the person entering the data to enter the size of the array first, followed by the array elements. We will expect the array elements to be separated by whitespace so we can read them with the nextInt() method of the scanner class.

We will write a static method to implement this logic. The following program contains the static method for reading in an array. It also contains a second method for printing out an array.

package arrayexample;
import java.util.*;
 * @author gcmuganda
public class ArrayExample 
     * The readArray method reads a sequence of white space separated 
     * integers from a scanner and returns an array of integers
     * containing those numbers in order. 
     * @param size: the size of the array and how many numbers to read
     * @param scanner: the scanner to read the numbers from.
     * @return an array containing the numbers that were read.
    static int [] readArray(int size, Scanner scanner)
        // Create an array of length size
        int [] arr = new int[size];
        // Read in the numbers
        for (int k = 0; k < arr.length; k++)
           arr[k] = scanner.nextInt();
        return arr;
     * PrintArray prints to standard output the elements of an array
     * @param arr : the array whose elements should be printed.
    static void printArray(int [] arr)
        for (int k = 0; k < arr.length; k++)
            System.out.print(arr[k] + " ");
        // print an extra blank line
     * Asks for data for two arrays,  reads them, prints them out.
    public static void main(String[] args) 
        Scanner sc = new Scanner(;
        // Read the first array
        System.out.print("Enter the size of an array: ");
        int size1 =  sc.nextInt();
        System.out.print("Enter " + size1 + " numbers and hit enter: ");
        int [] arr1 = ArrayExample.readArray(size1, sc);
        // Read the second array
        System.out.print("Enter the size of an array:");
        int size2 = sc.nextInt();
        System.out.print("Enter " + size2 + " numbers and hit enter: ");
        int [] arr2 =  ArrayExample.readArray(size2, sc);
        // Print the arrays
        System.out.println("The two arrays you entered are: ");

This is what a sample output looks like.

Enter the size of an array: 3
Enter 3 numbers and hit enter: 12 78 -34
Enter the size of an array: 5
Enter 5 numbers and hit enter: 12 34 4 23 2
The two arrays you entered are: 
12 78 -34 
12 34 4 23 2 

A simple way to print arrays of objects

There is a simple way to print an array. To print an array arr, use the Arrays.toString(arr) method call to convert the array to a string, and then print the string.

Here is an example:

import java.util.Arrays;

public class JavaApplication9
    public static void main(String[] args)
        // Array of objects
        String [] stooges = {"Larry", "Curly", "Moe"};
        // Print the array

The output will look like this:

[Larry, Curly, Moe]

What happens here is that the method Arrays.asList() converts an array into a type of object called a list. It turns out that List objects have a sensible toString() method that can be used to print the contents of the list.

2. Lab 4-1 Assignment: Length of Names

You and your friends are planning to take a group of kids on a camping trip. You have to store a list of the kids' names on a very small flash drive, so you are interested in answering questions like, for a given number, say X, how many kids there are whose names have X characters?

You decide to write a program to answer these questions. The program starts by asking the user to enter the number of kids going on the trip. Then the program reads in the names of the kids, prompting for each name, and storing the same into an array. After that the program prints a table of all the kids' names and their name lengths. The program then starts prompting the user to enter positive integers, and for each integer entered, the program prints a list of all names that have that length. The program terminates when the user enters a length of 0.

Here is an example of the program in use:

How many kids are there? 5
Enter name of kid # 0: Joseph
Enter name of kid # 1: Maria
Enter name of kid # 2: Al
Enter name of kid # 3: Bo
Enter name of kid # 4: J Lo
Names of kids with name lengths are: 
Joseph 6
Maria 5
Al 2
Bo 2
J Lo 4
Enter lengths of names as prompted, 0 to terminate.
?: 2
[Al, Bo]
?: 4
[J Lo]
?: 7
?: 6
?: 0

Here is an outline for program that you should use. Notice that it requires you to use two methods besides the main method. You must not change the names or parameter lists of these methods.

import java.util.Arrays;
import java.util.Scanner;

public class JavaApplication8

    public static void main(String[] args)
        // Read the number of kids
        // Read the names of the kids
        // Print all the names of the kids and the length of their names
        // Prompt for sequence of name lengths:
        // Read the lengths and print list of names till 0 is entered
     * Count the number of names in an array that have a certain length
     * @param names: An array of names
     * @param length : an integer length
     * @return the number of names in the array whose length is given
    static int countByLength(String [] names, int length)
        int count = 0;
        return count;
     * Filter an array of names and keep only those names with a given length
     * @param names: an array of names
     * @param length: an integer length
     * @return the array of names that have the given length
    static String [] filterByLength(String [] names, int length)
        String [] filtered;
        return filtered;

To print the lists of names, you can use the technique of converting an array to a string that is shown above, or you can modify the printArray from the example program so that it prints an array of strings.

5. Due Date

The lab project is due Friday of Week 4.