Write a program (in Java) that prompts the user to input a number, and outputs t
ID: 3592914 • Letter: W
Question
Write a program (in Java) that prompts the user to input a number, and outputs the cubed value of the number. Then program computes and outputs the sum of following sequence, 1 + 8 + 27 + 64 + 125 + 216 + 343.
Given Code:
import java.util.Scanner;
public class Lab6Part1 {
/**
* Returns an integer whose value is a^3 (a cubed).
* @param any integer to be cubed
* @return a^3 (a cubed)
* @throws none.
*/
public static int cubed(int a){
int cube = a * a * a;
return cube;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer");
int x = in.nextInt();
// invoking the cubed method
System.out.println("The cubed value of "+ x + " is "
+ Lab6Part1.cubed(x)+".");
/* Invoke the cubed method to find the sum of
* 1 + 8 + 27 + 64 + 125 + 216 + 343
*/
}
}
Identify the following of the method cubed:
*1) return type of the method :
*2) type of the formal parameter :
*3) identifier of the formal parameter:
*4) signature of the method:
*5) all local variables of the method:
*6) Identify the statement that invokes cubed method:
*7) identifier of the actual parameter (or argument):
Explanation / Answer
Lab6Part1.java
import java.util.Scanner;
public class Lab6Part1 {
/**
* Returns an integer whose value is a^3 (a cubed).
* @param any integer to be cubed
* @return a^3 (a cubed)
* @throws none.
*/
public static int cubed(int a){
int cube = a * a * a;
return cube;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer");
int x = in.nextInt();
int sumCube = 0;
// invoking the cubed method
for(int i=1; i<=x;i++) {
int cube = Lab6Part1.cubed(i);
sumCube+=cube;
System.out.println("The cubed value of "+ i + " is "
+ cube+".");
}
/* Invoke the cubed method to find the sum of
* 1 + 8 + 27 + 64 + 125 + 216 + 343
*/
System.out.println("Sum of Cubes is "+sumCube);
}
}
Output:
Enter an integer
7
The cubed value of 1 is 1.
The cubed value of 2 is 8.
The cubed value of 3 is 27.
The cubed value of 4 is 64.
The cubed value of 5 is 125.
The cubed value of 6 is 216.
The cubed value of 7 is 343.
Sum of Cubes is 784
Identify the following of the method cubed:
*1) return type of the method :
Answer: int
*2) type of the formal parameter :
Answer: int
*3) identifier of the formal parameter:
Answer: a
*4) signature of the method:
Answer: cubed
*5) all local variables of the method:
Answer: cube
*6) Identify the statement that invokes cubed method:
Answer: Lab6Part1.cubed(i)
*7) identifier of the actual parameter (or argument):
Answer: i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.