/** * ============================================================ * CS 231 — Data Structures & Algorithms * Lab 1 ver.A: Java Foundations * * IMPORTANT: * - This file is tested using a SEPARATE test harness. * - Do NOT print inside these methods. * - Do NOT read input. * - Do NOT change method names or signatures. * * Each method should be implemented exactly as specified. * * ============================================================ */ public class LabA_v1 { /* ======================================================== * Task 1: Even or Odd * ======================================================== */ public static boolean isEven(int value) { // TODO: Return true if value is even, false otherwise return false; // placeholder } /* ======================================================== * Task 2: Positive, Negative, or Zero * ======================================================== */ public static int signOfNumber(int value) { /* * Return: * 1 if value is positive * 0 if value is zero * -1 if value is negative */ return 0; // placeholder } /* ======================================================== * Task 3: Find Largest Element in an Array * ======================================================== * * Assume: * values.length >= 1 */ public static int maxElement(int[] values) { // TODO: Track and return the largest value in the array return 0; // placeholder } /* ======================================================== * Task 4: Array Average * ======================================================== * * THINK: * What happens if you divide integers? */ public static double arrayAverage(int[] values) { // TODO: Compute and return the average return 0.0; // placeholder } /* ======================================================== * Task 5: Swap Two Values (Returning the Result) * ======================================================== * * Java does not support returning multiple values directly. * * HINT: * As you may have discovered, Java does not support * a tuple type. How can you return two values using * a type you already know? * * EXAMPLE: * swap(3, 7) → {7, 3} */ public static int[] swap(int a, int b) { // TODO: Return the values in swapped order return null; // placeholder } /* ======================================================== * Task 6: Merge Two Arrays * ======================================================== * * DIRECTIONS: * - Given two integer arrays a and b, * - Return a NEW array containing all elements of a * followed by all elements of b. * * ASSUME: * - a and b are not null * * EXAMPLE: * a = {1, 2, 3} * b = {4, 5} * mergeArrays(a, b) → {1, 2, 3, 4, 5} * * RULES: * - Do NOT modify the original arrays */ public static int[] mergeArrays(int[] a, int[] b) { // TODO: Create a new array of length a.length + b.length // TODO: Copy elements of a into the new array // TODO: Copy elements of b into the new array return null; // placeholder } /* ======================================================== * Task 7: Check Character Case * ======================================================== * * Determine whether a single character is uppercase * or lowercase using ASCII character values. * * HINT: * Uppercase letters range from 'A' to 'Z' * Lowercase letters range from 'a' to 'z' * * You may compare characters directly: * if (ch >= 'a' && ch <= 'z') { * // lowercase * } * * if (ch >= 'A' && ch <= 'Z') { * // uppercase * } * * @param ch the character to classify * @return 1 if uppercase * 0 if lowercase * -1 otherwise (not a letter) */ public static int checkCharCase(char ch) { // TODO: Check for lowercase letters // TODO: Check for uppercase letters // TODO: Return the appropriate value return -1; // placeholder } /* ======================================================== * Task 8: Adjacent Twos * ======================================================== * * Determine whether an array contains the value 2 * appearing next to another 2. * * EXAMPLES: * has22({1, 2, 2}) → true * has22({1, 2, 1, 2}) → false * has22({2, 2, 2}) → true * * HINT: * Look at each element and the one immediately after it. * * ASSUME: * - The array has length >= 1 */ public static boolean has22(int[] values) { // TODO: Loop through the array // TODO: Check whether values[i] and values[i + 1] are both 2 // TODO: Return true if such a pair is found return false; // placeholder } /* ======================================================== * Task 9: Count Occurrences * ======================================================== * * Count how many times a target value appears in an array. * * EXAMPLES: * countOccurrences({1, 2, 2, 3}, 2) → 2 * countOccurrences({5, 5, 5}, 5) → 3 * countOccurrences({1, 2, 3}, 4) → 0 * * ASSUME: * - The array has length >= 1 * * @param values the array to search * @param target the value to count * @return the number of times target appears in the array */ public static int countOccurrences(int[] values, int target) { // TODO: Initialize a counter // TODO: Loop through the array // TODO: Increment the counter when values[i] equals target return 0; // placeholder } /* ======================================================== * Task 10: Is Array Non-Decreasing * ======================================================== * * Determine whether the values in an array are in * non-decreasing order. * * Non-decreasing means each value is greater than or equal * to the one before it. * * EXAMPLES: * isNonDecreasing({1, 2, 2, 3}) → true * isNonDecreasing({1, 3, 2}) → false * isNonDecreasing({5}) → true * * ASSUME: * - The array has length >= 1 * * @param values the array to examine * @return true if the array is non-decreasing, false otherwise */ public static boolean isNonDecreasing(int[] values) { // TODO: Loop through adjacent elements // TODO: If a later element is smaller, return false // TODO: If no violations are found, return true return true; // placeholder } }