setfromint import components.naturalnumber.NaturalNumber; import components.naturalnumber.NaturalNumber2; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; /** * Program to test arrays, references, and arrays of references. * * @author Put your name here * */ setfromint How to get it for free? setfromint public final class ArraysAndReferences { /** * Private constructor so this utility class cannot be instantiated. */ private ArraysAndReferences() { } /** * Computes the product of the {@code NaturalNumber}s in the given array. setfromint How to dowload it? setfromint * * @param nnArray * the array * @return the product of the numbers in the given array * @requires nnArray.length > 0 * @ensures
     * productOfArrayElements =
     *    [nnArray[0] * nnArray[1] * ... * nnArray[nnArray.length-1]]
     * 
*/ setfromint How to dowload it? setfromint private static NaturalNumber productOfArrayElements( NaturalNumber[] nnArray) { assert nnArray != null : "Violation of: nnArray is not null"; assert nnArray.length > 0 : "Violation of: nnArray.length > 0"; NaturalNumber product = nnArray[0].newInstance(); product.setFromInt(1); for (int i = 0; i < nnArray.length; i++) { product.multiply(nnArray[i]); setfromint How to use it? setfromint } return product; } /** * Replaces each element of {@code nnArray} with the partial product of all * the elements in the incoming array, up to and including the current * element. * setfromint How to use it? setfromint * @param nnArray * the array * @updates nnArray * @requires nnArray.length > 0 * @ensures
     * for all i: integer where (0 <= i < nnArray.length)
     *   (nnArray[i] = [#nnArray[0] * #nnArray[1] * ... * #nnArray[i]])
     * 
*/ private static void computePartialProducts(NaturalNumber[] nnArray) { setfromint How to use it? setfromint assert nnArray != null : "Violation of: nnArray is not null"; assert nnArray.length > 0 : "Violation of: nnArray.length > 0"; // Have to start by replacing the last term of the array for (int i = nnArray.length - 1; i >= 0; i--) { NaturalNumber product = nnArray[i].newInstance(); product.setFromInt(1); for (int j = 0; j <= i; j++) { // multiply all the previous (inclusive) terms product.multiply(nnArray[j]); setfromint How to dowload it? setfromint } nnArray[i].copyFrom(product); // set the value to the partial product } for (int i = 0; i < nnArray.length; i++) { System.out.print(nnArray[i] + " "); } } /** setfromint How to get it for free? setfromint * Creates and returns a new array of {@code NaturalNumber}s, of the same * size of the given array, containing the partial products of the elements * of the given array. * * @param nnArray * the array * @return the array of partial products of the elements of the given array * @requires nnArray.length > 0 * @ensures
     * partialProducts.length = nnArray.length  and
setfromint How to dowload it? setfromint
     *  for all i: integer where (0 <= i < partialProducts.length)
     *    (partialProducts[i] = [nnArray[0] * nnArray[1] * ... * nnArray[i]])
     * 
*/ private static NaturalNumber[] partialProducts(NaturalNumber[] nnArray) { assert nnArray != null : "Violation of: nnArray is not null"; assert nnArray.length > 0 : "Violation of: nnArray.length > 0"; NaturalNumber[] productArray = new NaturalNumber[nnArray.length]; NaturalNumber product = nnArray[0].newInstance(); setfromint How to dowload it? setfromint // Start at the end for (int i = nnArray.length - 1; i >= 0; i--) { product.setFromInt(1); // set to 1 each time we compute the product productArray[i] = nnArray[i].newInstance(); for (int j = 0; j <= i; j++) { product.multiply(nnArray[j]); } productArray[i].copyFrom(product); } setfromint PasteShr setfromint return productArray; } /** * Main method. * * @param args * the command line arguments */ setfromint How to use it? setfromint public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L(); /* * Initialize an array of NaturalNumbers with values 1 through 42. */ NaturalNumber[] array = new NaturalNumber[10]; NaturalNumber count = new NaturalNumber2(1); for (int i = 0; i < array.length; i++) { array[i] = count.newInstance(); setfromint How to get it? setfromint array[i].copyFrom(count); count.increment(); } for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } out.println(); setfromint How to dowload it? setfromint /* * Compute and output the product of the numbers in the array (should be * 42!, i.e., the factorial of 42). */ NaturalNumber product = productOfArrayElements(array); out.println(product); NaturalNumber[] testing = partialProducts(array); for (int i = 0; i < testing.length; i++) { setfromint PasteShr setfromint System.out.print(testing[i] + " "); } out.println(); computePartialProducts(array); out.close(); } } setfromint How to get it? setfromint setfromint