Welcome to Our Last Class! Announcements
Lesson 21 Practice Exam QuestionsAnswer 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; 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; 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)
Array, File I/O and Method Practice - No Pair Programming Allowed!
Activity 22.2: Final Exam Practice (10 pts) - due Friday, March 20 at 11:59pm
Activity 22.3: Methods + Arrays Worksheet 2 (10 pts) - due Friday, March 20 at 11:59pm
* 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
4 3.2 6.7 2.9 4.6
8 2.1 -5.6 9.0 8.7 -2.2 8.2 9.5 1.4
Enter the name of the file: input1.txt <--could have any name!
Enter the name of the file: input2.txt <-- could have any name!
Wrap Up
Upcoming Assignments
~Good Luck Studying for the Final!~ |