Assignment 20
Due Thursday, March 22 at 11:20am

Assignment 20.1: More Barnyard Animals (10 pts)

  • In this assignment we write a program that stores 5 Strings in a String array, and 5 ints in an integer array and then writes the contents of the two arrays to the console.
  • Create a new class named Animals2.
  • At the top of main declare a String array named animals of length 5. 
  • Then, let's declare an integer array of length 5 named number_animals.
  • Store the following values in the String array, one at each index:
"sheep"
"donkeys"
"goats"
"geese"
"cats"
  • Remember: you can do this one of two ways:
Option 1: static initialization:

String animals[SIZE] = {"sheep", "donkeys", "goats", "geese", "cats"};

Option 2: Non-static initialization:

String animals[SIZE] = new String[];
animals[0] = "sheep";
animals[1] = "donkeys";
//What goes here?
animals[4] = "cats"
  • Pick ONE of the above options to store the string values in your string array.
  • Next, pick one option to store the following values in the integer array (number_animals), one at each index:

5
3
8
2
4
  • We are now going to write the contents of our arrays to the console.
  • First, inside of main, add the following print statement:

    System.out.println("Other animals at our farm:");

  • Next, write a method, which uses a for loop to print out the the two arrays, side-by-side, like this:
5 sheep
3 donkeys
8 goats
2 geese
4 cats
  • The method is named printArray. It takes in two array parameters. It prints out the message "Other animals at our farm:" and, below that message, it prints the arrays side-by-side (as shown above). It returns nothing.
  • See the class notes if you get stuck for an example of using for loops and methods with arrays.
  • Finally, compile and run your code and verify that the following output appears in the console window:

    Other animals at our farm:
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your program to Canvas when you are finished



Assignment 20.2: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new Java project called Nums
  • Prompt a user to enter in the size of the array (the number of numbers).
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0


  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload Nums.java to Canvas when you are finished.


Activity 20.3: Arrays and Methods Worksheet (10 pts)

  • Copy the following into a new file called ArrayMethods.java.
  • Write the methods one-by-one. Compile and run your code after you write each method to make sure it was written properly.
  • When your code passes all of the tests, upload your file to Canvas.
public class ArrayMethods {

    /**
     * prints the contents of an array to the console using a for loop
     * @param data the array to print
     */
    public static void printArray(int[] data) {
        return;
    }

    
    /**
    * Given an array of ints, return true if 10 appears as
    * either the first or last element in the array. The array will be size 1 or more.
    * first10Last([1, 2, 10]) → true
    * first10Last([10, 1, 2, 3]) → true
    * first10Last([13, 10, 1, 2, 3]) → false
    * @param data the array of integer values
    * @return whether or not 10 appears as the first or last element
    */
    public static boolean first10Last(int data[]) {
        return false;
    }
    
    /**
    * Given an array of ints, return true if the array is size 1 or more,
    * AND the first element and the last element are equal.
    * equalFirstLast([1, 2, 3]) → false
    * equalFirstLast([1, 2, 3, 1]) → true
    * equalFirstLast([1, 2, 1]) → true
    * @param array the array of integers
    * @return whether the first and last element are identical
    */
    public static boolean equalFirstLast(int[] array){
        return false;
    }
    
    /**
    * Given an array of ints, add 2 to each element in the array
    * return nothing.
    * arrayPlus2([1,2,3]) -> [3, 4, 5]
    * arrayPlus2([10, 30, 50, 79, 85]) -> [12, 32, 52, 81, 87]
    * arrayPlus2([5]) -> [7]
    * @param array the array of integer values
    */
    public static void arrayPlus2(int array[]) {
        return;
    }
    
    /**
     * Given an array of ints, multiplies each element in the array by itself
     * squareArray([1,2,3]) -> [1, 4, 9]
     * squareArray([3, 5, 6, 8, 9]) -> [9, 25, 36, 64, 81]
     * squareArray([5]) -> [25]
     * @param array the array of integer values
     */
    public static void squareArray(int[] array){
        return;
    }
    

    public static void main(String[] args)
    {
      
        boolean answer;
      
        System.out.println("***Testing first10Last***\n");
        int array1[] = {1, 2, 10};
        answer = first10Last(array1);
        System.out.println("Should print true: " + answer);
        int array2[] = {10, 1, 2, 3};
        answer = first10Last(array2);
        System.out.println("Should print true: " + answer);
        int array3[] = {13, 10, 1, 2, 3};
        answer = first10Last(array3);
        System.out.println("Should print false: " + answer);
        System.out.println();

        System.out.println("***Testing equalFirstLast***\n");
        int array4[] = {1, 2, 3};
        answer = equalFirstLast(array4);
        System.out.println("Should print false: " + answer);
        int array5[] = {10, 20, 50, 60, 80, 90, 10};
        answer = equalFirstLast(array5);
        System.out.println("Should print true: " + answer);
        int array6[] = {1};
        answer = equalFirstLast(array6);
        System.out.println("Should print true: " + answer);
        System.out.println();

        System.out.println("***Testing arrayPlus2***\n");
        int array7[] = {1, 2, 3};
        arrayPlus2(array7);
        System.out.println("Should print 3 4 5: ");
        printArray(array7);
        System.out.println();
        int array8[] = {10, 30, 50, 79, 85};
        arrayPlus2(array8);
        System.out.println("Should print 12 32 52 81 87: ");
        printArray(array8);
        System.out.println();
        int array9[] = {5};
        arrayPlus2(array9);
        System.out.println("Should print 7: ");
        printArray(array9);
        System.out.println("\n");

        System.out.println("***squareArray***\n");
        int array10[] = {1, 2, 3};
        squareArray(array10);
        System.out.println("Should print 1 4 9: ");
        printArray(array10);
        System.out.println();
        int array11[] = {3, 5, 6, 8, 9};
        squareArray(array11);
        System.out.println("Should print 9 25 36 64 81: ");
        printArray(array11);
        System.out.println();
        int array12[] = {5};
        squareArray(array12);
        System.out.println("Should print 25: ");
        printArray(array12);
        System.out.println("\n");

        System.out.println("***End of Tests***");
       


    }

}