You are on page 1of 10

‭1)‬ ‭PROCEDURE‬

‭1. Create a Scanner object to read user input‬


‭2. Prompt the user to enter an integer‬
‭3. Read the user's input as an integer‬
‭4.Print a message indicating the start of prime numbers list‬
‭5. Iterate through numbers up to the entered integer‬
‭6. Check if the current number is prime using a function‬
‭a: Handle cases where the number is less than or equal to 1‬
‭b: Iterate through possible divisors up to the square root of the number‬
‭c: Check if the number is divisible by any other number‬
‭d: If no divisors found, the number is prime‬
‭7. Print the prime number‬
‭8. Close the Scanner to prevent resource leak‬

‭2)‬ ‭PROCEDURE‬
‭1. Start by prompting the user to enter the number of rows and columns for‬
‭the first matrix.‬
‭2. Read the input values for the number of rows and columns.‬
‭3. Create a 2D array first with dimensions m (rows) by n (columns) to store‬
‭the elements of the first matrix.‬
‭4. Prompt the user to enter the elements of the first matrix and read them‬
‭into the first array.‬
‭5. Prompt the user to enter the number of rows and columns for the second‬
‭matrix.‬
‭6. Read the input values for the number of rows and columns.‬
‭7. Check if the number of columns in the first matrix (n) is equal to the‬
‭number of rows in the second matrix (p). If not, print an error message‬
‭indicating that the matrices cannot be multiplied, and exit.‬
‭8. Create a 2D array second with dimensions p (rows) by q (columns) to store‬
‭the elements of the second matrix.‬
‭9. Prompt the user to enter the elements of the second matrix and read them‬
‭into the second array.‬
‭10. Create a 2D array multiply with dimensions m (rows) by q (columns) to‬
‭store the result of the matrix multiplication.‬
‭11. Perform the matrix multiplication using three nested loops:‬
‭- Outer loop iterates over the rows of the first matrix (m).‬
‭- Middle loop iterates over the columns of the second matrix (q).‬
‭- Inner loop iterates over the common dimension (n or p) and calculates‬
‭the sum of products of corresponding elements from the first and second‬
‭matrices.‬
‭12. Store the calculated sum in the corresponding cell of the multiply array.‬
‭13. Print the resulting matrix multiply, which contains the product of the two‬
‭input matrices.‬

‭3)‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. Ask the user to input text.‬
‭3. Initialize variables for character count (charCount), word count‬
‭(wordCount), and line count (lineCount) to zero.‬
‭4. Begin a loop to read input lines until the user presses Enter twice.‬
‭5. Inside the loop:‬
‭- Read a line of text from the user.‬
‭- If the line is empty, exit the loop.‬
‭- Increment the line count (lineCount) by 1.‬
‭- Add the length of the line to the character count (charCount).‬
‭- Split the line into words using whitespace as delimiter.‬
‭- Increment the word count (wordCount) by the number of words in the‬
‭line.‬
‭6. After exiting the loop, print the total character count, word count, and line‬
‭count.‬
‭7. End the program.‬

‭4)‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. Define the lower limit and upper limit for the random number range.‬
‭3. Create a Random object to generate random numbers.‬
‭4. Generate a random number within the range [lowerLimit, upperLimit].‬
‭5. Print the generated random number.‬
‭6. Check the value of the generated random number:‬
‭- If it's less than or equal to 20, print that the number is in the range [10,‬
‭20].‬
‭- If it's greater than 20 and less than or equal to 30, print that the number is‬
‭in the range (20, 30].‬
‭- If it's greater than 30 and less than or equal to 40, print that the number is‬
‭in the range (30, 40].‬
‭- If it's greater than 40 and less than or equal to 50, print that the number is‬
‭in the range (40, 50].‬
‭7. End the program‬

‭5)a‬ ‭PROCEDURE‬
‭1.Import the Scanner class for user input.‬
‭2. Define the main method to start the program execution.‬
‭3. Use ‘System.out.println()’to prompt the user to enter a string and use‬
‭‘scanner.nextLine()’ to read the input string.‬
‭4. Use the method ‘findStringLength()’ to calculate the length of the input‬
‭string. Convert the string to a character array using ‘toCharArray()’ method‬
‭and return the length of the array.‬
‭5. Print the length of the string to the console using `System.out.println()`.‬
‭6. Close the scanner object to release resources.‬
‭7.Define a method ‘findStringLength(String str)’ to calculate the length of the‬
‭string. Convert the string to a character array and return the length of the‬
‭array.‬
‭8. End the program execution.‬

‭5)b‬ ‭PROCEDURE‬
‭1. Start‬
‭2. Define the main class `StringManipulation`.‬
‭3. Create a `Scanner` object `scanner` to read input from the console.‬
‭4. Prompt the user to enter a string.‬
‭5. Read the input string using `scanner.nextLine()` and store it in‬
‭̀inputString`.‬
‭6. Convert the input string `inputString` into a character array `charArray`‬
‭using the `toCharArray()` method.‬
‭7. Prompt the user to enter a position.‬
‭8. Read the input position using `scanner.nextInt()` and store it in `position`.‬
‭9. Check if the entered position is within the bounds of the character array‬
‭(`position >= 0 && position < charArray.length`)‬
‭10. If the position is within bounds: Retrieve the character at the specified‬
‭position `position` from the character array `charArray` and store it in‬
‭̀character`.‬
‭11. Print the character at the specified position along with its index.‬
‭12. If the position is out of bounds Print a message indicating that the‬
‭position is out of bounds.‬
‭13. Close the scanner using `scanner.close()`.‬
‭14. End.‬

‭5)c‬ ‭PROCEDURE‬
‭1. Start‬
‭2. Define the main class `StringManipulation`.‬
‭3. Initialize two strings `str1` and `str2` with the values "Hello" and "World"‬
‭respectively.‬
‭4. Convert the strings `str1` and `str2` into character arrays `charArray1` and‬
‭̀charArray2` using the `toCharArray()` method.‬
‭5. Calculate the lengths of `charArray1` and `charArray2` and store them in‬
‭variables `len1` and `len2`.‬
‭6. Calculate the total length required for the concatenated string by adding‬
‭̀len1` and `len2`, store it in `totalLen`.‬
‭7. Create a character array `result` of size `totalLen` to hold the concatenated‬
‭string.‬
‭8. Copy characters from `charArray1` to `result` starting from index 0 to‬
‭̀len1`.‬
‭9. Copy characters from `charArray2` to `result` starting from index `len1` to‬
‭̀totalLen`.‬
‭10. Convert the character array `result` into a string `concatenatedString`.‬
‭11. Print the concatenated string `concatenatedString`.‬
‭12. End.‬
‭6)a‬ ‭PROCEDURE‬
‭1. Start.‬
‭2. Define two string variables ` str1` and ` str2` and assign them the desired‬
‭string values.‬
‭3. Concatenate ` str1` , a space `" "` , and ` str2` using the `+` operator and store‬
‭the result in a new string variable ` result`.‬
‭4. Print the concatenated string ` result`.‬
‭5. End.‬

‭6)b‬ ‭PROCEDURE‬
‭1. Start.‬
‭2. Define the main string and the substring to search for.‬
‭3. Use the `indexOf()` method of the `String` class to search for the substring‬
‭within the main string.‬
‭4. Store the index of the substring in a variable.‬
‭5. Check if the index is not equal to -1 (indicating that the substring is found).‬
‭- If true, print a message indicating the substring was found along with its‬
‭index.‬
‭- If false, print a message indicating that the substring was not found in the‬
‭main string.‬
‭6. End.‬

‭6)c‬ ‭PROCEDURE‬
‭1. Start.‬
‭2. Define the main string from which substrings will be extracted.‬
‭3. Use the `substring()` method of the `String` class to extract substrings.‬
‭- To extract a substring from a specific index to the end of the string:‬
‭- Call `substring(startIndex)` method.‬
‭- To extract a substring from a specific start index (inclusive) to an end‬
‭index (exclusive):‬
‭- Call `substring(startIndex, endIndex)` method.‬
‭4. Store the extracted substrings in separate variables.‬
‭5. Print the extracted substrings.‬
‭6. End.‬
‭7)a‬ ‭PROCEDURE‬
‭1. Start‬
‭2. Import the Scanner class to read user input.‬
‭3. Define the main class StringBufferExample.‬
‭4. Inside the main method:‬
‭a. Create a Scanner object to read user input.‬
‭b. Prompt the user to enter a string.‬
‭c. Read the input string using the nextLine() method of Scanner and store it‬
‭in a variable named inputString.‬
‭d. Create a StringBuffer object named stringBuffer with the inputString.‬
‭e. Find the length of the string using the length() method of StringBuffer‬
‭and store it in a variable named length.‬
‭f. Print out the length of the string.‬
‭g. Close the Scanner object.‬
‭5. End‬

‭7)b‬ ‭PROCEDURE‬
‭1. Start‬
‭2. Import the Scanner class to read user input.‬
‭3. Define the main class StringBufferExample.‬
‭4. Inside the main method:‬
‭a. Create a Scanner object to read user input.‬
‭b. Prompt the user to enter a string.‬
‭c. Read the input string using the nextLine() method of Scanner and store it‬
‭in a variable named inputString.‬
‭d. Create a StringBuffer object named stringBuffer with the inputString.‬
‭e. Use the reverse() method of StringBuffer to reverse the string.‬
‭f. Print out the reversed string using the toString() method of StringBuffer.‬
‭g. Close the Scanner object.‬
‭5. End‬

‭7)c‬ ‭PROCEDURE‬
‭1. Start‬
‭2. Import the Scanner class to read user input.‬
‭3. Define the main class StringBufferExample.‬
‭4. Inside the main method:‬
‭a. Create a Scanner object to read user input.‬
‭b. Prompt the user to enter a string and store it in a variable named‬
‭inputString.‬
‭c. Prompt the user to enter the substring to delete and store it in a variable‬
‭named substringToDelete.‬
‭d. Create a StringBuffer object named stringBuffer with the inputString.‬
‭e. Use the indexOf() method of StringBuffer to find the index of‬
‭substringToDelete and store it in a variable named index.‬
‭f. Check if the index is not equal to -1 (indicating that the substring is‬
‭found)‬
‭i. If true, use the delete() method of StringBuffer to delete the substring‬
‭starting from index to index + substringToDelete.length().‬
‭ii. Print out the modified string after deletion.‬
‭iii. If false, print a message indicating that the substring was not found.‬
‭g. Close the Scanner object.‬
‭5. End‬

‭8)‬ ‭PROCEDURE‬
‭1. Create a class `RandomNumberGenerator` that extends `Thread`.‬
‭2. Override the `run()` method in `RandomNumberGenerator` class.‬
‭3. Inside the `run()` method:‬
‭- Create an instance of `Random` class.‬
‭- Enter an infinite loop.‬
‭- Generate a random integer using `nextInt(100)` method.‬
‭- Check if the generated number is even or odd.‬
‭- If even, synchronize on `SquareThread.class` and call‬
‭̀SquareThread.square(num)` method.‬
‭- If odd, synchronize on `CubeThread.class` and call‬
‭̀CubeThread.cube(num)` method.‬
‭- Handle `InterruptedException`.‬
‭4. Create a class `SquareThread` that extends `Thread`.‬
‭5. Create a static method `square(int num)` in `SquareThread` class to‬
‭calculate and print the square of the given number.‬
‭6. Create a class `CubeThread` that extends `Thread`.‬
‭7. Create a static method `cube(int num)` in `CubeThread` class to calculate‬
‭and print the cube of the given number.‬
‭8. Create the `Main` class with the `main()` method.‬
‭9. In the `main()` method, create an instance of `RandomNumberGenerator`‬
‭and start the thread.‬

‭9)‬ ‭PROCEDURE‬
‭1. Define a class named PrintNumbers implementing the Runnable interface:‬
‭- Initialize two instance variables: start and end, which represent the range‬
‭of numbers to print.‬
‭- Create a constructor that sets these variables based on the provided start‬
‭and end values.‬
‭- Implement the run() method:‬
‭- Loop from the start value to the end value.‬
‭- Print the current thread's name along with the current number.‬
‭2. Define a class named Main:‬
‭- In the main method:‬
‭- Create two PrintNumbers instances, one for printing numbers from 1 to‬
‭10 and the other for printing numbers from 90 to 100.‬
‭- Create two Thread objects, passing the PrintNumbers instances as‬
‭arguments to their constructors.‬
‭- Start both threads.‬
‭3. When a thread starts, it executes the run() method of the corresponding‬
‭PrintNumbers instance, printing the numbers within the specified range.‬
‭4. Each thread prints its name along with the numbers it prints, which helps‬
‭differentiate between the outputs of the two threads.‬
‭5. End of the program execution.‬

‭10)a‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. In the main method:‬
‭- Define variables for the numerator and denominator, setting the‬
‭numerator to 10 and the denominator to 0.‬
‭- Attempt to perform the division operation, assigning the result to a‬
‭variable named result.‬
‭- Since division by zero is not allowed, an ArithmeticException is thrown.‬
‭- Catch the ArithmeticException using a try-catch block:‬
‭- If an ArithmeticException occurs, catch it and execute the code inside the‬
‭catch block.‬
‭- Print the message "Arithmetic Exception caught" along with the exception‬
‭message obtained from e.getMessage().‬
‭3. End the program execution.‬

‭10)b‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. In the main method:‬
‭- Define a string variable named str and assign it the value "abc", which is‬
‭not a valid integer representation.‬
‭- Attempt to parse the string as an integer using Integer.parseInt(str) and‬
‭assign the result to an integer variable named num.‬
‭- Since "abc" cannot be parsed as an integer, a NumberFormatException is‬
‭thrown.‬
‭- Catch the NumberFormatException using a try-catch block:‬
‭- If a NumberFormatException occurs, catch it and execute the code inside‬
‭the catch block.‬
‭- Print the message "Number Format Exception caught" along with the‬
‭exception message obtained from e.getMessage().‬
‭3. End the program execution.‬

‭10)c‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. In the main method:‬
‭- Define an integer array named arr with three elements: {1, 2, 3}.‬
‭- Attempt to access an element at index 5 in the array, which is beyond the‬
‭array's length.‬
‭- Since index 5 is out of bounds for the array, an‬
‭ArrayIndexOutOfBoundsException is thrown.‬
‭- Catch the ArrayIndexOutOfBoundsException using a try-catch block:‬
‭- If an ArrayIndexOutOfBoundsException occurs, catch it and execute the‬
‭code inside the catch block.‬
‭- Print the message "Array Index Out of Bound Exception caught" along‬
‭with the exception message obtained from e.getMessage().‬
‭3. End the program execution.‬

‭10)d‬ ‭PROCEDURE‬
‭1. Start the program.‬
‭2. In the main method:‬
‭- Attempt to create an array with a negative size by initializing an array‬
‭variable named negativeArray with a size of -3.‬
‭- Since creating an array with a negative size is not allowed, a‬
‭NegativeArraySizeException is thrown.‬
‭- Catch the NegativeArraySizeException using a try-catch block:‬
‭- If a NegativeArraySizeException occurs, catch it and execute the code‬
‭inside the catch block.‬
‭- Print the message "Negative Array Size Exception caught" along with the‬
‭exception message obtained from e.getMessage().‬
‭3. End the program execution.‬

‭13)‬ ‭PROCEDURE‬
‭1. Initialize the JFrame and JTextField.‬
‭2. Implement the ActionListener interface to handle mouse events.‬
‭3. Override the actionPerformed method to update the JTextField with the‬
‭name of the mouse event when a mouse event is fired.‬
‭4. Create an instance of the JFrame to run the program.‬
‭5. Display the GUI window.‬
‭6. Respond to mouse events by updating the JTextField with the name of the‬
‭mouse event at the center of the window.‬

You might also like