Changing Infix To Postfix Notation Using Stacks - How To Utilize Hash Maps
Apr 7, 2014
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
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.
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
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;
I am given the task to create a program that evaluates infix expressions using two generic stacks, one operator stack and one value stack.
This is my GenStack.java file:
import java.util.*; public class GenStack<T>{//T is the type parameter private Node top;//top of stack public class Node {//defines each node of stack T value; Node next;
[Code] ....
I'm having trouble with the eval and apply methods. The eval method doesn't appear to pickup ')' characters, like it doesn't even see them.
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.
I'm trying to create contact information as object and another object as parcel then calculate the shipping fee. Problem is I don't have a good understanding in implementing object type to my code. Attached are what I've done and I know that my constructor in my parcel class and contact class are probably wrong too but I don't know what to do with it.
//contact class public class Contact { private String Name; private String Address; private String City; private String State; private double Xco; private double Yco;
I am making a program which will have the user enter their location (by postcode) into the GUI (Swing).
I want to use Google's Directions API [URL] .... to find directions from this user input address to another address. However, I have never gone about using external APIs before and even going as far as installing the necessary files on my system to get started is getting complicated.
How do I go about using this API in my own application? I have tried following the tutorials (e.g. [URL] .....) but I suspect I may even be using the wrong tutorial (I am seeing references saying that the client thing is depreciated).
I'm developing a java swing application in which i need to show the map of some area and allow user to put markers on it. I've goggled a lot on the topic but unfortunately i haven't got any how i can do it.
I came across some ideas like displaying a web browser in swing just like JXBrowser (i can't afford JXBrowser). And I'm also not sure if this way will allow me adding markers.
The final map would have all the values from Map A as a key and the values from Map B as values in the Final Map. Is there a way to do this using Java?
At this point I know how to utilize Google Maps within Android but it always seems to take up the full window, there is an image below which shows what I'm attempting to accomplish (having a box below the Google maps where I can store text i.e. "Hello World"
How do I add box below Google Maps, so to store text i.e. "Hello World"
Code so far:
ActivityMain:
Java Code:
public class MainActivity extends Activity { private GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
I've been trying to learn more about Big O Notation and I've gotten stuck on a few pieces of code. What is the computational complexity for the following pieces of code?
1:
for(int i = n; i > 0; i /= 2) { for(int j = 1; j < n; j *= 2) { for(int k = 0; k < n; k += 2) { // constant number of operations
[Code] .....
5 : Determine the average processing time of the recursive algorithm. (int n) spends one time unit to return a random integer value uniformly distributed in the range [0,n] whereas all other instructions spend a negligibly small time(e.g., T(0) = 0)
int myTest(int n) { if(n <= 0) return 0; else { int i = random(n - 1); return myTest(i) + myTest(n - 1 - i);
6 : Assume array a contains n values, the method randomValue takes a constant number c of computational steps to produce each output value, and that the method goodSort takes n log n computational steps to sort the array.
I've to create a program to get user input in scientific notation. Now i'm confused that how can i check that how user have used the scientific notation. For example there are many ways to write a number in scientific notation. Also a user can either write a number as:
6.7E2 OR 6.7*10^2
How to handle this input and further convert it to double?
So recently I began Data Structures as core subject and the tutorials in this forum are great.
Right now, I seem to have trouble with Big Oh Notation algorithm and what is the mathematical side to it. "f(n) <= c.g(n), for n>=0.
The question I am working on is: Suppose we are maintaining a collection C of elements such that, each time we add a new element to the collection, we copy the contents of C into a new array list of just the right size. What is the running time of adding n elements to an initially empty collection C in this case?
// the MountainBike subclass has // one field public int seatHeight;
// the MountainBike subclass has // one constructor public MountainBike(int startHeight, int startCadence,
[Code] ....
At first, Java Code: public int seatHeight; mh_sh_highlight_all('java'); tells us that seatHeight is NOT a static field (because of the absence of static keyword).
Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in Java Code: seatHeight = newValue; mh_sh_highlight_all('java'); shows that it IS a non-member/static variable.
So 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(" "))
I am trying to create a class (DVD) with an instance variable that references a map, the constructor for this class must create an empty map and assign it to the instance variable map. I want to populate this map with instances of a different class called tv series, I am using blueJ, I am not sure why this doesn't work
Java Code:
import java.util.*; public class DVD { public static Map<String, TvSeries>DVD; public TvSeries program;