Class To Generate Random Output To Screen After Manipulation Of Input
Integer Manipulation
Here’s an example Java class that demonstrates a method to take a random integer, multiply it by 3, and return the result. The main method calls this method, prints the result, and includes all necessary syntax and documentation of the prepared code.
import java.util.Random;
public class IntegerMultiplier {
public static void main(String[] args) {
int randomNumber = getRandomInteger();
int result = multiplyByThree(randomNumber);
System.out.println("Random Integer: " + randomNumber);
System.out.println("Result after multiplying by 3: " + result);
}
// Method to generate a random integer between 1 and 10
private static int getRandomInteger() {
Random random = new Random();
return random.nextInt(10) + 1; // Generates a number between 1 and 10
}
// Method to multiply an integer by 3
private static int multiplyByThree(int number) {
return number * 3;
}
}
Explanation:
getRandomInteger: Generates a random integer between1and10by usingrandom.nextInt(10) + 1. Adding1ensures the random number is within1to10(inclusive).multiplyByThree: Takes an integer as input and returns the result of multiplying it by3.mainMethod: CallsgetRandomInteger, then callsmultiplyByThreewith the random integer, and prints both the original and multiplied results.
Sample Output:
Random Integer: 4
Result after multiplying by 3: 12
This code provides a clear example of generating a random integer, performing a multiplication, and displaying the results.
String Manipulation
Here’s an example Java class that demonstrates a method that takes a string, repeats it three times, and then returns the result. The main method will call this method, print the result to the screen, and include all the proper syntax and documentation of the prepared code.
import java.util.Random;
public class StringMultiplier {
public static void main(String[] args) {
String randomString = getRandomString();
String result = multiplyStringByThree(randomString);
System.out.println("Original String: " + randomString);
System.out.println("Multiplied String: " + result);
}
// Method to generate a random string
private static String getRandomString() {
String[] possibleStrings = {"apple", "banana", "cherry", "date", "fig"};
Random random = new Random();
return possibleStrings[random.nextInt(possibleStrings.length)];
}
// Method to multiply a string by 3
private static String multiplyStringByThree(String input) {
return input.repeat(3);
}
}
Explanation:
getRandomStringgenerates a random string from a predefined list of possible strings.multiplyStringByThreetakes an input string and uses theString.repeat(int count)method to concatenate it three times.- The
mainmethod gets a random string, multiplies it, and prints both the original and multiplied strings.
Sample Output:
Original String: banana
Multiplied String: bananabananabanana
This code provides a clear example of generating a random string, repeating the string 3 times, and displaying the results.
Java Library Method Used
The random.nextInt(int bound) method in Java is used to generate a random integer between 0 (inclusive) and the specified bound (exclusive).
Here’s how it works:
- Syntax:
random.nextInt(bound) - Parameter: The
boundspecifies the upper limit for the generated random number (exclusive). - Return Value: It returns a random integer between
0(inclusive) andbound(exclusive).
For example:
Random random = new Random();
int randomNumber = random.nextInt(5);
System.out.println(randomNumber);
In this example:
random.nextInt(5)will generate a random integer between0and4(i.e., one of0,1,2,3, or4).
This method is particularly useful when you need to pick a random index from an array or list, as in the code we used earlier.
Leave a Reply