Watch the Lesson Video HERE


Welcome to Our Last Class!

Announcements

  • Final Exam: this Tuesday, March 24 at 11:30am on Canvas
    • No makeups - the exam must be taken on the that day and at that time
    • Exam will have a similar format to your quizzes - short answer, multiple choice, true/false
      • Will select methods from Activity 21.3 and Activity 22.3 (assigned for today) and put them on the final.
    • It will appear on Canvas under the module for Week 12
    • I will proctor the exam via Zoom and Canvas
    • You must be online to take the exam at 11:30am sharp, or you might not be allowed to take the exam
      • Exam will be short - expect somewhere in the neighborhood of 20 -25 minutes long
    • You will have only one attempt to complete the exam
    • Exam is closed notes, closed technology.
    • Canvas will let me know if you look at any other page besides the exam - in which case you will receive a 0
    • You cannot work with anyone else during the exam - if I see you talking to someone else, then you will receive a 0
  • Lab 10 due Friday (tomorrow) at midnight
  • No quiz for Friday
  • Last activities for the quarter due Friday (tomorrow) at midnight
  • Last office hour of the quarter is today, Thursday, March 19 from 5:00-5:25pm
    • No late work accepted after this office hour
    • No makeup work accepted that is more than one week late
    • I will still answer emails and messages on Canvas, even though I will not hold office hours
    • However, I will not be accepting any late work


Lesson 21 Practice Exam Questions

Answer the below questions with a partner:

1. Given the following lines of code, fill in the missing parts of the loop to read in the file word-by-word:

String word;

int count = 0;

      File file = new File("in.txt");

Scanner input = new Scanner(file);

while(                                    ) {    

    word =

    count++;

    System.out.println("Word " + count + ": " + word);

}

2. Given the following lines of code, fill in the missing parts of the loop to read in the file line-by-line:


String line;

int count = 0;

      File file = new File("in.txt");

Scanner input = new Scanner(file);

while(                                ) {    

    line =

    count++;

    System.out.println("Line " + count + ": " + line);

}


Activity 22.1: Join the Zoom Practice Meeting from 11:30am-12:00pm (10 pts)

  • Once I check off your name, you are welcome to log out of the meeting


Array, File I/O and Method Practice - No Pair Programming Allowed!

  • For the rest of class, work on the below 3 activities.

Activity 22.2: Final Exam Practice (10 pts) - due Friday, March 20 at 11:59pm

  • Please copy and paste the below document into a file of type .txt, .odt, .doc, .docx and answer the questions
  • When you have finished answering all questions, upload your document to Canvas
  • Note that the answer key will be emailed to you on Saturday to help you double-check your answers

Activity 22.3: Methods + Arrays Worksheet 2 (10 pts) - due Friday, March 20 at 11:59pm

  • Copy the following into a new file called MoreArrayMethods.java.
  • Write each method provided below.
  • As you write each method, compile and run your code.
  • Once you have the correct output, move onto the next method.
  • When your methods pass all of the tests, upload your file to Canvas.
/**
 * Activity 22.3
 * @author
 * CIS 36A
 */
public class MoreArrayMethods {
    
    /**
     * Return the sum of the first two elements in
     * the array
     * If the array is smaller than length 2, you can
     * return the lone element stored in the array
     * If the array is empty, you should return 0
     * @param nums the array of numbers
     * @return the sum of the first two elements in the array
     * Test cases:
     * sumUp2({1, 2, 3, 4, 5} -> 3
     * sumUp2({10}) -> 10
     * sumUp2({}) -> 0
     */
    public static int sumUp2(int[] array) {
        return 0;
    }

    /**
    * Return the index of the first appearance of the
    * number 42 in any array. If not found return -1.
    * @param data array of ints
    * @return the index of the first number 42, -1 if not found.
    * Test cases:
    * locateFirst42({1, 1, 42, 3, 1}) --> 2
    * locateFirst42({1, 1, 2, 42, 1}) --> 3
    * locateFirst42({1, 1, 2, 1, 2, 3}) --> -1
    */
    public static int locateFirst42(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:
     * contains123({1, 2, 3, 1}) --> true
     * contains123({1, 2, 4, 1}) --> false
     * contains123({1, 2, 1, 2, 3}) --> true
     */
    public static boolean contains123(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:
     * deleteFirst42({1, 2, 42, 3, 1}) --> {1, 2, 0, 3, 1}
     * deleteFirst42({1, 2, 3, 42, 42, 1}) --> {1, 2, 3, 0, 42, 1}
     * deleteFirst42({1, 2, 3}) --> {1, 2, 3}
     */
    public static void deleteFirst42(int[] data) {
        return;
    }

    /**
    * Alters the given array such that the first and last elements are swapped
    * @param an array of ints
    * Note you can assume you will be given an array of length >= 1
    * Test cases:
    * reverseFirstLast([1, 2, 3, 4]) → [4, 2, 3, 1]
    * reverseFirstLast([1, 2, 3]) → [3, 2, 1]
    * reverseFirstLast([8, 6, 7, 9, 5]) → [5, 6, 7, 9, 8]
    */
    public static void reverseFirstLast(int[] data) {
        return;
    }

    public static void main(String[] args) {
        boolean answer = false;
        int num = 0;
        
        final int A3 = 3, A42 = 42;
        int[] data1 = { 1, 2, A42, A3, 1 };

        System.out.println("***Testing sumUp2***");
        num = sumUp2(data1);
        System.out.println("sumUp2 should be 3: " + num);
        int[] data = {10};
        num = sumUp2(data);
        System.out.println("sumUp2 should be 10: " + num);
        int[] empty = {};
        num = sumUp2(empty);
        System.out.println("sumUp2 should be 0: " + num);
        
        System.out.println("***Testing locateFirst42***");
        num = locateFirst42(data1);
        System.out.println("locateFirst42a should be 2: " + num);
        int[] data2 = { 1, 1, 2, A42, 1 };
        num = locateFirst42(data2);
        System.out.println("locateFirst42a should be 3: " + num);
        int[] data3 = { 1, 1, 2, 1, 2 };
        num = locateFirst42(data3);
        System.out.println("locateFirst42a should be -1: " + num);

        System.out.println("\n***Testing contains123***");
        final int A4 = 4;
        int[] data4 = { 1, 2, A3, 1 };
        answer = contains123(data4);
        System.out.println("contains123 should be true: " + answer);
        int[] data5 = { 1, 2, A4, 1 };
        answer = contains123(data5);
        System.out.println("contains123 should be false: " + answer);
        int[] data6 = { 1, 2, 1, 2, A3 };
        answer = contains123(data6);
        System.out.println("contains123 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 deleteFirst42***");
        deleteFirst42(data1);
        System.out.println("deleteFirst42 should be 1 2 0 3 1: ");
        printArray(data1);
        int[] data8 = { 1, 2, A3, A42, A42, 1 };
        deleteFirst42(data8);
        System.out.println("deleteFirst42 should be 1 2 3 0 42 1: ");
        printArray(data8);
        int[] data9 = { 1, 2, A3 };
        deleteFirst42(data9);
        System.out.println("deleteFirst42 should be 1 2 3: ");
        printArray(data9);

        System.out.println("\n***Testing reverseFirstLast***");
        int[] data10 = { 1, 2, 3, 4 };
        reverseFirstLast(data10);
        System.out.println("reverseFirstLast should be 4 2 3 1: ");
        printArray(data10);
        reverseFirstLast(data9);
        System.out.println("reverseFirstLast should be 3 2 1: ");
        printArray(data9);
        int[] data11 = {8, 6, 7, 9, 5};
        reverseFirstLast(data11);
        System.out.println("reverseFirstLast should be 5 6 7 9 8: ");
        printArray(data11);
       
        System.out.print("\n***End of Tests***");
    }
}


Activity 22.4: Array and File I/O Practice (10 pts) - due Friday, March 20 at 11:59pm

  • Open a new Java file named NumberNumber.java
  • Inside the same project folder as your Java file, create two new text files - names of your choice
  • Copy and paste the below values into the first text file:
4
3.2
6.7
2.9
4.6
  • Next, copy and paste the below values into the second text file:
8
2.1
-5.6
9.0
8.7
-2.2
8.2
9.5
1.4
  • The above input files contain information about arrays.
  • The first line of the file states the length of the array
  • The subsequent lines of the file specify the contents of the array.
  • Write a program that reads in the data from the file, uses the information to create an array of doubles, and then computes the sum and product of the values stored in the array.
    • Note that your program should work with any input file that is structured in the same format at the above two input files.
    • Therefore, you cannot assume that I will run your code with just the two examples.
    • What you can assume is that all input files will have a first line containing the length of the array, followed by a sequence of values to insert into the array.
  • First, you will need to prompt the user to enter the name of a file.
    • Save this file name as a String, and use it to create a new File object.
  • Next, read in the first line of the file to determine how big to make your array.
    • declare an array of the specified length - hint: you must use an integer variable to store the length
  • Note that you will need to use for loops to complete the rest of this assignment.
    • Use the length of the array in the test condition of the for loop
    • Inside the body of the for loop, read in each subsequent number and store it into the array. See class notes and Activity 19.1 for some examples.
    • Using another for loop, sum the numbers and report their sum to the user.
    • Also, multiply the numbers together and report their product to the user.
  • For full credit, I should see two for loops in your code (see above).
  • Also note: 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 name of the file: input1.txt  <--could have any name!
Array length = 4

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 name of the file: input2.txt <-- could have any name!
Array length = 8

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 Numerical.java to Canvas when you are finished.

Wrap Up
  • With a partner, answer the questions from today's learning objectives


Upcoming Assignments

  • Activities 22.2-22.4 due Friday at 11:59pm
  • Lab 10 due Friday
  • Final Exam on Tuesday, March 24!


~Good Luck Studying for the Final!~