Java programming In this game 4 cards are randomly picked fr

Java programming.
In this game, 4 cards are randomly picked from 52 cards, and we need to use these 4 cards and addition, subtraction, multiplication, division and parentheses to make a 24. Note each of the 4 card can only be used once. The below lists the face value of each card. \"A\": 1 \"2\": 2 \"3\": 3 \"4\": 4 \"5\": 5 \"6\": 6 \"7\": 7 \"8\": 8 \"9\": 9 \"10\": 10 \"J\": 11 \"Q\": 12 \"K\": 13 For example, if the 4 cards are \"A 2 3 Q\", then we can do (3 - 2 + 1) * 12 = 24 Read the following link for more information about the game. Our job for this assignment to find all the 4-card combinations that the solutions are available. and print out one solution for each of those 4-card combinations. The key to solving the problem is Postfix Notation, which is an unambiguous way of writing an arithmetic expressions without parentheses. It is defined so that \"(exp_1) op (exp_2)\" is a normal fully parenthesized expression whose operations is op, the postfix version of this is \"pexp, pexp, op\", where pexp1 is the postfix version of exp1 and pexp2 is the postfix version of exp2. The postfix version of a single number of variable is just that number or variable. So, for example, the postfix version of \"((5+2)*(8-3))/4\" is \"5.2 + 8.3 - *4 r. First, describe a nonrecursive way of evaluating an expression in postfix notation. Second, for a 4-card combination find all the valid postfix rotations that include the 4 numbers, and also addition, subtraction, multiplication and division operations. Evaluate all of these postfix notations and see whether some of these postfix notations lead to 24. Third, loop through all the possible 4-card combinations, and check whether a combination can be solved using the above technique. If a combination can be solved, print out a solution. Show the solution step by step. For example, tor combination \"A 2 3 Q\", we print A 2 3 Q; 3 - 2 = 1, 1 + 1 = 2, 2 * 12 = 24

Solution

import java.util.Random; public class Twentyfour { double a,b,c,d,e=0,f=0; boolean par=false; public Twentyfour(boolean par){ this.par=par; } public void answer(double a, double b, double c, double d) { this.a=a; this.b=b; this.c=c; this.d=d; for (int i=0;i< 4; i++){ for (int v=0;v< 3; v++){ for (int x=0; x<2; x++){ if ((((a+b)-c)*d)==24||(((a+b)-c)*d)==-24) System.out.print(a+\"+\"+b+\"-\"+c+\"*\"+d); else if ((((a+b)-c)/d)==24||(((a+b)-c)/d)==-24) System.out.print(a+\"+\"+b+\"-\"+c+\"/\"+d); else if ((((a+b)/c)-d)==24||(((a+b)/c)-d)==-24) System.out.println (a+\"+\"+b+\"/\"+c+\"-\"+d); else if ((((a+b)/c)*d)==24||(((a+b)/c)*d)==-24) System.out.println (a+\"+\"+b+\"/\"+c+\"*\"+d); else if ((((a+b)*c)/d)==24||(((a+b)*c)/d)==-24) System.out.println (a+\"+\"+b+\"*\"+c+\"/\"+d); else if ((((a+b)*c)-d)==24||(((a+b)*c)-d)==-24) System.out.println (a+\"+\"+b+\"*\"+c+\"-\"+d); else if ((((a-b)+c)*d)==24||(((a-b)+c)*d)==-24) System.out.println (a+\"-\"+b+\"+\"+c+\"*\"+d); else if ((((a-b)+c)/d)==24||(((a-b)+c)/d)==-24) System.out.println (a+\"-\"+b+\"+\"+c+\"/\"+d); else if ((((a-b)/c)+d)==24||(((a-b)/c)+d)==-24) System.out.println (a+\"-\"+b+\"/\"+c+\"+\"+d); else if ((((a-b)/c)*d)==24||(((a-b)/c)*d)==-24) System.out.println (a+\"-\"+b+\"/\"+c+\"*\"+d); else if ((((a-b)*c)/d)==24||(((a-b)*c)/d)==-24) System.out.println (a+\"-\"+b+\"*\"+c+\"/\"+d); else if ((((a-b)*c)+d)==24||(((a-b)*c)+d)==-24) System.out.println (a+\"-\"+b+\"*\"+c+\"+\"+d); else if ((((a*b)+c)/d)==24||(((a*b)+c)/d)==-24) System.out.println (a+\"*\"+b+\"+\"+c+\"/\"+d); else if ((((a*b)+c)-d)==24||(((a*b)+c)-d)==-24) System.out.println (a+\"*\"+b+\"+\"+c+\"-\"+d); else if ((((a*b)-c)/d)==24||(((a*b)-c)/d)==-24) System.out.println (a+\"*\"+b+\"-\"+c+\"/\"+d); else if ((((a*b)-c)+d)==24||(((a*b)+c)+d)==-24) System.out.println (a+\"*\"+b+\"-\"+c+\"+\"+d); else if ((((a*b)/c)+d)==24||(((a*b)/c)+d)==-24) System.out.println (a+\"*\"+b+\"/\"+c+\"+\"+d); else if ((((a*b)/c)-d)==24||(((a*b)/c)-d)==-24) System.out.println (a+\"*\"+b+\"/\"+c+\"-\"+d); else if ((((a/b)+c)*d)==24||(((a/b)+c)*d)==-24) System.out.println (a+\"/\"+b+\"+\"+c+\"*\"+d); else if ((((a/b)+c)-d)==24||(((a/b)+c)-d)==-24) System.out.println (a+\"/\"+b+\"+\"+c+\"-\"+d); else if ((((a/b)-c)+d)==24||(((a/b)-c)+d)==-24) System.out.println (a+\"/\"+b+\"-\"+c+\"+\"+d); else if ((((a/b)-c)*d)==24||(((a/b)-c)*d)==-24) System.out.println (a+\"/\"+b+\"-\"+c+\"*\"+d); else if ((((a/b)*c)-d)==24||(((a/b)*c)-d)==-24) System.out.println (a+\"/\"+b+\"*\"+c+\"-\"+d); else if ((((a/b)*c)+d)==24||(((a/b)*c)+d)==-24) System.out.println (a+\"/\"+b+\"*\"+c+\"+\"+d); if (par){ f=c+d; if (((a/b)*f)==24||(((a/b)*f))==-24) System.out.println (a+\"/\"+b+\"*(\"+c+\"+\"+d+\")\"); else if (((a/b)-f)==24||(((a/b)-f)==-24)) System.out.println (a+\"/\"+b+\"-(\"+c+\"+\"+d+\")\"); else if (((a*b)-f)==24||(((a*b)-f)==-24)) System.out.println (a+\"*\"+b+\"-(\"+c+\"+\"+d+\")\"); else if (((a*b)/f)==24||(((a*b)/f)==-24)) System.out.println (a+\"*\"+b+\"/(\"+c+\"+\"+d+\")\"); else if (((a-b)*f)==24||(((a-b)*f)==-24)) System.out.println (a+\"-\"+b+\"*(\"+c+\"+\"+d+\")\"); else if (((a-b)/f)==24||(((a-b)/f)==-24)) System.out.println (a+\"-\"+b+\"/(\"+c+\"+\"+d+\")\"); f=c-d; if (((a/b)*f)==24||(((a/b)*f))==-24) System.out.println (a+\"/\"+b+\"*(\"+c+\"-\"+d+\")\"); else if (((a/b)+f)==24||(((a/b)+f)==-24)) System.out.println (a+\"/\"+b+\"+(\"+c+\"-\"+d+\")\"); else if (((a*b)+f)==24||(((a*b)+f)==-24)) System.out.println (a+\"*\"+b+\"+(\"+c+\"-\"+d+\")\"); else if (((a*b)/f)==24||(((a*b)/f)==-24)) System.out.println (a+\"*\"+b+\"/(\"+c+\"-\"+d+\")\"); else if (((a+b)*f)==24||(((a+b)*f)==-24)) System.out.println (a+\"+\"+b+\"*(\"+c+\"-\"+d+\")\"); else if (((a+b)/f)==24||(((a+b)/f)==-24)) System.out.println (a+\"+\"+b+\"/(\"+c+\"-\"+d+\")\"); f=c*d; if (((a/b)-f)==24||(((a/b)-f))==-24) System.out.println (a+\"/\"+b+\"*(\"+c+\"*\"+d+\")\"); else if (((a/b)+f)==24||(((a/b)+f)==-24)) System.out.println (a+\"/\"+b+\"+(\"+c+\"*\"+d+\")\"); else if (((a-b)+f)==24||(((a-b)+f)==-24)) System.out.println (a+\"-\"+b+\"+(\"+c+\"*\"+d+\")\"); else if (((a-b)/f)==24||(((a-b)/f)==-24)) System.out.println (a+\"-\"+b+\"/(\"+c+\"*\"+d+\")\"); else if (((a+b)-f)==24||(((a+b)-f)==-24)) System.out.println (a+\"+\"+b+\"-(\"+c+\"*\"+d+\")\"); else if (((a+b)/f)==24||(((a+b)/f)==-24)) System.out.println (a+\"+\"+b+\"/(\"+c+\"*\"+d+\")\"); f=c/d; if (((a-b)*f)==24||(((a-b)*f))==-24) System.out.println (a+\"-\"+b+\"*(\"+c+\"/\"+d+\")\"); else if (((a-b)+f)==24||(((a-b)+f)==-24)) System.out.println (a+\"-\"+b+\"+(\"+c+\"/\"+d+\")\"); else if (((a*b)+f)==24||(((a*b)+f)==-24)) System.out.println (a+\"*\"+b+\"+(\"+c+\"/\"+d+\")\"); else if (((a*b)-f)==24||(((a*b)-f)==-24)) System.out.println (a+\"*\"+b+\"-(\"+c+\"/\"+d+\")\"); else if (((a+b)*f)==24||(((a+b)*f)==-24)) System.out.println (a+\"+\"+b+\"*(\"+c+\"/\"+d+\")\"); else if (((a+b)-f)==24||(((a+b)-f)==-24)) System.out.println (a+\"+\"+b+\"-(\"+c+\"/\"+d+\")\"); f=b*c; if (((a-f)/d)==24||(((a-f)/d))==-24) System.out.println (a+\"-(\"+b+\"*\"+c+\")/\"+d); else if (((a-f)+d)==24||(((a-f)+d)==-24)) System.out.println (a+\"-(\"+b+\"*\"+c+\")+\"+d); else if (((a/f)+d)==24||(((a/f)+d)==-24)) System.out.println (a+\"/(\"+b+\"*\"+c+\")+\"+d); else if (((a/f)-d)==24||(((a/f)-d)==-24)) System.out.println (a+\"/(\"+b+\"*\"+c+\")-\"+d); else if (((a+f)/d)==24||(((a+f)/d)==-24)) System.out.println (a+\"+(\"+b+\"*\"+c+\")/\"+d); else if (((a+f)-d)==24||(((a+f)-d)==-24)) System.out.println (a+\"+(\"+b+\"*\"+c+\")-\"+d); f=b-c; if (((a*f)/d)==24||(((a*f)/d))==-24) System.out.println (a+\"*(\"+b+\"-\"+c+\")/\"+d); else if (((a*f)+d)==24||(((a*f)+d)==-24)) System.out.println (a+\"*(\"+b+\"-\"+c+\")+\"+d); else if (((a/f)+d)==24||(((a/f)+d)==-24)) System.out.println (a+\"/(\"+b+\"-\"+c+\")+\"+d); else if (((a/f)*d)==24||(((a/f)*d)==-24)) System.out.println (a+\"/(\"+b+\"-\"+c+\")*\"+d); f=b/c; if (((a-f)*d)==24||(((a-f)*d))==-24) System.out.println (a+\"-(\"+b+\"/\"+c+\")*\"+d); else if (((a-f)+d)==24||(((a-f)+d)==-24)) System.out.println (a+\"-(\"+b+\"/\"+c+\")+\"+d); else if (((a*f)+d)==24||(((a*f)+d)==-24)) System.out.println (a+\"*(\"+b+\"/\"+c+\")+\"+d); else if (((a*f)-d)==24||(((a*f)-d)==-24)) System.out.println (a+\"*(\"+b+\"/\"+c+\")-\"+d); else if (((a+f)*d)==24||(((a+f)*d)==-24)) System.out.println (a+\"+(\"+b+\"/\"+c+\")*\"+d); else if (((a+f)-d)==24||(((a+f)-d)==-24)) System.out.println (a+\"+(\"+b+\"/\"+c+\")-\"+d); f=b+c; if (((a*f)/d)==24||(((a*f)/d))==-24) System.out.println (a+\"*(\"+b+\"+\"+c+\")/\"+d); else if (((a*f)-d)==24||(((a*f)-d)==-24)) System.out.println (a+\"*(\"+b+\"+\"+c+\")-\"+d); else if (((a/f)-d)==24||(((a/f)-d)==-24)) System.out.println (a+\"/(\"+b+\"+\"+c+\")-\"+d); else if (((a/f)*d)==24||(((a/f)*d)==-24)) System.out.println (a+\"/(\"+b+\"+\"+c+\")*\"+d); } e=c; c=d; d=e; } e=b; b=c; c=d; d=e; } e=a; a=b; b=c; c=d; d=e; } } public double[] question(){ while (true){ Random random = new Random(); int aa = random.nextInt(30); double a=(double)aa; int bb= random.nextInt(30); double b=(double)bb; int cc= random.nextInt(30); double c=(double)cc; int dd= random.nextInt(30); double d=(double)dd; double e =0; double[] numbers = new double[4]; numbers[0]=a; numbers[1]=b; numbers[2]=c; numbers[3]=d; for(int p=0;p<4;p++){ if (numbers[p]==0){ numbers[p]=random.nextInt(30); } // Gives numbers that are negative if (random.nextInt(2)==0){ numbers[p]=-numbers[p]; } } for (int i=0;i< 4; i++){ for (int v=0;v< 3; v++){ for (int x=0; x<2; x++){ if ((((a+b)-c)*d)==24||(((a+b)-c)*d)==-24) return numbers; else if ((((a+b)-c)/d)==24||(((a+b)-c)/d)==-24) return numbers; else if ((((a+b)/c)-d)==24||(((a+b)/c)-d)==-24) return numbers; else if ((((a+b)/c)*d)==24||(((a+b)/c)*d)==-24) return numbers; else if ((((a+b)*c)/d)==24||(((a+b)*c)/d)==-24) return numbers; else if ((((a+b)*c)-d)==24||(((a+b)*c)-d)==-24) return numbers; else if ((((a-b)+c)*d)==24||(((a-b)+c)*d)==-24) return numbers; else if ((((a-b)+c)/d)==24||(((a-b)+c)/d)==-24) return numbers; else if ((((a-b)/c)+d)==24||(((a-b)/c)+d)==-24) return numbers; else if ((((a-b)/c)*d)==24||(((a-b)/c)*d)==-24) return numbers; else if ((((a-b)*c)/d)==24||(((a-b)*c)/d)==-24) return numbers; else if ((((a-b)*c)+d)==24||(((a-b)*c)+d)==-24) return numbers; else if ((((a*b)+c)/d)==24||(((a*b)+c)/d)==-24) return numbers; else if ((((a*b)+c)-d)==24||(((a*b)+c)-d)==-24) return numbers; else if ((((a*b)-c)/d)==24||(((a*b)-c)/d)==-24) return numbers; else if ((((a*b)-c)+d)==24||(((a*b)+c)+d)==-24) return numbers; else if ((((a*b)/c)+d)==24||(((a*b)/c)+d)==-24) return numbers; else if ((((a*b)/c)-d)==24||(((a*b)/c)-d)==-24) return numbers; else if ((((a/b)+c)*d)==24||(((a/b)+c)*d)==-24) return numbers; else if ((((a/b)+c)-d)==24||(((a/b)+c)-d)==-24) return numbers; else if ((((a/b)-c)+d)==24||(((a/b)-c)+d)==-24) return numbers; else if ((((a/b)-c)*d)==24||(((a/b)-c)*d)==-24) return numbers; else if ((((a/b)*c)-d)==24||(((a/b)*c)-d)==-24) return numbers; else if ((((a/b)*c)+d)==24||(((a/b)*c)+d)==-24) return numbers; if (par){ f=c+d; if (((a/b)*f)==24||(((a/b)*f))==-24) return numbers; else if (((a/b)-f)==24||(((a/b)-f)==-24)) return numbers; else if (((a*b)-f)==24||(((a*b)-f)==-24)) return numbers; else if (((a*b)/f)==24||(((a*b)/f)==-24)) return numbers; else if (((a-b)*f)==24||(((a-b)*f)==-24)) return numbers; else if (((a-b)/f)==24||(((a-b)/f)==-24)) return numbers; f=c-d; if (((a/b)*f)==24||(((a/b)*f))==-24) return numbers; else if (((a/b)+f)==24||(((a/b)+f)==-24)) return numbers; else if (((a*b)+f)==24||(((a*b)+f)==-24)) return numbers; else if (((a*b)/f)==24||(((a*b)/f)==-24)) return numbers; else if (((a+b)*f)==24||(((a+b)*f)==-24)) return numbers; else if (((a+b)/f)==24||(((a+b)/f)==-24)) return numbers; f=c*d; if (((a/b)-f)==24||(((a/b)-f))==-24) return numbers; else if (((a/b)+f)==24||(((a/b)+f)==-24)) return numbers; else if (((a-b)+f)==24||(((a-b)+f)==-24)) return numbers; else if (((a-b)/f)==24||(((a-b)/f)==-24)) return numbers; else if (((a+b)-f)==24||(((a+b)-f)==-24)) return numbers; else if (((a+b)/f)==24||(((a+b)/f)==-24)) return numbers; f=c/d; if (((a-b)*f)==24||(((a-b)*f))==-24) return numbers; else if (((a-b)+f)==24||(((a-b)+f)==-24)) return numbers; else if (((a*b)+f)==24||(((a*b)+f)==-24)) return numbers; else if (((a*b)-f)==24||(((a*b)-f)==-24)) return numbers; else if (((a+b)*f)==24||(((a+b)*f)==-24)) return numbers; else if (((a+b)-f)==24||(((a+b)-f)==-24)) return numbers; f=b*c; if (((a-f)/d)==24||(((a-f)/d))==-24) return numbers; else if (((a-f)+d)==24||(((a-f)+d)==-24)) return numbers; else if (((a/f)+d)==24||(((a/f)+d)==-24)) return numbers; else if (((a/f)-d)==24||(((a/f)-d)==-24)) return numbers; else if (((a+f)/d)==24||(((a+f)/d)==-24)) return numbers; else if (((a+f)-d)==24||(((a+f)-d)==-24)) return numbers; f=b-c; if (((a*f)/d)==24||(((a*f)/d))==-24) return numbers; else if (((a*f)+d)==24||(((a*f)+d)==-24)) return numbers; else if (((a/f)+d)==24||(((a/f)+d)==-24)) return numbers; else if (((a/f)*d)==24||(((a/f)*d)==-24)) return numbers; f=b/c; if (((a-f)*d)==24||(((a-f)*d))==-24) return numbers; else if (((a-f)+d)==24||(((a-f)+d)==-24)) return numbers; else if (((a*f)+d)==24||(((a*f)+d)==-24)) return numbers; else if (((a*f)-d)==24||(((a*f)-d)==-24)) return numbers; else if (((a+f)*d)==24||(((a+f)*d)==-24)) return numbers; else if (((a+f)-d)==24||(((a+f)-d)==-24)) return numbers; f=b+c; if (((a*f)/d)==24||(((a*f)/d))==-24) return numbers; else if (((a*f)-d)==24||(((a*f)-d)==-24)) return numbers; else if (((a/f)-d)==24||(((a/f)-d)==-24)) return numbers; else if (((a/f)*d)==24||(((a/f)*d)==-24)) return numbers; } e=c; c=d; d=e; } e=b; b=c; c=d; d=e; } e=a; a=b; b=c; c=d; d=e; } } } //This runs the game with the numbers created in the question to see if it works public double[] check(){ double l,m,j,p; double[] numbs = new double[4]; numbs= question(); l=numbs[0]; m=numbs[1]; j=numbs[2]; p=numbs[3]; answer(l,m,j,p); return numbs; } }
Java programming. In this game, 4 cards are randomly picked from 52 cards, and we need to use these 4 cards and addition, subtraction, multiplication, division
Java programming. In this game, 4 cards are randomly picked from 52 cards, and we need to use these 4 cards and addition, subtraction, multiplication, division
Java programming. In this game, 4 cards are randomly picked from 52 cards, and we need to use these 4 cards and addition, subtraction, multiplication, division

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site