package arrayAndArrayList; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; /** * Demonstrates the use of arrays and ArrayList's. Students: Do the TODo's in * this file in some sensible order. * * @author TODO Put your name here. Created September 27, 2009. */ public class Main { /** * Students: These constants may be used in our grading. Please do NOT * monkey with them. */ private static final String TEST_FILENAME = "TestFile.txt"; private static final int NUMBER_OF_BIG_INTEGERS_IN_TESTFILE = 1000; private static final int DIGITS_IN_RANDOM_BIG_INTEGERS = 14; private static final long SEED = 1234567890; private static Scanner SCANNER = null; /** * Demonstrates the use of arrays and ArrayList's. * * @param commandLineArguments * Command-line arguments, unused here. */ public static void main(String[] commandLineArguments) { // Students: do not change the next line. Main.generateTestFile(Main.NUMBER_OF_BIG_INTEGERS_IN_TESTFILE); // TODO Write the statement that declares and allocates an array of // BigInteger's capable of holding 35 BigInteger's. // NOTE: The number 35 should NOT be mentioned anywhere other than in // this statement. // TODO Write the Main.fillArray method described below. Then return to // here to write the statement that uses Main.fillArray below to fill // your array with BigInteger's. // TODO Write the Main.printArray method described below. Then return to // here to write the statement that uses Main.printArray below to print // your array. // TODO Write the Main.maxInArray method described below. Then return to // here to write the statement that uses Main.maxInArray below to print // the largest element in your array. // TODO Use the sort method in the Arrays class to sort your array. Then // print the last (hence largest) element in the sorted array. // TODO Set the value of clone1 below by calling Main.cloneArray below. // Set the value of clone2 below by calling the copyOf method of the // Arrays class, sending it your array. Set the value of clone3 below by // using the clone method that all objects have. Then use the equals // method of the Arrays class to compare the three cloned arrays -- they // should all be equal to each other, as measured by the equals method // of the Arrays class. BigInteger[] clone1; BigInteger[] clone2; BigInteger[] clone3; // TODO Write the statement that declares a List of BigInteger's and // constructs it to be a new ArrayList of BigIntegers. // TODO Write the Main.fillList method described below. Then return to // here to write the statement that uses Main.fillList below to fill // your List with 15 BigInteger's. NOTE: The number 15 should NOT be // mentioned anywhere other than in this statement. // TODO Write the Main.printList method described below. Then return to // here to write the statement that uses Main.printList below to print // your List. // TODO Write the Main.maxInList method described below. Then return to // here to write the statement that uses Main.maxInList below to print // the largest element in your List. // TODO Use the max method in the Collections class to print the largest // element in your List. // TODO Use the sort method in the Collections class to sort your List. // Then print the last (hence largest) element in the sorted List. // TODO Write the statement that sets your array element at index 13 to // a BigInteger whose decimal value is 8. // TODO Write the statement that sets your List element at index 13 to a // BigInteger whose decimal value is 8. // TODO Write the statement that declares and constructs a List that is // an ArrayList of Integer's. // TODO Write the statements that put 17, then 13, into your List of // Integer's. Use autoboxing. // TODO Write the statement that replaces the number at index 0 of your // List of Integer's by three times what it was. Requirement: You can // use "3" and "0" but NO OTHER NUMBERS in this statement. Use // autoboxing. // TODO Here are three lists. For each, use // Main.numberAppearingMoreThanOnce below to print the number of entries // in the List that appear more than once. The answers are: 24; 26,408; // and 99,944, respectively. List list1 = Main.generateListOfSmallIntegers(100, 100); List list2 = Main.generateListOfSmallIntegers(100000, 100000); List list3 = Main.generateListOfSmallIntegers(1000000, 100000); } /* * Fills the given array of BigInteger's by repeatedly calling * Main.nextBigInteger for the next BigInteger to put in the array. */ // TODO Write the fillArray method, per its description above. /* * Prints the contents of the given array of BigInteger's, in the order that * they appear in the array. Implementation requirement: Use a new-style FOR * loop. */ // TODO Write the printArray method, per its description above. /* * Returns the biggest number in the given array of BigInteger's. Assumes * that the given array is not empty. Implementation requirement: Use a FOR * loop. */ // TODO Write the maxInArray method, per its description above. /* * Returns a shallow copy of the given array of BigIntegers. Implementation * requirement: Use a FOR loop. */ // TODO Write the cloneArray method, per its description above. /* * Takes an empty List of BigInteger's and an integer n. Fills the given * List of BigInteger's by calling Main.nextBigInteger n times, adding the * result to the end of the List each time. */ // TODO Write the fillList method, per its description above. /* * Prints the contents of the given List of BigInteger's, in the order that * they appear in the List. Implementation requirement: Use a new-style FOR * loop. */ // TODO Write the printList method, per its description above. /* * Returns the biggest number in the given List of BigInteger's. Assumes * that the given List is not empty. Implementation requirement: Use a FOR * loop. */ // TODO Write the maxInList method, per its description above. /* * Uses the histogram pattern to determine the number of entries in the * given List that appear more than once in the List, and returns the result * as a long (NOT int). Assumes that the List contains integers between 0 * and 100,000, inclusive. Implementation requirement: Use autoboxing where * appropriate. */ // TODO Write the numberAppearingMoreThanOnce method, per its description // above. /* * Returns the next BigInteger from Main.TEST_FILENAME. Students: Do NOT * modify this method. */ private static BigInteger nextBigInteger() { if (Main.SCANNER == null) { try { Main.SCANNER = new Scanner(new File(Main.TEST_FILENAME)); } catch (FileNotFoundException exception) { System.out.println("I could not open the test file:\n " + Main.TEST_FILENAME + "\nfor input. I will exit the program now."); exception.printStackTrace(); System.exit(1); } } return Main.SCANNER.nextBigInteger(); } /* * Fills the test file with the given number of pseudo-random BigIntegers. * Students: Do NOT modify this method. */ private static void generateTestFile(int numberOfBigIntegersToGenerate) { PrintWriter output = null; Random randomNumberGenerator = new Random(Main.SEED); try { output = new PrintWriter(Main.TEST_FILENAME); } catch (FileNotFoundException exception) { System.out.println("I could not open the test file:\n " + Main.TEST_FILENAME + "\nfor output. I will exit the program now."); exception.printStackTrace(); System.exit(1); } for (int j = 0; j < numberOfBigIntegersToGenerate; ++j) { String bigIntegerAsString = ""; // Each digit is chosen at random. for (int k = 0; k < Main.DIGITS_IN_RANDOM_BIG_INTEGERS; ++k) { bigIntegerAsString += (int) (randomNumberGenerator.nextDouble() * 10); } output.println(new BigInteger(bigIntegerAsString)); } output.close(); } /* * Returns a List of Integer's of the given size filled with single or * double-digit integers. Students: Do NOT modify this method. */ private static List generateListOfSmallIntegers(int sizeOfList, int largestIntegerToConsider) { Random randomNumberGenerator = new Random(Main.SEED); List listOfSmallIntegers = new ArrayList(); for (int k = 0; k < sizeOfList; ++k) { listOfSmallIntegers .add((int) (randomNumberGenerator.nextDouble() * (largestIntegerToConsider + 1))); } return listOfSmallIntegers; } }