How Does Prefix And Postfix Change The Answer
Mar 26, 2015Will the postfix x++ and the prefix --y change the answer for this question?
If x has the value 10 and so does y, then what is the value of (x ++) * (-- y)?
Answer: 11*9
Will the postfix x++ and the prefix --y change the answer for this question?
If x has the value 10 and so does y, then what is the value of (x ++) * (-- y)?
Answer: 11*9
So my code works perfectly when I input (a+(c-d) and i get ab+cd- for postfix and *+ab-cd for prefix. However when I input a+b+c for infix i receive abc++ postfix and +a+bc prefix when its supposed to be ab+c+ postfix and ++a b c prefix. So my issue is that any infix input with parenthesis, it converts them correctly, however without parenthesis it does not convert correctly.
import java.util.*;
public class stack {
public static char[] convertToPostfix(char[] infixEx) {
Stack<Character> operatorStack = new Stack<Character>();
char[] postfix = new char[infixEx.length];
int index = 0;
[Code] .....
I was looking at this for loop
for ( int i = 0; i < 10; i++){/*print i*/}
It prints 0..9 - fine.
I then looked at this loop
for ( int i = 0; i < 10; ++i){/*print i*/}
It prints 0..9 - why? From what I understand ++i is a prefix, therefore by the time the for loop condition is checked, shoudln't i start with 1? So 1..9?
If I did this test in a print statement so:
int i=0;
System.out.print(++i) //this prints 1,
so why doesn't this apply in the for loop?
I had a list of some numbers having a prefix A like A12 , A55, A76 ,A111 ,A888, A88 ,A880 A111 , A11,A1
I need to sort this list so the result would be A1,A11,A12,A55,A76....
How to do this. Can I use arrays.sort method to achieve the same or any other way.
If I have a comboBox full of id's - is it possible that when I choose said id (click it) it will then transfer over into my textField where I can use that as a prefix for my filename ...
(The file can be created just by having a name in the text field it doesn't need to already exists) ...
I am having a problem with infix and prefix ... Here is the code
import java.util.StringTokenizer;
import static java.lang.Math.pow;
import java.util.Stack;
public class Calculator {
static int precedence(char op) {
switch(op) {
[Code] ....
it says in line 72
possible loss of precision
required: int
found: double
the pow only takes int values?not doubles?how do i fix it?
Case study : infix to postfix conversion, i don't really know how i could make codes, I can understand what is the meaning of infix and postfix but when it comes of making codes i really have a hard time with it.
View Replies View RelatedSo I am working on a PostFix calculator that is used in command line for a class project, and I am having a little trouble on developing a memory for it. I have been told to create a hashMap and I have researched it and understand the basics of it. I have the calculating method working, but what I am having trouble trying to implement a way for the user to declare variables. For example this what the user should be able to do:
> a = 3 5 + 1 -
7
> bee = a 3 *
21
> a bee +
28
[code]....
I just need to be able to save the answers as a variable the User names, such as the example and let the user be able to use the variables later.
after i am done calculating everything from numbers stack, i pop the last number and return it... my question is how can i catch an exception if the size of my numbers stack is greater than 1;
public static String evaluate(String input) {
char[] a = input.toCharArray();
if (input.isEmpty())
return "No input";
else if (input.equals(" "))
return "No input";
else if (input.equals(" "))
[code]....
I need to convert infix To Postfix but have a few errors.
Error msg:
PostFix:
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at assignment4.infixToPostfix.evaluatePostfix(infixTo Postfix.java:129)
at assignment4.infixToPostfix.main(infixToPostfix.jav a:19)
NB:
129 >>return (double) s1.pop(); AND 19>>>double ans = evaluatePostfix(postfixStr);
Deadline = less than 1 hour
My question is to evaluate a Postfix notation entered from keyboard. I have no errors in my code but it prints only :
Exception in thread "main"
java.util.NoSuchElementException
at ArrayStack.pop(PostFixEvaluation.java:72)
at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107)
at PostFixEvaluation.main(PostFixEvaluation.java:140)
I tried many values but it prints the same exception all the time.
Here is my code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
interface Stack<E> {
[Code] ....
My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.
The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.
My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.
Also, my verify method always returns false and I can't pinpoint why.
Here's my current code. Some things are commented out for debugging purposes.
import java.util.Scanner;
/**
* Simple calculator that reads infix expressions and evaluates them.
*/
public class Calculator
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
[Code] .....
package hw9;
import java.util.Scanner;
import java.util.Stack;
public class stack {
public static Integer evaluate(String expression) {
Scanner scanner = new Scanner(expression);
Stack <Integer> operands = new Stack<Integer>();
[Code] ....
When I input my expression which has spaces between characters e.g.:10 2 8 * + 3 -, it worked; when I put expression which may not add space between each char e.g.: 3 14+2*7/, the error showed:
Enter a postfix expression: 3 14+2*7/
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at java.util.Stack.pop(Stack.java:84)
at hw9.stack.evaluate(stack.java:22)
at hw9.stack.main(stack.java:45)
So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).
To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.
I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.
The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...
public class Operator implements Comparable<Operator>
{
public char operator; // operator
privateint inputPrecedence; // input precedence of operator in the range [0, 5]
privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]
[Code] ....
So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.
My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time
If I for example choose 8 as "multiplikationstabell" and 4 as "faktor" the whole code works except that "svar" gets printed as 8 in every turn. Why? "Faktor" gets added with 1 every time but "svar" stays at 8.......
import java.util.Scanner;
public class Multiplikationstabell {
public static void main(String args[]){
Scanner Heltal = new Scanner(System.in);
[Code] .....
I have tried as much as I can to place code to give me only two decimals in answers but cannot get it to work not sure if placement or syntax.This is code so far and it works but gives answers to 8 decimals.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
[code]...
I am new to programing and I am making a dice game where I have to roll two dice and add the total together for each dice (2, 3, 4 ect.) I have all the program finished except one thing.
How would I use the * character that shows the total percentage each number was rolled. Each * will represent 1% of the total rolls. Instead of saying there were ( 4 ) 2's rolled i have to put ( **** ) 2's rolled.
2: ***
3: ******
4: *********
I am creating a body mass index calculator and i was wondering how i could make its so that the program resets itself once the BMI has been found....
My code is below...
package bmiCalculatorSource;
import java.util.Scanner;
public class BMICalculator {
public static void main(String[] args) {
final double KilogramsPerPound = 0.453;
final double MetersPerInch = 0.026;
[Code] ....
I would like to put the answer of JCheckBox inside of a JTextArea. How to call the answer of JCheckBox if its check the response should be Yes if its unchecked the response should be No, and the yes/no answer should be appear inside JTextArea. But my problem is inside the JTextArea the answer from a JTextField and JComboBox is also there, and they should be appear in a parallel. I attached the physical appearance of the GUI.
Attached image(s)
I want to design an open-answer quiz game using Java. Basically, the plan is to design a quiz where the name of a medicine is displayed as "the question" and the player will need to input the unique code (as "the answer") of the medicine in an open box (all the codes are two characters). The list has about 204 named medicine (with potentially more to be included at a later stage). The questions will not appear in the same order for each session restarted; they will appear randomly.
The player will have 60 seconds, and for each correct answer, will score 1 point and add 2 seconds to the timer. The player will also have 6 "lives", and for each incorrect answer, the player will lose a life, with no effect on the time.
The idea of the game is that the player memorise as many of these medicine codes as possible.
I am writing a simple learning program that does basic math. Everything is working as needed except for when it comes to division. I need to know if there is a way to format the users input to two decimal places and that when the program checks the users answer against the division, it gives it a yay or nay. Right now, it wants the answer out to 12 decimal places.
So for 5/3
I would like it to accept the answer 1.67
not 1.666666666667
Is there a way to do this?
I am having trouble figuring out where to put %.2f in my code. I need it to get my answers for surfaceArea and Volume to be rounded to two decimal points. I have everything completed but wherever i put it, it seems to not work.
System.out.println("This program will compute the volume and surface area of a rectangular prism.");
String output;
double length;
double width;
double height;
double volume = 0;
double surfaceArea = 0;
System.out.printf("%.2f", surfaceArea);
[code]....
I want to design an open-answer quiz game using Java. Basically, the plan is to design a quiz where the name of a medicine is displayed as "the question" and the player will need to input the unique code (as "the answer") of the medicine in an open box (all the codes are two characters). The list has about 204 named medicine (with potentially more to be included at a later stage). The questions will not appear in the same order for each session restarted; they will appear randomly.
The player will have 60 seconds, and for each correct answer, will score 1 point and add 2 seconds to the timer. The player will also have 6 "lives", and for each incorrect answer, the player will lose a life, with no effect on the time.The idea of the game is to support the player memorise as many of these medicine codes as possible. How this can be achieved from total grassroots.
I want make a math equation game. usually math game use equation for the question and we answer that equation. ex : what is the result of 2*5 / 2+2/ 2^2 etc. and we answering that. but i want to make it different so i want to make the equation answer as the question and the equation as the answer of the game. Ex : the question is 54 so we can answer it with 9*6 or 24+30. and this is my java code
package coba;
import java.util.Scanner;
public class Coba {
public static void main(String[] args) {
double angka = Math.random()*70;
[Code] ....
but when i run it the error code show like this : Quote
package FracCalc;
import java.util.Scanner;
public class FracCalc {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n1 = 0; //n1 = first numerator user have to enter
int d1 = 0; //d1 = first denominator user have to enter
int n2 = 0; //n2 = second numerator user have to enter
int d2 = 0; //d2 = second denominator user have to enter
int numeratorAnswer = 0; //calculates numerator of an equation
int denominatorAnswer = 0; //calculates denominator of an equation
[code]...
I mean its not that important but i figure it will be better if I could print out the answer both in fraction format and decimal format. while my answer prints in fraction just fine how do i show it in decimal?
System.out.println("");
System.out.print("And ");
System.out.print("answer in Decimal is: ");
decimalAnswer = ((double)numeratorAnswer/denominatorAnswer);
System.out.println((float)decimalAnswer);
Code in the book is written exactly like this:
import java.util.Scanner;
public class AdditionQuiz {
public static void main (String[] args){
//Generate random two integers using utility System.currentTimeMillis
int n1 = (int)(System.currentTimeMillis() % 10);
int n2 = (int)(System.currentTimeMillis() / 7 % 10);
[Code] ....
however the true/false result can not be printed due to the "answer" variable...