Question
Hello your great assistance and help are required to correctly complete this code. Write a Java program which asks the user for a number, a
Hello your great assistance and help are required to correctly complete this code.
Write a Java program which asks the user for a number, a mathematical operation, and another number, and then outputs the result of the requested computation. For example, if you typed in 3, +, and 5, the program would output 3 + 5 = 8.
Your program will define a single class with a main method, which contains all program logic. A starter file, ProjectOne.java, is provided to help with this structure. The program should first prompt the user for a number. For example, it might say Enter the first number: . Next, the program should tell the user what the valid operations are and ask the user to choose one. For example, it might say Enter an operation. Choices are +, -, *, /, or %:. These operations correspond to addition, subtraction, multiplication, division, and modulo (respectively). If the user enters anything other than one of the five above operations, the program should tell the user that they have entered an invalid operation and then exit. For example, Error! $ is not a valid operation!. If the user entered a valid operation, the program should then prompt them for the second number. Enter the second number: If the user requested division or modulo and entered zero for the second number, warn the user (e.g. Sorry, you cant divide by zero!) and exit.
Finally, the program should output the whole equation and the result. For example, if the inputs were 2, +, and 3, the program might output 2.000000 + 3.000000 = 5.000000 The program should use double-precision floating-point numbers (conveniently, these are Javas double data type).
import java.util.Scanner;
/* * Project One */
public class ProjectOne {
public static void main(String[] args) {
// Your code here Scanner kbd = new Scanner(System.in);
System.out.println("Enter your number");
double number; double result;
number = kbd.nextDouble(); kbd.nextLine();
System.out.println("Enter an operation. Choices are +, -, *, /, %: ");
String operator;
operator = kbd.nextLine();
if (!(operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/") || operator.equals("%"))) { System.out.println("Not a valid operator"); System.exit(0); }
System.out.println("Enter another number");
double secondnumber; secondnumber = kbd.nextDouble(); kbd.nextLine();
if((secondnumber == 0 && operator.equals("/")|| operator.equals("%"))) { System.out.println("Sorry you cannot divide by zero."); System.exit(0); }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started