0% found this document useful (0 votes)
14 views2 pages

Java Programming Exercises Worksheet

This document contains 12 problems involving Java programming concepts such as: 1) Reading input and determining the number of negative, positive, and zero values 2) Finding the maximum difference between two elements in an array 3) Checking if a 5-digit integer is a palindrome 4) Using a for loop to compute a summation 5) Checking if an array contains only one digit 6) Finding the second most frequent character in a string 7) Adding two matrices 8) Evaluating the output of a code snippet involving operators 9) Displaying an array in descending order 10) Reading a string and outputting it in uppercase and lowercase 11) Checking if a string ends with the contents of another

Uploaded by

Abdulwahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Java Programming Exercises Worksheet

This document contains 12 problems involving Java programming concepts such as: 1) Reading input and determining the number of negative, positive, and zero values 2) Finding the maximum difference between two elements in an array 3) Checking if a 5-digit integer is a palindrome 4) Using a for loop to compute a summation 5) Checking if an array contains only one digit 6) Finding the second most frequent character in a string 7) Adding two matrices 8) Evaluating the output of a code snippet involving operators 9) Displaying an array in descending order 10) Reading a string and outputting it in uppercase and lowercase 11) Checking if a string ends with the contents of another

Uploaded by

Abdulwahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet -1

1. (Negative, Positive and Zero Values) Write a java program that inputs five numbers and
determines and prints the number of negative numbers input, the number of positive numbers
input and the number of zeros input.
2. Write a Java program to find maximum difference between two elements in a given array of
integers such that smaller element appears before larger element.

Example:
Input :
nums = { 2, 3, 1, 7, 9, 5, 11, 3, 5 }
Output:
The maximum difference between two elements of the said array elements
10

3. (Palindromes) A palindrome is a sequence of characters that reads the same backward as forward.
For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and
11611. Write an application that reads in a five-digit integer and determines whether it’s a
palindrome. If the number is not five digits long, display an error message and allow the user to
enter a new value.

4. Write a Java program using for loop that can compute the following summation:

5. An array is called vanilla if all its elements are made up of the same digit. For example {1, 1, 11,
1111, 1111111} is a vanilla array because all its elements use only the digit 1. However, the array
{11, 101, 1111, 11111} is not a vanilla array because its elements use the digits 0 and 1. Write a java
program that display true if the array a vanilla array. Otherwise false.

6. Write a Java program to find the second most frequent character in a given

string Sample output


The given string is: successes
The second most frequent char in the string is: c

7. Write a java program that to Add Two Matrices.


8. What will be the output of the following code?
int k = 5, j = 9;
k += k++ – ++j + k;
boolean result= k + j * j > j * ( K + J)-1;
[Link](“k=” +k);
[Link](“j=” +j);
[Link](result);

9. Suppose that scores is an array of 10 components of type double, scores = {2.5, 3.9, 4.8, 6.2, 6.2,
7.4, 7.9, 8.5, 8.5, 9.9} and Display the array element in decreasing order.

10. Write a Java program that reads a string from the keyboard, and outputs the string twice in a
row, first all uppercase and next all lowercase. If, for instance, the string “Hello" is given, the
output will be “HELLOhello".
11. Write a Java program to check whether a given string ends with the contents of another string.

Sample Output:

"java Exercises" ends with "se"? false


"java Exercise" ends with "se"? true

12. Convert the following switch statement into if-else if statements:

String dayString1, dayString2, dayString3;


int day = [Link]();
switch (day) {
case 1: dayString1 = "Saturday";
case 2: dayString2 = "Sunday";
break;
case 3: dayString3 = "Monday";
break;
case 4: dayString1 = "Tuesday";
case 5: dayString2 = "Wednesday";
break;
default: dayString3 = "Invalid day";
break;
}

Common questions

Powered by AI

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.

You might also like