Assignment 21
Due Thursday, June 21 at 1:20pm on Canvas


Assignment 21.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 21.2: Customer Data (10 pts)
  • Write a program that reads in a series of names, along with their gender, from a file.
  • The file is called names.txt.
  • The program uses a loop to read in each name from the file and store each person's title (Mr. or Ms. or Mx.) + name in an array called customers.
  • Note: you can assume that the size of the array is know ahead of time to be 15.
  • The program should then call two methods.
  • The first method:
    • The method is named capitalizeArray
    • It take in a String array
    • It alters the values in the String array to capitalize all of the letters
    • This method must use a for loop. It cannot call any outside methods that we have not discussed in this class.
    • It returns nothing.
  • The second method:
    • The method is named print
    • It takes in an array of Strings
    • It opens up a new PrintWriter and connects it to a file called customers.txt
    • It prints out the array to the file using a for loop
    • It closes the PrintWriter
    • It returns nothing
  • Once you have filled the customers array with titles and names, call the capitalizeArray method to capitalize all the names in the array.
  • Next, call the print method to display each of the capitalized names in the customers.txt file
  • Copy and paste the starter code below into a new file called Customers.java
/**
 * @author
 *
 */
import java.util.Scanner;
public class Customers
{
    /**
     * Write the javadoc comment for capitalizeArray here
     */
    public static void capitalizeArray(String[] customers)
    {
        return;
    }
    
    /**
     * Write the javadoc comment for printArray here
     */
    public static void printArray(String[] customers)
    {
        return;
    }
    
    public static void main(String[] args)
    {
        //declare your customers array here
        //note, you can assume your array will be of length 8
        String name, gender;
        
        
        //Use a while loop to read in the data from the file
        
        
        //call your two methods
    }
}

  • Copy and paste the below data into a file called names.txt:
Barbara Stanton
F
Timoteo Martinez
M
Ally Gu
O
Xing Xiao
M
Dung Kim
F
Tanya White
F
Alan Ngo
M
Abir Fadel
M
Nataly Luna
F
Charles Atkinson
M
Stacey Cahill
O
Stephanie Teatro
F
Ping Li
M
June Park
F
Binh Nguyen
M
  • Below is the output that your program should give in a file called customers.txt:
MS. BARBARA STANTON
MR. TIMOTEO MARTINEZ
MX. ALLY GU
MR. XING XIAO
MS. DUNG KIM
MS. TANYA WHITE
MR. ALAN NGO
MR. ABIR FADEL
MS. NATALY LUNA
MR. CHARLES ATKINSON
MX. STACEY CAHILL
MS. STEPHANIE TEATRO
MR. PING LI
MS. JUNE PARK
MR. BINH NGUYEN