Library  Lab 0
 Lab 1
 Lab 2
 Lab 3
 Lab 4
 Lab 5
 Welcome to Lab 6
 Lab 7
 Lab 8
 Examples Page

Welcome to Lab 6

By the end of this Lab you should be able to do the following.

Lab Topics

Lets say you had to store ten thousand values in your program. This could be a long list of almost anything, lets say it is ten thousand whole numbers. Normally this would mean creating ten thousand variables to hold all this information. I do not know about you, but I do not want to spend a few days typing out ten thousand variable names. The Java library provides an ArrayList to handle this (and others as well) situation. We can create and fill an arrayList with data and refer to each location in the array by its index number rather than a variable name so we do not have to invent ten thousand names. Using your editor create a file called stuff.dat with 30 integers in it. Make sure the numbers have a space or a new line between each one.


/**
 * ThirtyNumbers is a program that reads 30 integers from a file and 
 * fins the largest and smallest ones.
 * 
 * @author Allan kranz
 * @version 1.0.0.0
 */

import java.io.FileInputStream;
import java.util.Scanner;

public class ThirtyNumbers
{
    public static void main(String[] args)
    {
        ThirtyNumbers thirty = new ThirtyNumbers();
        thirty.run();
    }

    /**
     * Constructor for objects of class ThirtNumbers
     */
    public ThirtyNumbers()
    {    
    }

    /**
     * run - runs the calculation to find the smallest 
     * and largest numbers
     * 
     * @param  none
     * @return none     
     */
    public void run()
    {
        // smallest so far, will have smallest at end, 
        // start at largest possible Java int
        int ssf = Integer.MAX_VALUE;
        // largest so far, will have largest at end,
        // start at smallest possible Java int
        int lsf = Integer.MIN_VALUE; 
        int number;
		String filename = "data.dat";
        try{
            FileInputStream fstream = new FileInputStream(filename);
			Scanner fin = new Scanner(fstream);

            // Continue to read numbers while there are still 
            // some left to read
            while (fin.hasNextInt()){
                number = fin.nextInt();
                if( number > lsf ){
                    lsf = number;
                }
                
                if(number < ssf){
                    ssf = number;
                }
			}

			fin.close();
			
			System.out.println("Largest number is "+lsf);
			System.out.println("Smallest number is "+ssf);
		} 
        catch (Exception e){
			System.err.println("File error with "+filename);
		} 
    }
}

The following code shows how to load ints from a file into an ArrayList. There are some hints on handling excpetions that you can incorporate if you wish. The constructort method for the class is used here to create the new ArrayList, it is common practice to allocate the resources your class will need in the constructor. This exampel also demonstrates the enhanced for loop used with ArrayList.
/**
 * An example of filling an ArrayList from a file of ints.
 * 
 * @author Allan Kranz
 * @version 1.0.0
 */

import java.util.ArrayList;
import java.io.File;
import java.util.Scanner;

public class FileAndArrayList
{
    private ArrayList<Integer> numbers;
    private String filename = "data.txt";
    
    /** Constructor for objects of class FileArray */
    public FileAndArrayList(){
        numbers = new ArrayList<Integer>(100);
    }

    /** The run method where everything actually happens
     * @param   void
     * @return  void  */
    public void run(){
        try{
            File file = new File(filename);
            Scanner fin = new Scanner(file);
        
            while(fin.hasNextInt()){
                numbers.add(fin.nextInt());
            }
            
            for(Integer number:numbers){
                System.out.print(number+" ");
            }
            System.out.print("\n");
            
            java.util.Collections.sort(numbers);
            
            for(Integer number:numbers){
                System.out.print(number+" ");
            }
            System.out.print("\n");
        }
        catch(java.io.FileNotFoundException e){
             System.out.println("Error opening "+filename+", ending program");
             System.exit(1);
        }
    }

    public static void main(String[] args){
        FileAndArrayList f = new FileAndArrayList();
        f.run();
    }

}

Programming Problem

Write and turn in a program which reads the numbers from a data file and does the following things. Use an ArrayList and loops. The data comes from a sound file. I have extracted 8886 values form this file where each value represents the sound level of that channel for the next 44 thousandth of a second. Here is the data file, data.dat