0% found this document useful (0 votes)
2 views4 pages

4,5,6

The document contains two Java programs: one for generating random numbers within a user-defined range and categorizing them into lower, middle, and higher ranges, and another for string manipulation using character arrays, which includes operations like calculating string length, finding a character at a specific position, and concatenating two strings. The first program utilizes the Random class for number generation and prints messages based on the generated number's range. The second program takes user input for two strings, performs various string operations, and displays the results.

Uploaded by

abiramis38
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)
2 views4 pages

4,5,6

The document contains two Java programs: one for generating random numbers within a user-defined range and categorizing them into lower, middle, and higher ranges, and another for string manipulation using character arrays, which includes operations like calculating string length, finding a character at a specific position, and concatenating two strings. The first program utilizes the Random class for number generation and prints messages based on the generated number's range. The second program takes user input for two strings, performs various string operations, and displays the results.

Uploaded by

abiramis38
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
You are on page 1/ 4

1.

Generate random numbers between two given limits using Random class and print
messages according to the range of the value generated.
import java.util.Random;

import java.util.Scanner;

public class RandomNumberRange {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Random random = new Random();

// Get limits from the user

System.out.print("Enter the lower limit: ");

int lowerLimit = scanner.nextInt();

System.out.print("Enter the upper limit: ");

int upperLimit = scanner.nextInt();

// Generate a random number in the given range

int randomNumber = random.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;

// Print the random number

System.out.println("Generated Random Number: " + randomNumber);

// Print messages based on the range

if (randomNumber < lowerLimit + (upperLimit - lowerLimit) / 3) {

System.out.println("The number is in the lower range.");

} else if (randomNumber < lowerLimit + 2 * (upperLimit - lowerLimit) / 3) {


System.out.println("The number is in the middle range.");

} else {

System.out.println("The number is in the higher range.");

scanner.close();

1. User Input: The program takes two integer inputs from the user (lower and upper limits).
2. Random Number Generation: The Random class generates a number within the specified
range.
3 Range Classification: The generated number is categorized into three ranges:

 Lower Range: The first third of the range.


 Middle Range: The second third of the range.
 Higher Range: The last third of the range.

4. Output Message: The program prints a message based on where the number falls.

5.Write a program to do String Manipulation using Character Array and perform the
following string operations:
a. String length
b. Finding a character at a particular position
c. Concatenating two strings
import java.util.Scanner;
public class StringManipulation
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

// Taking input for the first string


System.out.print("Enter the first string: ");
String input1 = scanner.nextLine();
char[] charArray1 = input1.toCharArray(); // Convert to character array

// Taking input for the second string


System.out.print("Enter the second string: ");
String input2 = scanner.nextLine();
char[] charArray2 = input2.toCharArray(); // Convert to character array

// a. String Length
System.out.println("Length of first string: " + charArray1.length);
System.out.println("Length of second string: " + charArray2.length);

// b. Finding a character at a particular position


System.out.print("\nEnter the position to find the character (1-based index) in first
string: ");
int pos = scanner.nextInt();
if (pos > 0 && pos <= charArray1.length) {
System.out.println("Character at position " + pos + ": " + charArray1[pos - 1]);
} else {
System.out.println("Invalid position!");
}

// c. Concatenating two strings using Character Array


char[] concatenatedArray = new char[charArray1.length + charArray2.length];

// Copy first string to new array


System.arraycopy(charArray1, 0, concatenatedArray, 0, charArray1.length);
// Copy second string to new array
System.arraycopy(charArray2, 0, concatenatedArray, charArray1.length,
charArray2.length);
// Convert character array back to string for display
String concatenatedString = new String(concatenatedArray);
System.out.println("\nConcatenated String: " + concatenatedString);

scanner.close();
}
}

You might also like