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


Assignment 21.1: 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***");
       


    }

}

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