ArraysDemo.java - \/= Title Arrays Description This program demonstrates arrays\/= package wsu.Arrays Import classes import java.util.Arrays import

ArraysDemo.java - /= Title Arrays Description This program...

This preview shows page 1 - 2 out of 3 pages.

//============================================================== // // Title: Arrays // Description: // This program demonstrates arrays. // //============================================================== package wsu.Arrays; // Import classes import java.util.Arrays; import java.util.Random; import java.util.Scanner; //============================================================== // class ArraysDemo // This class is called ArraysDemo so that there is no // conflict with class java.util.Arrays. //============================================================== public class ArraysDemo { //-------------------------------------------------------------- // Fields //-------------------------------------------------------------- // Declare constant fields private final static String COLFMTS1 = "%-6s"; private final static String COLFMTS2 = "%6s"; private final static String COLFMTD1 = "%-6d"; private final static String COLFMTD2 = "%6d"; //---------------------------------------------------------------- // randomizeArray //---------------------------------------------------------------- private static void randomizeArray(String heading, int[] arr, int upperLimit) { // Loop to generate and store random numbers in array Random rand = new Random(); for (int i = 0; i < arr.length; i++) arr[i] = rand.nextInt(upperLimit) + 1; System.out.println("\nRandomized numbers in the range 1 to " + upperLimit + " stored in array " + heading + " values"); } //---------------------------------------------------------------- // printArray //---------------------------------------------------------------- private static void printArray(String heading, int[] arr) { // Loop to print array numbers System.out.println("\n" + heading); System.out.println("Array size: " + arr.length); System.out.printf(COLFMTS1 + COLFMTS2 + "%n", "Index", "Value");
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture