Converting infix expressions to postfix expressions - java

I have a problem i can't find the wrong in this function it sometimes do well with any input but it cached when there is parentheses in the input {i want to know where is the wrong in this code and how to fix it , and Is there another better way instead this way}
public static String Converting_infix_expressions_to_postfix_expressions(String infix) throws Exception{
StringTokenizer st = new StringTokenizer(infix);
int numOF_tokens = st.countTokens();
String postfix = "" ;
for (int i = 1; i <= numOF_tokens; i++) {
String term = st.nextToken();
try { // if it is an Float there is no problem will happen
float x = Float.parseFloat(term);
postfix += x +" " ;
System.out.println("term is number " + term);
} catch (Exception e) {
System.out.println("term is symbol " + term);
if(stack.isEmpty())
stack.push(term);
else if(term == "(")
stack.push(term);
else if(term == ")"){
while((String)stack.peek() != "(")
postfix += stack.pop() +" ";
stack.pop();
}
else{
int x = 0,y = 0;
switch(term){
case "+": x = 1; break;
case "-": x = 1; break;
case "*": x = 2; break;
case "/": x = 2; break;
}
switch((String)stack.peek()){
case "+": y = 1; break;
case "-": y = 1; break;
case "*": y = 2; break;
case "/": y = 2; break;
}
if(x > y)
stack.push(term);
else {
int x1 = x , y1 = y;
boolean puchedBefore = false;
while(x1 <= y1){
postfix += stack.pop() +" ";
if(stack.isEmpty() || stack.peek() == "(" ){
stack.push(term);
puchedBefore = true;
break;
}
else{
switch(term){
case "+": x1 = 1; break;
case "-": x1 = 1; break;
case "*": x1 = 2; break;
case "/": x1 = 2; break;
}
switch((String)stack.peek()){
case "+": y1 = 1; break;
case "-": y1 = 1; break;
case "*": y1 = 2; break;
case "/": y1 = 2; break;
}
}
}
if(!puchedBefore)
stack.push(term);
}
}
}
}
while(!stack.isEmpty()){
postfix += stack.pop() +" ";
}
System.out.println("The postfix expression is : " + postfix);
return postfix;
}

You have a couple of problems with your code.
You should make a custom string tokenizer since there may not be a space between a parenthesis and a number. Ex: (5 + 6)
The try-catch block is not being used properly. Consider, first checking if the field is symbol then moving on to parsing it as a Float. This way you can avoid having most of your code appear in the catch block.
The bug you are referring to can be fixed with the follow change to line 18.
while(!stack.isEmpty() && (String)stack.peek() != "(")

Related

calculating sin, cos, log using stack and post fix notation in java

I wanna write a program that that take a infix string and change it to postfix then evaluate postfix and print the answer ; for +-*/^ it's easy just need precedence of operators but I don't know what should I do for sin cos log and other math function.
private static String infixToPostfix(String infix) {
String[] exp = infix.split("");
Stack<String> stack = new Stack<>();
String result = "";
for (int i = 0; i < exp.length; i++){
if (exp[i].equals("(")) {
stack.push(exp[i]);
}
else if (isOperator(exp[i]))
{
while (!stack.isEmpty() && precedence(exp[i]) <= precedence(stack.getTop())){
result += stack.pop() + " ";
}
stack.push(exp[i]);
}
else if (exp[i].equals(")"))
{
while (!stack.isEmpty() && !stack.getTop().equals("(")){
result += stack.pop() + " ";
}
stack.pop();
}
else if (Character.isLetterOrDigit(infix.charAt(i)) || exp[i].equals(".")){
boolean haveDot = exp[i].equals(".");
String temp = haveDot ? "0." : exp[i];
while ((i + 1) < exp.length && (Character.isLetterOrDigit(infix.charAt(i + 1)) || exp[i + 1].equals("."))){
temp += exp[i + 1];
i++;
}
result += temp + " ";
}
}
while (!stack.isEmpty()){
result += stack.pop() + " ";
}
return result;
}
it is working correctley !
but this
private static Double postFixEvaluator(String[] postFix) {
Stack<Double> operands = new Stack<>();
double value = 0.0;
for (int str = 0; str < postFix.length; str++) {
if (postFix[str].trim().equals("")) {
continue;
}
switch (postFix[str]) {
case "+":
case "-":
case "*":
case "/":
case "^":
Double right = operands.pop();
Double left = operands.pop();
long intValue = 0;
switch (postFix[str]) {
case "+":
value = left + right;
break;
case "-":
value = left - right;
break;
case "*":
value = left * right;
break;
case "/":
value = left / right;
break;
case "^":
value = Math.pow(left, right);
break;
default:
break;
}
case "sin":
case "cos":
case "tan":
case "cot":
if (Character.isLetterOrDigit(Arrays.toString(postFix).charAt(str + 2))) {
str++;
break;
}
else{
Double oper = operands.pop();
switch (postFix[str]) {
case "sin":
value = Math.sin(oper);
break;
case "cos":
value = Math.cos(oper);
break;
case "tan":
value = Math.tan(oper);
break;
case "cot":
value = 1 / Math.tan(oper);
break;
}
}
operands.push(value);
break;
default:
operands.push(Double.parseDouble(postFix[str]));
break;
}
}
return operands.pop();
}
it's not working correctly .

How to call a java main function?

public class Calculator {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Calculator operand1 operator operand2");
System.exit(0);
}
int result = 0;
switch (args[1].charAt(0)) {
case '+':
result = Integer.parseInt(args[0]) + Integer.parseInt(args[1]);
break;
case '-':
result = Integer.parseInt(args[0]) - Integer.parseInt(args[1]);
break;
case '*':
result = Integer.parseInt(args[0]) * Integer.parseInt(args[1]);
break;
case '/':
result = Integer.parseInt(args[0]) / Integer.parseInt(args[1]);
break;
}
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
}
}
When I ran Calculator.java in terminal, I tried a few ways like this:
Rasperry:src maggiesmac$ javac Calculator.java
Rasperry:src maggiesmac$ java Calculator 1+2
Usage: java Calculator operand1 operator operand2
Rasperry:src maggiesmac$ java Calculator 1 + 2
Exception in thread "main" java.lang.NumberFormatException: For input string: "+"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:572)
at java.lang.Integer.parseInt(Integer.java:615)
at Calculator.main(Calculator.java:15)
Rasperry:src maggiesmac$ java Calculator.main(1,+,2)
-bash: syntax error near unexpected token `('
So how can I call the Java main() method? How should I pass the parameters to it?
Please change the index in the case structure (as suggested by #SotiriosDelimanolis). Also, we can use String in case:
public class Calculator {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Calculator operand1 operator operand2");
System.exit(0);
}
int result = 0;
switch (args[1]) {
case "+":
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
break;
case "-":
result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
break;
case "*":
result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
break;
case "/":
result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
break;
}
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
}
}
You can also try the below the code get the result
class Calculator {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Calculator operand1 operator operand2");
System.exit(0);
}
int result = 0;
char op = args[1].charAt(0);
System.out.println((int)op);
int ope = (int)op;
switch (ope) {
case 43:
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
break;
case 45:
result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
break;
case 42:
result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
break;
case 47:
result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
break;
}
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
}
}

Calculator using Switch in Java

I'm new to Java programming. I am trying to make a Fraction Calculator but when I try to run the program it gives me an error. The error is with the Switch statements but I don't know what happened.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Welcome to My FracCalc");
boolean continueProcessing = true;
while (continueProcessing) {
System.out.println("Type an expression and press <enter>");
String Operand1 = console.next();
if (Operand1.equals("quit")) {
continueProcessing = false;
System.out.println("Good bye!");
break;
} else {
Operand1 = parseFullNumber(Operand1);
}
String Operator = console.next();
if (Operator.equals("quit")) {
continueProcessing = false;
System.out.println("Good bye!");
break;
} else if (Operator.equals("+") || Operator.equals("-") || Operator.equals("/") || Operator.equals("*")) {
} else {
throw new ArithmeticException();
}
String Operand2 = console.next();
if (Operand2.equals("quit")) {
continueProcessing = false;
System.out.println("Good bye!");
break;
} else {
Operand2 = parseFullNumber(Operand2);
}
System.out.println( Operand1 + " " + Operator + " " + Operand2);
//System.out.println("First Fraction is: " + Operand1);
//System.out.println("Operator is: " + Operator);
//System.out.println("Second Fraction is: " + Operand2);
float answer;
System.out.println(Operator);
switch (Operator) {
case "+":
answer = Operand1 + Operand2;
break;
case "-":
answer = Operand1 - Operand2;
break;
case "*":
answer = Operand1 * Operand2;
break;
case "/":
answer = Operand1 / Operand2;
break;
}
}
}
public static String parseFullNumber(String input) {
int wholeNumber = 0;
int numerator = 0;
int denominator = 0;
;
int underscoreId = input.indexOf('_');
int slashId = input.indexOf('/');
// Check for underscore "_" //
if (underscoreId > -1) {
wholeNumber = Integer.parseInt(input.substring(0, underscoreId));
numerator = Integer.parseInt(input.substring(underscoreId + 1, slashId));
denominator = Integer.parseInt(input.substring(slashId + 1, input.length()));
} else {
if (slashId > -1) {
// no underscore but there is a slash //
numerator = Integer.parseInt(input.substring(0, slashId));
denominator = Integer.parseInt(input.substring(slashId + 1, input.length()));
} else {
// there is no underscore or slash //
wholeNumber = Integer.parseInt(input);
}
}
return simplify(wholeNumber, numerator, denominator);
}
//simplifying fractions //
public static String simplify(int wholeNumber, int numerator, int denominator) {
// absolute values //
int absNumerator = Math.abs(numerator);
// factor if applicable //
if (absNumerator > 1) {
int commonFactor = 1;
for (int i = 2; i < Math.min(absNumerator, denominator); i++) {
if (numerator % i == 0 && denominator % i == 0) {
commonFactor = i;
}
}
numerator /= commonFactor;
denominator /= commonFactor;
}
// reduce if applicable //
if (absNumerator > denominator) {
int reduction = numerator / denominator;
if (wholeNumber >= 0) {
wholeNumber += reduction;
} else {
wholeNumber -= reduction;
}
numerator %= denominator;
}
// prints //
if (wholeNumber != 0) {
if (numerator != 0) {
return wholeNumber + "_" + numerator + "/" + denominator;
} else {
return String.valueOf(wholeNumber);
}
} else {
if (numerator != 0) {
return numerator + "/" + denominator;
} else {
return String.valueOf(0);
}
}
}
}
Here is the error i got:
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
Type mismatch:
cannot convert from String to float The operator - is undefined for the argument type(s) java.lang.String, java.lang.String
The operator * is undefined for the argument type(s) java.lang.String, java.lang.String
The operator / is undefined for the argument type(s) java.lang.String, java.lang.String
at FracCalcApp.main(FracCalcApp.java:53)
Operand1 and Operand2 are String(s). You need to parse them before you can perform arithmetic. Something like,
double answer;
System.out.println(Operator);
switch (Operator) {
case "+":
answer = Double.valueOf(Operand1) + Double.valueOf(Operand2);
break;
case "-":
answer = Double.valueOf(Operand1) - Double.valueOf(Operand2);
break;
case "*":
answer = Double.valueOf(Operand1) * Double.valueOf(Operand2);
break;
case "/":
answer = Double.valueOf(Operand1) / Double.valueOf(Operand2);
break;
}
Finally, by convention, Java variables should start with a lower case letter; operand1, operand2 and operator.
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
float a = sc.nextFloat();
System.out.println("Enter second number");
float b = sc.nextFloat();
System.out.println("choose your operation");
char operator = sc.next().charAt(0);
float answer;
switch (operator){
case '+' :
answer = a +b;
System.out.println("Answer:"+answer);
break;
case '-' :
answer = a-b;
System.out.println("Answer:"+answer);
break;
case '*' :
answer = a*b;
System.out.println("Answer:"+answer);
break;
default:
answer = a/b;
System.out.println("Answer:"+answer);
}
}

Java display does not show whole method

Having trouble with this the whole day. Please help me. I can't get the problem to display
The output shows
PROBLEM NUMBER 1
Answer:0
Correct....
PROBLEM NUMBER 2
Answer:1
Wrong....
It must show:
PROBLEM NUMBER 1
10 + 11 = ?
Answer: 21
Correct...*/
import java.util.Random;
import java.util.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.lang.Math;
public class MathIsSuperFun1{
Scanner input = new Scanner(System.in);
int correctAnswers;
int randomNum1;
int randomNum2;
int choice;
int corrrectAnswers, wrongAnswers;
String playerName ="";
int operation;
int userAnswer;
int correctAnswer = 0;
int userRemainder, correctRemainder;
int x = 0;
int temporaryNum1, temporaryNum2;
int range;
int randomNumber;
public static void main (String args[]){
MathIsSuperFun1 lab = new MathIsSuperFun1();
lab.init();
}
public void init(){
getName();
pickDifficulty();
pickOperation();
for(int x = 0; x < 10; x++)
{
System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~");
assignNum();
getProblem();
checkAnswer();
}
}
//GET PLAYER NAME USING PANE
public static String getName(){
String playerName;
playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
System.out.println("Do your best," + playerName + "!");
return playerName;
}
//GET PROBLEM BASED ON OPERATION
public void getProblem(){
switch(operation){
case 1:
System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 + randomNum2;
break;
case 2:
System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n");
correctAnswer = randomNum1-randomNum2;
break;
case 3:
System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n");
correctAnswer = randomNum1*randomNum2;
break;
case 4:
System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n");
correctAnswer = randomNum1/randomNum2;
correctRemainder = randomNum1%randomNum2;
break;
}
System.out.print("Answer: ");
userAnswer = input.nextInt();
if(operation == 4){
System.out.print("Remainder: ");
userRemainder = input.nextInt();
}
return 0;
}
//PICK DIFFICULTY USING DIALOG
public void pickDifficulty(){
int choice = 0;
System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius");
choice = input.nextInt();
}
//PICK OPERATIONS
public void pickOperation(){
int operation = 0;
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
//GET NUMBER RANGE BASED ON DIFFICULTY
public int numberRange(){
int range = 0;
switch(choice){
case 1:
range = 100;
break;
case 2:
range = 1000;
break;
case 3:
range = 10000;
break;
case 4:
range = 100000;
break;
case 5:
range = 1000000;
break;
case 6:
range = 10000000;
break;
}
return range;
}
//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public void correctResponse(){
String responseCorrect = "";
switch (getRandom(5)){
case 1:
responseCorrect = "Correct. Keep up the good work!";
break;
case 2:
responseCorrect = "Correct. Keep aiming higher!";
break;
case 3:
responseCorrect = "Correct. Well done!";
break;
case 4:
responseCorrect = "Correct. Nice work!";
break;
case 5:
responseCorrect = "Correct. We're almost there!";
break;
}
System.out.println(responseCorrect);
correctAnswers += 1;
}
//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public String wrongResponse(){
String responseWrong = "";
switch (getRandom(5)){
case 1:
responseWrong = "Wrong. Don't give up!";
break;
case 2:
responseWrong = "Wrong. You can do it!";
break;
case 3:
responseWrong = "Wrong. Try again puny human!";
break;
case 4:
responseWrong = "Wrong. You must be really weak at math!";
break;
case 5:
responseWrong = "Wrong. I pity you!";
break;
}
System.out.println(responseWrong);
System.out.println("The correct answer is:" + correctAnswer);
if(operation == 4)
System.out.println("Correct Remainder: " + correctRemainder);
return responseWrong;
}
public void checkAnswer(){
if(operation != 4 && userAnswer == correctAnswer){
correctResponse();
}
else if(operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder){
correctResponse();
}
else{
wrongResponse();
}
}
public void assignNum(){
int temporaryNum1 = getRandom(numberRange());
int temporaryNum2 = getRandom(numberRange());
while(operation == 4 && temporaryNum1 == 0){
temporaryNum1 = getRandom(numberRange());
}
while(operation == 4 && temporaryNum2 == 0){
temporaryNum2 = getRandom(numberRange());
}
if(temporaryNum1 > temporaryNum2)
{
randomNum1 = temporaryNum1;
randomNum2 = temporaryNum2;
}
else
{
randomNum1 = temporaryNum2;
randomNum2 = temporaryNum1;
}
}
public int getRandom(int range){
randomNumber = (int)Math.floor((Math.random()*range)+1);
return randomNumber;
}
}
The reason your questions are not outputting is simple.
public void pickOperation(){
int operation = 0;
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
You are creating a local variable, operation, and assigning the input value to it. This means that the field, operation, is never set, so when it comes to the switch statement in your getProblem method, it doesn't output anything because it doesn't match the switch statement.
To fix this, simply remove the int operation = 0; declaration.
Edit
Just noticed the same problem with your pickDifficulty method. I would strongly recommend you have a look at this tutorial on scope in Java.
Explanation of your Problem
Okay. So let's look at your code:
public void pickOperation(){
int operation = 0;
// Declare an int, called 'operation'.
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
// Set the newly declared value to an int from the keyboard.
operation = input.nextInt();
}
As soon as this method is finished, the value inside operation is destroyed. The reason why it isn't staying in your field int operation, is because you declared a more local operation variable. By removing the int operation = 0; at the start of this method, you force the JVM to look for the next available variable named operation in your class, which is in the field. That's why, when you remove the first assertion statement, your value for operation will stick around.
the problem is : int operation = 0; you need to take the value of the operation in operation variable which you create it in Main
Try this :
import java.util.Random;
import java.util.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.lang.Math;
public class Main {
Scanner input = new Scanner(System.in);
int correctAnswers;
int randomNum1;
int randomNum2;
int choice;
int corrrectAnswers, wrongAnswers;
String playerName = "";
int operation;
int userAnswer;
int correctAnswer = 0;
int userRemainder, correctRemainder;
int x = 0;
int temporaryNum1, temporaryNum2;
int range;
int randomNumber;
public static void main(String args[]) {
Main lab = new Main();
lab.init();
}
public void init() {
getName();
pickDifficulty();
pickOperation();
for (int x = 0; x < 10; x++) {
System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~");
assignNum();
getProblem();
getAnswers();
checkAnswer();
}
}
//GET PLAYER NAME USING PANE
public static String getName() {
String playerName;
playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
System.out.println("Do your best," + playerName + "!");
return playerName;
}
//GET PROBLEM BASED ON OPERATION
public void getProblem() {
switch (operation) {
case 1:
System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 + randomNum2;
break;
case 2:
System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 - randomNum2;
break;
case 3:
System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 * randomNum2;
break;
case 4:
System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 / randomNum2;
correctRemainder = randomNum1 % randomNum2;
break;
}
System.out.print("Answer: ");
userAnswer = input.nextInt();
if (operation == 4) {
System.out.print("Remainder: ");
userRemainder = input.nextInt();
}
// return 0;
}
//PICK DIFFICULTY USING DIALOG
public void pickDifficulty() {
System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius");
choice = input.nextInt();
}
//PICK OPERATIONS
public void pickOperation() {
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
//GET NUMBER RANGE BASED ON DIFFICULTY
public int numberRange() {
int range = 0;
switch (choice) {
case 1:
range = 100;
break;
case 2:
range = 1000;
break;
case 3:
range = 10000;
break;
case 4:
range = 100000;
break;
case 5:
range = 1000000;
break;
case 6:
range = 10000000;
break;
}
return range;
}
//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public void correctResponse() {
String responseCorrect = "";
switch (getRandom(5)) {
case 1:
responseCorrect = "Correct. Keep up the good work!";
break;
case 2:
responseCorrect = "Correct. Keep aiming higher!";
break;
case 3:
responseCorrect = "Correct. Well done!";
break;
case 4:
responseCorrect = "Correct. Nice work!";
break;
case 5:
responseCorrect = "Correct. We're almost there!";
break;
}
System.out.println(responseCorrect);
correctAnswers += 1;
}
//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public String wrongResponse() {
String responseWrong = "";
switch (getRandom(5)) {
case 1:
responseWrong = "Wrong. Don't give up!";
break;
case 2:
responseWrong = "Wrong. You can do it!";
break;
case 3:
responseWrong = "Wrong. Try again puny human!";
break;
case 4:
responseWrong = "Wrong. You must be really weak at math!";
break;
case 5:
responseWrong = "Wrong. I pity you!";
break;
}
System.out.println(responseWrong);
System.out.println("The correct answer is:" + correctAnswer);
if (operation == 4) {
System.out.println("Correct Remainder: " + correctRemainder);
}
return responseWrong;
}
public void checkAnswer() {
if (operation != 4 && userAnswer == correctAnswer) {
correctResponse();
} else if (operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder) {
correctResponse();
} else {
wrongResponse();
}
}
public void assignNum() {
int temporaryNum1 = getRandom(numberRange());
int temporaryNum2 = getRandom(numberRange());
while (operation == 4 && temporaryNum1 == 0) {
temporaryNum1 = getRandom(numberRange());
}
while (operation == 4 && temporaryNum2 == 0) {
temporaryNum2 = getRandom(numberRange());
}
if (temporaryNum1 > temporaryNum2) {
randomNum1 = temporaryNum1;
randomNum2 = temporaryNum2;
} else {
randomNum1 = temporaryNum2;
randomNum2 = temporaryNum1;
}
}
public int getRandom(int range) {
randomNumber = (int) Math.floor((Math.random() * range) + 1);
return randomNumber;
}
private void getAnswers() {
////////////////Not yet implemented";
}
}

Maths operators

I have this method which will generate a random maths expression solve it and output the answer to a variable:
public int Nov2()
{
char[] ops = new char[] {'+', '-', '*', '/'};
int i = rand.nextInt(4-0) + 0;
char op1 = ops[i];
int novnum1 = rand.nextInt(101-1) + 1;
int novnum2 = rand.nextInt(101-1) + 1;
int nov2result = 0;
switch(op1) {
case '+': nov2result = novnum1 + novnum2; break;
case '-': nov2result = novnum1 - novnum2; break;
case '*': nov2result = novnum1 * novnum2; break;
case '/': nov2result = novnum1 / novnum2; break;
}
String nov2Exp = novnum1 + " " + op1 + " " + novnum2 + " = ";
Nov2resstor = nov2result;
setContentView(R.layout.gameview);
TextView display = (TextView) findViewById(R.id.exp);
display.setText(nov2Exp);
return nov2result;
}
How would i use the same sort of thing for expressions with more than two terms without having to write really complex if statements like this in my next method:
public int Eas3()
{
char[] ops = new char[] {'+', '-', '*', '/'};
int i = rand.nextInt(4-0) + 0;
char op1 = ops[i];
i = rand.nextInt(4-0) + 0;
char op2 = ops[i];
int easnum1 = rand.nextInt(101-1) + 1;
int easnum2 = rand.nextInt(101-1) + 1;
int easnum3 = rand.nextInt(101-1) + 1;
int eas3result = 0;
if (op1 == '+' && op2 == '+')
{
eas3result = ((easnum1 + easnum2) + easnum3);
}
else if (op1 == '+' && op2 == '-')
{
eas3result = ((easnum1 + easnum2) - easnum3);
}
else if (op1 == '+' && op2 == '*')
{
eas3result = ((easnum1 + easnum2) * easnum3);
}
else if (op1 == '+' && op2 == '-')
{
eas3result = ((easnum1 + easnum2) - easnum3);
}
.../
I have methods which do this for 2,3,4,5 and 6 so my if statements would become very large using this method.
Any ideas?
you can use the built-in Javascript engine.
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Test
{
public static void main(String[] args) throws Exception
{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}
Yes, another way to do it is to write Command objects:
public interface Command<V> {
V execute(Object ... args);
}
You'll write an object that implements this interface:
public class AdditionCommand implements Command<Double> {
public Double execute(Object ... args) {
Double x = (Double)args[0];
Double y = (Double)args[1];
return x+y;
}
}
Now you can look up in a Map using the operator:
Map<String, Command> opsLookup = new HashMap<String, Command>() {{
opsLookup.put("+", new AddCommand<Number>());
opsLookup.put("-", new SubtractCommand<Number>());
}};
No need for a switch.
Check out this MathEval class I found online It will evaluate a String that represents an equation for you.
mySolver = new MathEval();
double answer = mySolver.evaluate(equation);
What you're looking for is called the composite pattern. You define an abstract Expression base class and derive it.
The classes must implement an evaluate() method which returns the result.
One sub class will be the constant which return it's value, another one would be a binary expression like plus, minus, etc. The evaluate() method will add/subtract/etc the result of the evaluated sub-expressions.
You can then build arbitrary expressions out of other expressions and then evaluate it without using one if condition.
How about using recursion:
int num(int numberOfOperands, int current){
if(numberOfOperands<=0) return current;
switch(rand.nextInt(4)){
case 0: return num(numberOfOperands-1, current + (rand.nextInt(100)+1)); break;
case 1: return num(numberOfOperands-1, current - (rand.nextInt(100)+1)); break;
case 2: return num(numberOfOperands-1, current * (rand.nextInt(100)+1)); break;
case 3: return num(numberOfOperands-1, current / (rand.nextInt(100)+1)); break;
}
}
int num(int numberOfOperands) throws Exception{
if(numberOfOperands <=0)
throw new Exception("invalid number of operands: "+numberOfOperands);
return num(numberOfOperands, rand.nextInt(100)+1);
}
This would, of course, ignore precedence of operations.
You could make a string with the variables you are using like this:
String temp = "(" + easnum1 + op1 + easnum2 + ")" + op2 + easnum3;
after that you can use the ScriptEngineManager class to use javascript as the engine so you can use the eval method.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(temp);
this method does the calculations and returns the result.
Hope this helps.
I would use an array for the values easnum[], an array for the operands op[] and an array with intermediate values.
Something along the following lines
for(...)
{
if(op[i]=='+') easintermediat[i+1] = easintermediate[i] + easnum[i]
...
}
If you've got n operations on n+1 numbers, and you do the first one, then you're left with n-1 operations on n numbers. You can use this as the basis for a loop that will process any number of items easily.
int operate(int[] numbers, int[] operations) {
if (numbers.length < 1 || numbers.length != operations.length + 1) {
throw new IllegalArgumentException();
}
int result = numbers[0];
for (int i = 0; i < operations.length; ++i) {
result = operate(operations[i], result, numbers[i+1]);
// where operate() is your switch statement
}
return result;
}
Try this:
public int Eas3()
{
char[] ops = new char[] {'+', '-', '*', '/'};
int i = rand.nextInt(4-0) + 0;
char op1 = ops[i];
i = rand.nextInt(4-0) + 0;
char op2 = ops[i];
int easnum1 = rand.nextInt(101-1) + 1;
int easnum2 = rand.nextInt(101-1) + 1;
int easnum3 = rand.nextInt(101-1) + 1;
int eas3result = 0;
if (op1 == '+')
{
switch(op2)
{
case '+': eas3result=((easnum1 + easnum2) + easnum3); break;
case '-': eas3result=((easnum1 - easnum2) - easnum3); break;
case '*': eas3result=((easnum1 * easnum2) * easnum3); break;
case '/': eas3result=((easnum1 / easnum2) / easnum3); break;
}
}
..../
}
or even you can put outer IF in SWITCH like the following
public int Eas3()
{
char[] ops = new char[] {'+', '-', '*', '/'};
int i = rand.nextInt(4-0) + 0;
char op1 = ops[i];
i = rand.nextInt(4-0) + 0;
char op2 = ops[i];
int easnum1 = rand.nextInt(101-1) + 1;
int easnum2 = rand.nextInt(101-1) + 1;
int easnum3 = rand.nextInt(101-1) + 1;
int eas3result = 0;
int tempResult=0;
switch(op1)
{
case '+': tempResult=(easnum1 + easnum2); break;
case '-': tempResult=(easnum1 + easnum2) ; break;
case '*': tempResult=(easnum1 + easnum2) ; break;
case '/': tempResult=(easnum1 + easnum2) ; break;
}
switch(op2)
{
case '+': eas3result=(tempResult + easnum3); break;
case '-': eas3result=(tempResult - easnum3); break;
case '*': eas3result=(tempResult * easnum3); break;
case '/': eas3result=(tempResult / easnum3); break;
}
}

Categories