\"Create the logic for a program that continuously prompts a user for a numeric
ID: 3583785 • Letter: #
Question
"Create the logic for a program that continuously prompts a user for a numeric value until the user enters 0. The application passes the value in turn to a method that squares the number and to a method that cubes the number. The program displays the results before prompting the user again. Create the two methods that respectively square and cube a number that is passed to them, returning the calculated value."
Your are building the logic for a special purpose calculator that only calculates two things: the square and the cube of a number entered by the user. The squaring and cubing of the number are each done in separate modules that are called by the main logic. The main logic asks for the number from the user, calls the square function and the cube function, and displays the results. A loop structure is used in the main logic that keeps repeating the process until the user enters a 0 to indicate they are done.
Note that Farrell uses the term method for a module that performs a sub-process or subtask. The term function is more generally used, unless the module is associated with a specific software object, in which case method is the appropriate term.
Please use either Pseudocode or Flowchart
Explanation / Answer
package com.se.beans;
import java.util.Scanner;
public class SwitchTest {
public static int square(int number) {
return number * number;
}
public static int cube(int number) {
return number * number * number;
}
public static void main(String[] args) {
// to create scanner object
Scanner scanner = new Scanner(System.in);
System.out.println("1.square");
System.out.println("2.cube");
System.out.println("3.exit");
System.out.println("enter your choice");
System.out.println("-----------------");
int choice = scanner.nextInt();
switch (choice) {
/*
* System.out.println("1.square"); System.out.println("2.cube");
* System.out.println("3.exit");
*/
case 1:
System.out.println("enter the number");
int number = scanner.nextInt();
int result = square(number);
System.out.println("the square of a given no is:" + result);
main(null);
break;
case 2:
System.out.println("enter the number");
int number1 = scanner.nextInt();
int result1 = cube(number1);
System.out.println("the cube of a given no is:" + result1);
main(null);
break;
case 3:
System.out
.println("do you want exit from the calculator if yes press 0,if no press any");
int confirmation = scanner.nextInt();
if (confirmation == 0)
System.exit(0);
else
main(null);
break;
default:
System.out.println("wrong choice");
main(null);
break;
}
}
}
output
1.square
2.cube
3.exit
enter your choice
-----------------
1
enter the number
25
the square of a given no is:625
1.square
2.cube
3.exit
enter your choice
-----------------
2
enter the number
25
the cube of a given no is:15625
1.square
2.cube
3.exit
enter your choice
-----------------
3
do you want exit from the calculator if yes press 0,if no press any
5
1.square
2.cube
3.exit
enter your choice
-----------------
3
do you want exit from the calculator if yes press 0,if no press any
0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.