How can I add exponent in my code - java

import java.util.Scanner;
public class Calculator
{
public static void main(String[] args )
{
Scanner userInput = new Scanner(System.in);
String operator;
double num1,num2,answer = 0;
System.out.println("Enter first number: ");
num1 = userInput.nextDouble();
System.out.println("Enter operator: ");
operator = userInput.next();
System.out.println("Enter second number: ");
num2 = userInput.nextDouble();
if (operator.equals ("+")){
answer = num1 + num2;
}
else if (operator.equals ("-")){
answer = num1 - num2;
}
else if (operator.equals ("*")){
answer = num1 * num2;
}
else if (operator.equals ("/")){
answer = num1 / num2;
}
System.out.println("First number:" + num1);
System.out.println("Operator:" + operator);
System.out.println("Second number:" + num2);
System.out.println("Answer: " + answer);
}
}

Use this function:
Math.pow(x, y)
In this way: place the code
else if (operator.equals ("^")){
answer = Math.pow(num1, num2);
}
just after your present code
else if (operator.equals ("/")){
answer = num1 / num2;
}
So this part of code will then be
else if (operator.equals ("/")){
answer = num1 / num2;
}
else if (operator.equals ("^")){
answer = Math.pow(num1, num2);
}

Related

Java Calculator Switch Issue

I incorporated a couple different methods I've seen on here. Does anyone know how to fix this problem I am having? When you use this code, It asks for you to enter the mathematical operator, BUT when I do if I enter +9 or -%, it will still work and use the first symbol. I want it to give an error if someone inputs */ instead of just *. I even tried switching the case to numbers (ie case 1:) and it will do the same thing if I set addition to 1 and if I enter 15, it will read the one and do addition. Any ideas?
import java.util.Scanner;
public class javacalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double num1, num2, answer;
char userOperation;
System.out.print("Enter your full name: ");
String userName = input.nextLine();
System.out.println("+ Addition");
System.out.println("- Subtraction");
System.out.println("* Multiplication");
System.out.println("/ Division");
System.out.println("% Modulus");
System.out.println("\n");
System.out.print("Enter mathematical operator e.g. + for Addition: ");
userOperation = operation.next().charAt(0);
boolean invalidOperator = false;
switch (userOperation) {
case '+':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 + num2;
System.out.print(num1 + " + " + num2 + " = " + answer);
break;
case '-':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 - num2;
System.out.print(num1 + " - " + num2 + " = " + answer);
break;
case '*':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 * num2;
System.out.print(num1 + " * " + num2 + " = " + answer);
break;
case '/':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 / num2;
System.out.print(num1 + " / " + num2 + " = " + answer);
break;
case '%':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 % num2;
System.out.print(num1 + " % " + num2 + " = " + answer);
break;
default:
System.out.println("Invalid operator!");
break;
}
}
}
Let us break this down: operation.next().charAt(0);
operation.next() -> Gives you full input string
.charAt(0); -> returns the first character of the full input string
So anything we enter +9 or */ -> it returns the first element.
Now let's handle this case:
String o = operation.next(); // here is complete input
if(o.length()!=1){
userOperation = 0; //if length of input is not 1 we set custom value
}else{
userOperation = o.charAt(0); //get and set the first and only element
}
Hopefully it helps!

How can i make this code simpler? Also how can i check if input is an int?

Hi im a beginner in Java. How could i make this bais calculator simpler?
Also, i want the user to get an error if num1 or num2 arent Integers. How could i do that?
I already tried parseint and scanner.hasnextint but couldnt get them to work.
import java.util.Scanner;
public class topfirst{
public static void main(String[] args) {
int num1;
int num2;
int result;
String yesorno;
String yes = "yes";
String op;
while(yesorno.equals(yes)){
System.out.println("Please enter the first number :");
Scanner inputnum1 = new Scanner(System.in);
num1 = inputnum1.nextInt();
System.out.println("Please enter the second number :");
Scanner inputnum2 = new Scanner(System.in);
num2 = inputnum2.nextInt();
System.out.println("Please enter an operation :");
Scanner inputop = new Scanner(System.in);
op = inputop.next();
String minus = "-";
String plus = "+";
String multipl = "*";
String div = "/";
while ((!op.equals(minus)) && (!op.equals(plus)) && (!op.equals(multipl)) && (!op.equals(div)))
{
System.out.println("Incorrent operation, try again.");
op = inputop.next();
}
switch(op){
case "+":
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case "-":
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case "*":
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case "/":
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
break;
}
System.out.println("Do you want to calculate something else?");
Scanner inyesorno = new Scanner(System.in);
yesorno = inyesorno.next();
}}
}
import java.util.Scanner;
public class TopFirst {
private static final String PLUS = "+";
private static final String MINUS = "-";
private static final String MULTIPLY = "*";
private static final String DIV = "/";
private static final Scanner INPUT = new Scanner(System.in);
public static void main(String[] args) {
do {
System.out.print("Please enter the first number: ");
int num1 = readInt();
System.out.print("Please enter the second number: ");
int num2 = readInt();
System.out.print("Please enter an operator: ");
String operator = readOperator();
int result = countResult(num1, num2, operator);
System.out.printf("%d %s %d = %d%n", num1, operator, num2, result);
System.out.println("Do you want to calculate something else?");
} while (INPUT.next().equals("yes"));
}
private static String readOperator() {
String operator = INPUT.next();
if (isCorrect(operator)) {
return operator;
} else {
System.out.print("Incorrect operator, try again: ");
return readOperator();
}
}
private static int readInt() {
try {
return Integer.parseInt(INPUT.next());
} catch (NumberFormatException e) {
System.out.print("Incorrect number, try again: ");
return readInt();
}
}
private static boolean isCorrect(String operator) {
return MINUS.equals(operator)
|| PLUS.equals(operator)
|| MULTIPLY.equals(operator)
|| DIV.equals(operator);
}
private static int countResult(int num1,
int num2,
String operator) {
switch (operator) {
case PLUS:
return num1 + num2;
case MINUS:
return num1 - num2;
case MULTIPLY:
return num1 * num2;
case DIV:
return num1 / num2;
default:
return 0;
}
}
}
One Error that needs fixing before anything else:
String yesorno;
String yes = "yes";
String op;
while(yesorno.equals(yes)){
[...]
}
Your String yesorno does NOT equal "yes", so the while-loop is never executed in the first place!

How to print out the GCD in different format

I am trying to ask the user to enter two integers and have the message to say
"The GCD of "first integer" and "second integer" is "GCD"
I have all my calculations right but it is just printing out my num1 for all values.
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = scan.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scan.nextInt();
while (num1 != num2)
{
if (num1> num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
}
System.out.println("The gcd of" + num1 + " and " + num2 + " is " + num1);
}
}
In your while loop you check if num1 != num2 so when the println gets executed num1 will have the same value as num2.

How does number swapping work with if statements?

I want to know exactly how these if statements are switching the numbers. I have never been asked to do non-descending order before so I took a little snip off the internet.
import java.util.Scanner;
/**
* Created by Nicholas on 10/26/2015.
*/
public class Main {
final static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter three numbers.");
System.out.println();
System.out.print("Number 1: ");
int num1 = userInput.nextInt();
System.out.println();
System.out.print("Number 2: ");
int num2 = userInput.nextInt();
System.out.println();
System.out.print("Number 3: ");
int num3 = userInput.nextInt();
System.out.println();
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.print("The numbers in non-descending order are, " + num1 + " " + num2 + " " + num3);
}
}
The if statements switch the numbers by creating a temporary value to store the old number in before setting it to the new number, the other number is then set to the old, saved number
A more interesting way of switching numbers is with bitwise operators
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
This works from xoring the bits together, and because xor inverts itself when given the same value ((A xor B) xor B) == A

Basic Calculator

Im having problems with my program. the program displays the first JOption message dialog box, but when you input a value, it fails to display the second dialog box??
import java.util.Scanner;
import javax.swing.JOptionPane;
public class javaCalculator
{
public static void main(String[] args)
{
int num1;
int num2;
String operation;
Scanner input = new Scanner(System.in);
JOptionPane.showInputDialog(null,"please enter the first number");
num1 = input.nextInt();
JOptionPane.showInputDialog(null,"please enter the second number");
num2 = input.nextInt();
JOptionPane.showInputDialog(null,"Please enter operation");
operation = input.next();
if (operation.equals ("+"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 + num2));
}
if (operation.equals ("-"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 - num2));
}
if (operation.equals ("/"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 / num2));
}
if (operation.equals ("*"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 * num2));
}
}
}
Read the numbers from the dialog output instead of from System.in:
String firstNumber = JOptionPane.showInputDialog(null,"please enter the first number");
if (firstNumber != null) {
num1 = Integer.parseInt(firstNumber);
}
Scanner is unnecessary in your code.
You can try using that sample code:
import javax.swing.JOptionPane;
public class Hw4_3 {
private static boolean flag;
public static void main(String[] args) {
do {
String[] expression = getTask();
double result = calculate(expression);
display(expression, result);
repeat();
} while (flag);
}
public static String[] getTask()
{
String bleh = JOptionPane.showInputDialog(null, "Please enter what you would like to calculate: ");
String[] tokens = bleh.split(" ");
return tokens;
}
public static double calculate(String[] data)
{
double num1 = Double.parseDouble(data[0]);
double num2 = Double.parseDouble(data[2]);
double result = 0;
if(data[1].equals("+"))
{
result = add(num1, num2);
return result;
}
else if(data[1].equals("-"))
{
result = subtract(num1, num2);
return result;
}
else if(data[1].equals("*"))
{
result = multiply(num1, num2);
return result;
}
else
{
if(num2 == 0)
{
JOptionPane.showMessageDialog(null, "Sytax error, divide by zero");
}
result = divide(num1, num2);
return result;
}
}
public static double add(double num1, double num2)
{
return num1 + num2;
}
public static double subtract(double num1, double num2)
{
return num1 - num2;
}
public static double multiply(double num1, double num2)
{
return num1 * num2;
}
public static double divide(double num1, double num2)
{
return num1 / num2;
}
public static void display(String[] data, double result)
{
if(data[1].equals("/") && data[2].equals("0"))
{
}
else
{
JOptionPane.showMessageDialog(null, data[0] + " " + data[1] + " " + data[2] + " = " + result);
}
}
public static void repeat()
{
int bleh = JOptionPane.showConfirmDialog(null, "More?",null, JOptionPane.YES_NO_OPTION);
if(bleh == JOptionPane.NO_OPTION)
{
flag = false;
JOptionPane.showMessageDialog(null, "Have a good day.");
}
else
flag = true;
}
}

Categories