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

Lab2 2024 2025

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

Lab2 2024 2025

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

Lab2: Arrays & Methods

Exercise 1: Arrays Manipulation

Create an array of integers with a predefined set of values. For example:

int[] numbers = {5, 12, 8, 31, 19, 42, 7};

1. Write a Java program that does the following:

a. Prints the original array.

b. Calculates and prints the sum of all the elements in the array.

c. Finds and prints the maximum and minimum values in the array.

d. Searches for a specific value (e.g., 19) in the array and prints whether it's found or not.

e. Reverses the array and prints the reversed array.

f. Sorts the array in ascending order and prints the sorted array.

g. Removes a specific element (e.g., 31) from the array and prints the modified array.

2. Make sure to use loops, conditional statements, and appropriate array manipulation
methods to accomplish these tasks.

3. Test your program with different arrays and values to ensure it works correctly.

Exercise 2: Methods Manipulation

Implement a program that analyzes a given string to calculate the word count, character
count, and determine if it contains a specific character.

Here are the main steps:


1. Create a Java program with a main method.

2. In the main method, define a sample string. For example:

String input = "Hello, this is a sample string. Let's analyze it.";

3. Use the String class to perform the following operations:

 Calculate and print the total word count in the string.


 Calculate and print the total character count in the string (including spaces and
punctuation).
 Check if the string contains a specific character (e.g., 'a' or 'z') using the contains
method and print the result.

4. Use the Character class to check if a specific character (e.g., 'H' or 'A') is uppercase or
lowercase, and print the result.

5. Use the Math class to calculate and print the square root of a number (e.g., 16) and the
absolute value of a number (e.g., -25).

6. Compile and run your program to see the results.

You might also like