Assignment 20
Due Thursday, December 6 at 9:20am on Canvas


Assignment 20.1: Arrays and Methods Worksheet 2 (10 pts)

  • Copy the following into a new file called ArrayMethods2.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.
/**
 * Assignment 20.1
 * @author Your Name
 * @version 1.0 Date Completed
 */
public class ArrayMethods2 {

    /**
    * Given an array of ints, return the index of the first appearance of the
    * number 42. If not found return -1.
    * @param data array of ints
    * @return the index of the first number 42, -1 if not found.
    * Test cases:
    * findFirst42({1, 1, 42, 3, 1}) --> 2
    * findFirst42({1, 1, 2, 42, 1}) --> 3
    * findFirst42({1, 1, 2, 1, 2, 3}) --> -1
    */
    public static int findFirst42(int[] data) {
       
        return -1;
    }

    /**
     * Given an array of ints, return true if
     * the sequence .. 1, 2, 3, .. appears
     * in the array somewhere.
     * @param data array of ints
     * @return true if .. 1, 2, 3, .. appears in the array,
     * false otherwise.
     * Test cases:
     * is123({1, 2, 3, 1}) --> true
     * is123({1, 2, 4, 1}) --> false
     * is123({1, 2, 1, 2, 3}) --> true
     */
    public static boolean is123(int[] data) {
       
        return false;
    }

    /**
     * Prints all the elements in an array to the console
     * on one line with a space between each element.
     * @param data The array to print.
     */
    public static void printArray(int[] data) {
       
        System.out.println();
    }

    /**
     * Given an array of ints, delete first appearance of the number 42.
     * By replacing it with a 0.
     * If the number does not exist in the array, leave the array unchanged.
     * @param data array of ints
     * Hint: use return; to end the method early!
     * Test cases:
     * eraseFirst42({1, 2, 42, 3, 1}) --> {1, 2, 0, 3, 1}
     * eraseFirst42({1, 2, 3, 42, 42, 1}) --> {1, 2, 3, 0, 42, 1}
     * eraseFirst42({1, 2, 3}) --> {1, 2, 3}
     */
    public static void eraseFirst42(int[] data) {
        return;
    }



    public static void main(String[] args) {
        boolean answer = false;
        int num = 0;
       
        System.out.println("***Testing findFirst42***");
        final int A3 = 3, A42 = 42;
        int[] data1 = {1, 2, A42, A3, 1};
        num = findFirst42(data1);
        System.out.println("findFirst42a should be 2: " + num);
        int[] data2 = {1, 1, 2, A42, 1};
        num = findFirst42(data2);
        System.out.println("findFirst42a should be 3: " + num);
        int[] data3 = {1, 1, 2, 1, 2};
        num = findFirst42(data3);
        System.out.println("findFirst42a should be -1: " + num);

        System.out.println("\n***Testing is123***");
        final int A4 = 4;
        int[] data4 = {1, 2, A3, 1};
        answer = is123(data4);
        System.out.println("is123a should be true: " + answer);
        int[] data5 = {1, 2, A4, 1};
        answer = is123(data5);
        System.out.println("is123a should be false: " + answer);
        int[] data6 = {1, 2, 1, 2, A3};
        answer = is123(data6);
        System.out.println("is123a should be true: " + answer);
   
        System.out.println("\n***Testing printArray***");
        System.out.println("printArray1 should be 1 2 3 1: ");
        printArray(data4);
        System.out.println("printArray2 should be 1 2 4 1: ");
        printArray(data5);
        final int A7 = 7;
        int[] data7 = {A7};
        System.out.println("printArray1 should be 7: ");
        printArray(data7);
  
        System.out.println("\n***Testing eraseFirst42***");
        eraseFirst42(data1);
        System.out.println("eraseFirst42a should be 1 2 0 3 1: ");
        printArray(data1);
        int[] data8 = {1, 2, A3, A42, A42, 1};
        eraseFirst42(data8);
        System.out.println("eraseFirst42b should be 1 2 3 0 42 1: ");
        printArray(data8);
        int[] data9 = {1, 2, A3};
        eraseFirst42(data9);
        System.out.println("eraseFirst42c should be 1 2 3: ");
        printArray(data9);
   
        System.out.print("\n***End of Tests***");

    }
}

Assignment 20.2: More Barnyard Animals (10 pts)
  • Remember the Barnyard Animals from Assignment 19?
  • Let's update that program to use File I/O.
  • Open up Animals.java
  • Then, create a text file to be used in the program..
  • Right-click the Animals project folder and select New->File. Name your file nums.txt.
  • Make sure that you did not accidentally save your file under the src folder. If you did, you can simply drag and drop it into the main project folder.
  • Here are the values to place in your file:
5
3
8
2
4
  • We will read from this file after writing our program.

  • Next, create a second file called animals.txt.
  • Copy the following data into the that file:
sheep
donkeys
goats
geese
cats
  • Follow a similar process to create a blank text file named outfile.txt.
  • Add the following import statement to the top of your program
    import java.io.*;
    
  • Alter the signature of main() as required for File I/O (see lesson notes if you are unsure).
  • Then, declare a new file object for the nums.txt input file, and a new Scanner to read in from this file
  • Next, declare a second file object for the animals.txt input file, and a new Scanner to read from this file
  • Update the portion of the code where you assign values to the animals array so that you read in those values from the file instead. Hint: you can use 5 input.next() statements, or use a for loop. For example:
animals[0] = input.next();
  • Also update the portion of the code where you assign values to the number_animals array so that you read in those values from the file instead. For example:
    number_animals[0] = input.nextInt();
    
  • Then, update your print statement to write to the file instead. Then, copy and paste this line inside of your printArrays method:
    out.println("Animals at our farm");
    
  • Lastly, update your printArrays method so that it writes to a file instead.
  • Hint: declare another file object inside this method for your outfile.txt file and a new PrintWriter to write to this file.
  • When you are finished, outfile.txt should contains the following:
Animals at our farm
5 chickens
3 cows
8 ducks
2 horses
4 pigs
  • Submit your revised Animals.java program to Canvas.