In: Computer Science
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:
o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.
2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.
o Write a method called larger that accepts two double parameters and returns true if the first parameter is greater and false otherwise. Use the Boolean data type.
3. Write a Java program called Characters that calls the following methods and displays the returned value: o Write a method called countA that accepts a String parameter and returns the number of times the character ‘A’ is found in the string.
o Write a method called multiConcat that takes a String and integer as parameters. Return a String that consists of the string parameter concatenated with itself count times, where the count is the integer parameter. For example, if the parameter values are “hi” and 3, the return value is “hihihi”.
• Display the returned values in main.
• There is no user input.
• Set up variables and change the values to test the methods.
• Submit only one .java file
// Numbers.java (all in one java file)
public class Numbers {
// method to return the cube of a number
public int cubeIt(int num) {
return num * num * num;
}
// method to generate and return a random number between min and max
public int randomInRange(int min, int max) {
// generating a value between min and max inclusive
int num = (int) (Math.random() * (max - min + 1)) + min;
return num;
}
// returns true if n1 > n2
public Boolean larger(double n1, double n2) {
return n1 > n2;
}
public static void main(String[] args) {
// creating an object of Numbers and testing all methods
Numbers numbers = new Numbers();
System.out.println("cube of 5: " + numbers.cubeIt(5));
System.out.println("cube of 0: " + numbers.cubeIt(0));
System.out.println("random in range 10 and 100: "
+ numbers.randomInRange(10, 100));
System.out.println("random in range -5 and 5: "
+ numbers.randomInRange(-5, 5));
System.out.println("5 larger than 6: " + numbers.larger(5, 6));
System.out.println("15 larger than 6: " + numbers.larger(15, 6));
// creating an object of Characters and testing all methods
Characters characters = new Characters();
System.out.println("\nCount of 'A' in ABRACADABRA: "
+ characters.countA("ABRACADABRA"));
System.out.println("multiConcat (hi, 3): "
+ characters.multiConcat("hi", 3));
}
}
class Characters {
// returns the count of 'A' in str (case sensitive)
public int countA(String str) {
int count = 0;
// looping and counting 'A'
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'A') {
count++;
}
}
return count;
}
// returns str multiplied n times
public String multiConcat(String str, int n) {
String result = "";
//combining str n times
for (int i = 0; i < n; i++) {
result += str;
}
return result;
}
}
/*OUTPUT*/
cube of 5: 125
cube of 0: 0
random in range 10 and 100: 42
random in range -5 and 5: 5
5 larger than 6: false
15 larger than 6: true
Count of 'A' in ABRACADABRA: 5
multiConcat hi, 3: hihihi