20120201

Java Project #11: Array Work

This script shows off various array operations.


/**
 * class ArrayWork.
 * 
 * @author (Andrew Samuels) 
 * @version (04-05-2010)
 */
//import scanner
import java.util.Scanner;

public class ArrayWork
{
    
    public static void main(String[] args)
    {
        //create variables
        int length;
        int i;
        int zeros;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture length from user
        System.out.print("Enter the length of array: ");
        length = keyboard.nextInt();
        
        int[] p = new int[length]; //creates array of length, "length"
        System.out.println();
        
        //read integers one by one
        for(i = 0; i < length; i ++)
        {
            System.out.print("Enter number " + (i + 1) +": ");
            p[i] = keyboard.nextInt();
        }
        
        //print numbers in order user entered them
        System.out.print("Numbers: ");
        System.out.println();
        for(i = 0; i < length; i++)
        {
            System.out.print(p[i] + " ");
        }
        
        
        //find sum and average of of numbers and print them
        int sum = 0;
        for(i = 0; i < length; i++)
        {
            sum = sum + p[i];
        }
        System.out.println();
        System.out.println("Sum: " + sum);
        System.out.println();
        
        double average = sum/(double)length;
        System.out.println("Average: " + average);
        System.out.println();
        
        //find max and min in array and print
        int maximum = p[0];
        for (i = 0; i < length; i++)
        {
            if (p[i] > maximum)
            {
                maximum = p[i];
            }
        }
        System.out.println("Maximum: " + maximum);
        System.out.println();
        
        int minimum = p[0];
        for (i = 0; i < length; i++)
        {
            if (p[i] < minimum)
            {
                minimum = p[i];
            }
        }
        System.out.println("Minimum: " + minimum);
        System.out.println();
        
        //print all positive numbers
        
        System.out.println("Positives: ");
        for (i = 0; i < length; i++)
        {
            if (p[i] > 0)
            {
                System.out.print(p[i]+" ");
            }
        }
        
        //find and print zeros
        zeros = countZeros(p);
        System.out.println("Zeros: " + zeros);
        
        //reverse array and print
        printReverse(p);
        
        
    }
    
    //count zeros method
    private static int countZeros(int[] array)
    {
        int answer = 0;
        int i;
        for(i = 0; i < array.length; i++)
        {
            if(array[i] == 0)
            {
                answer = answer + 1;
            }
         
        }
        return answer;
    }
    
    //print reverse method
    private static void printReverse(int[] array)
    {
        int i;
        for(i = array.length - 1; i >= 0; i--)
        {
            System.out.print(array[i] + " ");
        }
        
    }
   
    //display method
    public static void display(int[] array)
    {
        for(int i = 0; i < array.length; i++)
        {
            System.out.println(array[i]);
        }
    }
}