Java Programming Exercises Worksheet
Java Programming Exercises Worksheet
Read the integer from the user and convert it to a string. Check if its length is five; if not, display an error and prompt for reentry. For a valid five-digit integer, compare the string with its reverse. If they are the same, the integer is a palindrome; otherwise, it is not.
To add two matrices in Java, ensure both matrices have the same dimensions. Initialize a resultant matrix of the same size. Iterate over each element, adding corresponding elements from each input matrix, and store the result in the corresponding position in the resultant matrix.
Read the string from the keyboard. Use the String class's toUpperCase() and toLowerCase() methods to transform it, then concatenate and print the uppercase version followed by the lowercase version.
Initially, k is 5 and j is 9. In the expression 'k += k++ – ++j + k', k++ uses k's original value (5) then increments k to 6. ++j increments j to 10 before it is used. Thus, the expression evaluates as k += 5 - 10 + 6 = k += 1. Thus, k becomes 7. After execution, j is 10. In evaluating the boolean result, we have 7 + 10 * 10 > 10 * (7 + 10) - 1, or 107 > 169 - 1, implying result is false.
A key problem is handling characters with tied frequencies, which requires developing a strategy to determine what constitutes 'second most frequent.' Also, efficiently tracking character counts and updating the second most frequent character without excessive re-computation or memory usage presents a challenge.
To find the maximum difference where a smaller element appears before a larger element, iterate through the array while maintaining the minimum value encountered so far. For each element, calculate the difference between it and the current minimum, updating the maximum difference when a larger difference is found. The key is keeping track of the lowest element value encountered as you progress through the array.
To sort a double array in descending order, use Arrays.sort with a custom comparator or convert the numbers to their negatives and sort in ascending order. Alternatively, sort in ascending order and reverse the array.
Switch statements are concise for discrete values. Conversion to if-else requires clear structuring to maintain logical flow and prevent fall-through. Each case converts to its corresponding if-else branch, ensuring conditions and actions align logically. This requires careful attention to detail, especially managing exclusive versus shared actions across cases.
To determine if an array is vanilla, convert the first element to a string to extract the unique digits. Loop through each subsequent array element, converting each to a string and checking that all digits match the initial set of digits. If a mismatch occurs, the array is not vanilla.
To design this Java program, you would prompt the user to input five integers. You would initialize three counters, one for negative numbers, one for positive numbers, and one for zeros. For each input number, you would use if-else statements to check whether the number is negative, positive, or zero and increment the corresponding counter. After processing all five numbers, you would print the counts of negative, positive, and zero numbers.