Please write this in JAVA ONLY and DO NOT USE THE MATH Class methods for this as
ID: 3923388 • Letter: P
Question
Please write this in JAVA ONLY and DO NOT USE THE MATH Class methods for this assignment! There are 4 simple parts to this question, thank you for your help!
1. Write a program that outputs the cube of every number between 1 and 9, inclusive.
2. Add a method to the program that calculates the cube of every number between a lower bound and an upper bound that a user inputs (for instance, lower bound 1 and upper bound 9, for between 1 and 9).
3. Write an additional method that return the cubed value of an integer
4. Write an additional method to accomplish the same thing in #2, but this time calls the method you wrote in #3.
Explanation / Answer
CubeTest.java
import java.util.Scanner;
public class CubeTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the lower bound: ");
int lower = scan.nextInt();
System.out.print("Enter the upper bound: ");
int upper = scan.nextInt();
calculateCube(lower, upper);
}
public static void calculateCube(int lower, int upper){
for(int i=lower; i<=upper; i++){
int cubeValue = getCube(i);
System.out.println(i+ " cube is "+cubeValue);
}
}
public static int getCube(int i){
return i * i *i;
}
}
Output:
Enter the lower bound: 1
Enter the upper bound: 9
1 cube is 1
2 cube is 8
3 cube is 27
4 cube is 64
5 cube is 125
6 cube is 216
7 cube is 343
8 cube is 512
9 cube is 729