1. Write a program that outputs the cube of every number between 1 and 9, inclus
ID: 2247836 • Letter: 1
Question
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.
I posted this question yesterday but I need this written in Java. Also, do not use MATH class methods.
Explanation / Answer
//Please see the below code please do thumbs up if you like the solution
import java.util.*;
import java.util.Scanner;
public class Calculator{
// Below method calculates the cube of every number between
// a lower bound and an upper bound that a user inputs
public static void cube(int lowerBound, int upperBound)
{
for(int i=lowerBound+1;i<upperBound;i++)
{
System.out.println("Cube of "+i+" is "+i*i*i);
}
}
//Method that return the cubed value of an integer
public static int getCubeValue(int val)
{
return val*val*val;
}
//additional method to accomplish the same thing in #2
public static void sameAscubeFun(int lowerBound, int upperBound)
{
for(int i=lowerBound+1;i<upperBound;i++)
{
System.out.println("Cube of "+i+" is "+getCubeValue(i));
}
}
public static void main(String[] args) {
//The cube of every number between 1 and 9
for(int i=1;i<=9;i++)
{
System.out.println("Cube of "+i+" is "+i*i*i);
}
Scanner sc=new Scanner(System.in);
System.out.println("Enter Lower Bound ");
int lb=sc.nextInt();
System.out.println("Enter Upper Bound ");
int ub=sc.nextInt();
cube(lb, ub);
cube(lb, ub);
}
}
OUTPUT:
Cube of 1 is 1
Cube of 2 is 8
Cube of 3 is 27
Cube of 4 is 64
Cube of 5 is 125
Cube of 6 is 216
Cube of 7 is 343
Cube of 8 is 512
Cube of 9 is 729
Enter Lower Bound
1
Enter Upper Bound
4
Cube of 2 is 8
Cube of 3 is 27
Cube of 2 is 8
Cube of 3 is 27
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.