Here's A Java Calculator Program I Just Made Recently, But It Doesn't Meet My Expectations! I Want It In A More Convenient Way Like It Has 6 Classes And Some Exclamation Marks, I Wanna Get A+ So Please Help Me!
1) Can I loop the codes so after displaying the answer, It runs the code again?
2) Can I somehow decrease the number of classes and the length of codes?
3) Can I clear screen in the console like in C++, So it should display a separate view for the Intro and the answer?
Here's The Code:
import java.util.Scanner;
public class javaCalc {
public static void welcome() {
System.out.println("Welcome to Calculator.java v0.1");
System.out.println("(Developed By RAZ0229)");
}
public static void main(String[] args) {
welcome();
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
default: System.out.println("\n(Invalid Argument)");
return;
}
}
public static void additionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne + numTwo;
System.out.println(numOne + " + " + numTwo + " = " + answer);
}
public static void substractionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne - numTwo;
System.out.println(numOne + " - " + numTwo + " = " + answer);
}
public static void multiplicationMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne * numTwo;
System.out.println(numOne + " x " + numTwo + " = " + answer);
}
public static void divisionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne / numTwo;
System.out.println(numOne + " / " + numTwo + " = " + answer);
}
}
You are asking for two floats in every method and using the same prints many times, so you can just create some method such as this and call it inside your operation method to stop repeating code (constantly repeated blocks of code is a strong indicator that the block can probably be abstracted into its own method):
public static float[] getValues(){
float[] values;
/*Implement your logic here asking user for floats, then put into above array
and do calculations in your methods using float array*/
return values;
}
You can also loop your main by wrapping it in a while loop and adding an extra case to your switch statement like so (if you would like to exit program, enter 5):
public static void main(String[] args) {
welcome();
while (true){
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Quit");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
case 5: System.exit(0);
default: System.out.println("\n(Invalid Argument)");
return;
}
}
}
Related
I am trying to program a menu for my calculator. I do not know how to get out of my inner loop and get back to my outer loop once I have executed the inner loop. I have to finish the whole case of the inner loop before I can get back to the outer loop. Is there a way that I can go back and forth of the nested switch whenever I want to? and when I execute my outer switch case 3, it executes infinitely. Maybe because it's inside a loop.
import java.util.Scanner;
public class MainInterface {
static Scanner input = new Scanner(System.in);
static boolean hasRun = false;
public static void main(String args[])
{
Calculator Mycal = new SimpleCalculator();
ScientificCalculator Mycal2 = new ScientificCalculator();
mainMenu();
int choice = input.nextInt();
do {
System.out.println("\n");
switch(choice) // outer switch
{
case 1: simpleCalculatorMenu();
int choice2 = input.nextInt();
switch(choice2) // inner switch
{
case 1: //ADDITION
System.out.println("ADDITION");
System.out.println("\n");
System.out.println("Enter first number: ");
float x = input.nextFloat();
System.out.println("Enter second number: ");
float y = input.nextFloat();
System.out.println("\n");
System.out.println("The sum of " + x + " " + y + " is " + Mycal.add(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
}
break;
case 2: System.out.println("SCIENTIFIC CALCULATOR");
System.out.println("\n");
System.out.println("1. power(x, pow) 2. sin(xDeg) 3. cos(xDeg) 4. tan(xDeg)");
System.out.println("\n");
System.out.println("5. pi() 6. fact(x) ");
int choice3 = input.nextInt();
switch(choice3) {
case 1:
System.out.println("POWER");
System.out.println("\n");
System.out.println("Enter first number: ");
double x = input.nextDouble();
System.out.println("Enter second number: ");
double y = input.nextDouble();
System.out.println("\n");
System.out.println("The power of " + x + " to the power of " + y + " is " + Mycal2.power(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
case 2:
}
break;
case 3: mainMenu();
break;
case 4: System.exit(choice);
break;
}
} while(choice != 0);
}
public static void simpleCalculatorMenu () {
System.out.println("SIMPLE CALCULATOR");
System.out.println("\n");
System.out.println("1. Addition 2. Subtraction 3. Multiplication 4. Division");
System.out.println("\n");
System.out.println("5. Squared Root 6. Squared 7. Cube 8. Discount ");
}
public static void mainMenu () {
System.out.println("What calculator do you want to use?");
System.out.println("\n");
System.out.println("1. Simple Calculator \t 2. Scientific Calculator");
}
}
by adding label you can try like this
OUTER:
switch(condition1) {
case x:
switch(condition2) {
case y:
System.out.println("hello");
break OUTER;
}
}
i have a calculator class and i want the operation input to be specific to these strings "+, -, /, *" if its not, i want to print a message to enter the operator again.
and please if someone would lessen my code please do it. thanks
here is my code
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
String operation;
Double fnum, lnum, answer;
System.out.println("Enter first Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter first Number: ");
}
fnum = num.nextDouble();
System.out.println("Enter Operation: ");
//HERE IM BEING CONFUSED
while (!num.equals("+, -, /, *"))
{
num.next();
System.out.print("Enter Operator: ");
}
operation = num.next();
//End
System.out.println("Enter Second Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter Second Number: ");
}
lnum = num.nextDouble();
switch (operation) {
case "+":
answer = fnum + lnum;
System.out.print("Equals= " + answer);
break;
case "-":
answer = fnum - lnum;
System.out.print("Equals= " + answer);
break;
case "*":
answer = fnum * lnum;
System.out.println("Equals= " + answer);
break;
case "/":
answer = fnum / lnum;
System.out.println("Equals= " + answer);
break;
default:
System.out.println("wrong operator");
break;
}
}
}
This is a way to use operators with SWITCH sentence.
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
double v1,v2;
String v3;
System.out.print("Enter first number: ");
v1 = num.nextDouble();
System.out.print("Enter second number: ");
v2 = num.nextDouble();
System.out.print("Enter operation [+] [-] [*] [/]: ");
v3 = num.next();
System.out.println( ("+".equals(v3) ) ? Operators.ADD.calculate(v1, v2) : "....");
}
public enum Operators {
ADD;
double calculate(double x, double y) {
switch (this) {
case ADD:
return x + y;
default:
throw new AssertionError("Unknown operations " + this);
}
}
}
I have to make a simple calculator in java that calls methods instead of repeating the entire program over and over again. All of my methods work, and it allows the user to make incorrect choices for as long as they want until a correct choice is made. The problem I am having is that it won't break out of a case after the operation is done and the answer is given.
package menuDrivenCalculator;
import java.util.Scanner;
public class MenuDrivenCalculator {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int menuOption = getMenuOption();
while (menuOption < 1 || menuOption > 6) {
System.out.println("I'm sorry, " + menuOption + " is not a valid choice. Please try again.");
menuOption = getMenuOption();
if (menuOption >= 1 && menuOption <= 6) {
break;
}
}
while (menuOption >= 1 && menuOption <= 6) {
switch (menuOption) {
case 1:
System.out.print("What is the first number? ");
double operand1 = getOperand();
System.out.print("What is the second number?");
double operand2 = getOperand();
double add = add(operand1, operand2);
System.out.println("Your answer is: " + add);
break;
case 2:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double subtract = subtract(operand1, operand2);
System.out.println("Your answer is: " + subtract);
break;
case 3:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double multiply = multiply(operand1, operand2);
System.out.println("Your answer is: " + multiply);
break;
case 4:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double divide = divide(operand1, operand2);
System.out.println("Your answer is: " + divide);
break;
case 5:
System.out.print("What is the lower limit? ");
operand1 = getOperand();
System.out.print("What is the upper limit?");
operand2 = getOperand();
double random = random(operand1, operand2);
System.out.println("Your answer is: " + random);
break;
case 6:
System.out.println("Goodbye!");
return;
}
}
}
public static int getMenuOption() {
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Generate a random number");
System.out.println("6. Quit\n");
System.out.print("What would you like to do? ");
int menuOption = input.nextInt();
return menuOption;
}
public static double getOperand() {
double operand = input.nextDouble();
return operand;
}
public static double add(double operand1, double operand2) {
double add = (operand1 + operand2);
return add;
}
public static double subtract(double operand1, double operand2) {
double subtract = (operand1 - operand2);
return subtract;
}
public static double multiply(double operand1, double operand2) {
double multiply = (operand1 * operand2);
return multiply;
}
public static double divide(double operand1, double operand2) {
double divide = 0;
if (operand2 == 0) {
divide = Double.NaN;
} else if (operand2 != 0) {
divide = (operand1 / operand2);
}
return divide;
}
public static double random(double operand1, double operand2) {
double random = Math.random() * operand2 + operand1;
return random;
}
}
What is happening is the program prompts the user for input for the same operation over and over again until you manually stop the program from running. I've tried putting the entire thing in different types of loops and nothing has changed.
Since the code for performing the operations is inside a loop (while (menuOption >= 1 && menuOption <= 6)) the program will continue to cycle on the last chosen operation.
You need a loop that includes also the getMenuOption() method so the user can choose another operation.
To do so, instead of having 2 separate loops, you could have just 1 to take care of everything (remember also you could use the default case inside the switch).
Since it seems homework I will not give you the complete solution but if you have other specific doubts let us know.
If you dont want it to repeat over,why put the switch statement in while loop?
Replace 'while' with 'if' in your code
I'm trying to write a simple calculator with 2 functions (for right now). I'm using Java in Eclipse. I can't get what I'm currently attempting to work. I want to have just the menu item as its own function and then the case switch to be its own function after taking in the entryChoice. When I run this code as is and I make a selection all it does is repeats "Enter two numbers..." OR if I separate the userInput into each case to be printed after the entryChoice is passed, it breaks. Any advice?
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7)
{
userSelection(entryChoice);
}
System.exit(0);
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x,y);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
break;
case 7:
result = 0;
break;
default:
}
return result;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7) {
System.out.println(userSelection(entryChoice));
displayMenu();
entryChoice = scanChoice.nextInt();
}
System.exit(0);
}
public static void displayMenu() {
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice) {
Scanner userInput = new Scanner(System.in);
double x = 0;
double y = 0;
double result = 0;
if (entryChoice == 6) {
System.out.println("Enter one number: ");
x = userInput.nextDouble();
} else {
System.out.println("Enter two numbers seperated by a space");
x = userInput.nextDouble();
y = userInput.nextDouble();
}
switch (entryChoice) {
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x, y);
break;
case 6:
result = Math.sqrt(x);
break;
case 7:
result = 0;
break;
default:
}
return result;
}
I think what you want to have is the following:
public static void main(String[] args) {
// TODO Auto-generated method stub
int entryChoice = -1;
while (entryChoice != 7)
{
displayMenu();
Scanner scanChoice = new Scanner(System.in);
entryChoice = scanChoice.nextInt();
if (entryChoice != 7)
System.out.println(userSelection(entryChoice));
}
System.exit(0);
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x,y);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
break;
default:
}
return result;
}
Please try this, it is working as per your expectation
import java.util.*;
class Calculator
{
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7)
{
userSelection(entryChoice);
}
System.exit(0);
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
System.out.println("Result is :"+result);
break;
case 2:
result = x - y;
System.out.println("Result is :"+result);
break;
case 3:
result = x * y;
System.out.println("Result is :"+result);
break;
case 4:
result = x / y;
System.out.println("Result is :"+result);
break;
case 5:
result = Math.pow(x,y);
System.out.println("Result is :"+result);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
System.out.println("Result is :"+result);
break;
case 7:
result = 0;
break;
default:
}
displayMenu();
return result;
}
}
i am new to java and have been trying to learn it for a while now. This program that i am making is a basic calculator, but with the user inputting their choice of operation. I am having trouble finding out a way to put the 3 variables/operators together.
Here is what I've got.
package calulator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String first;
String second;
String operator;
int numone;
int numtwo;
int answer;
System.out.println("Enter first number.");
first = scanner.nextLine();
numone = Integer.parseInt(first);
System.out.println("Enter operator.");
operator = scanner.nextLine();
//also don't know if i should convert this to a char or a string.
System.out.println("Enter Second number.");
second = scanner.nextLine();
numtwo = Integer.parseInt(second);
answer = numone + operator + numtwo;
//I need a way so ^^^^ that you can implement the operator.
System.out.println(answer);
}
}
switch(operator) {
case "+": answer = numone + numtwo; break;
case "-": answer = numone - numtwo; break;
case "*": answer = numone * numtwo; break;
case "/": answer = numone / numtwo; break;
// any other operators you want go here
default: throw new RuntimeException(operator+" isn't a valid operator!");
}
There is not a shorter way.