Infix expression and translate it to a postfix expression

Assignment Help JAVA Programming
Reference no: EM13168445

 program below is suppose to take in a infix expression and translate it to a postfix expression, but I need a way for the user to be able to input the expression using a scanner and output the postfix. Can someone help me with this and make any changes to my program to make it work? Thank you! I know there is no main method, but that is what I need help writing to make it run properly.

 

import java.util.Scanner;
import java.util.Stack;


public class InfixToPostfix {
    
    private final char ADD = '+';
    /** constant for subtraction symbol */
    private final char SUBTRACT = '-';
    /** constant for multiplication symbol */
    private final char MULTIPLY = '*';
    /** constant for division symbol */
    private final char DIVIDE = '/';
    
    private Stack stack;
    public String expression;
    public char a;
    char top;
    String result = "";
    int value2;
    
    public InfixToPostfix(String b){
        expression = b;
        int sizeOfExpression = expression.length();
        stack = new Stack();
    }
    
    
    public String operator()
    {
        
        for (int i = 0; i < expression.length(); i++)
            a = expression.charAt(i);
        switch (a)
        {
        case ADD:
        onStack(a,1);
        break;
        
        case SUBTRACT:
        onStack(a,1);
        break;
        
        case MULTIPLY:
        onStack(a,2);
        break;
        
        case DIVIDE:
        onStack(a,2);
        
        default:
        result = result + a;
        break;
        }
    
        while (!stack.isEmpty())
        {
            result = result + stack.pop();
            
        }
        return result;
}
    public void onStack(char a, int value1)
    {
        while (!stack.isEmpty())
        {
            top = (char) stack.pop();
            
        
        if (top == '+' || top == '-')
            value2 = 1;
        else value2 = 2;
        if (value2 < value1)
        {stack.push(top);
                }
        else
            result = result + top;
        
        }


stack.push(a);
}

    
    
    


    
        // TODO Auto-generated method stub

    }

 

 

Reference no: EM13168445

Questions Cloud

Is the process in control : The upper and lower limits for diving ring diameters made by John's Swimming are 40 and 39 cm. John took 11 samples with the following average diameters (39, 39.1, 39.2, 38.9, 39.4, 39.5 39.6, 39.7, 39.8, 39.9, 40.1). Is the process in control?
Write a function that collects integers from the user until : Write a function that collects integers from the user until a 0 is encountered and returns them in a list in the order they were input (ML only).
Differences between mixed methods approach and survey design : Discuss the similarities and differences between mixed methods approach and survey design.I am looking forward to receiving your thoughts.
State how you would gain support for your plan : State how you would gain support for your plan and from whom and state who you would circulate your plan to and why you chose these people.
Infix expression and translate it to a postfix expression : program below is suppose to take in a infix expression and translate it to a postfix expression, but I need a way for the user to be able to input the expression using a scanner and output the postfix.
Calculate concentration of the hydrochloric acid solution : A student used 40.5mL of 0.48M sodium carbonate solution and 45.6mL of the hydrochloric acid solution. The substance in the beaker at the end of the experiment had a mass of 0.93g. Calculate the concentration of the hydrochloric acid solution.
How much naf can be produced : Sodium reacts with flourine to produce sodium flouride. If 4.0 grams of sodium reacts with 5.0 grams of flourine, how much NaF can be produced?
Include methods to set and get values for each data field : Design a class named MagazineSubscription that has fields for a subscriber's name, the magazine name, and number of months remaining in the subscription. Include methods to set and get the values for each data field.
How much volume will you need to draw : A fluid has a density of 4.25 g/mL. If you need to acquire 5.0 g of the fluid, how much volume will you need to draw?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Study the code and implement the operator overloading

The Table Q3 on the next page is the code of a class named Circle. Study the code and implement the operator overloading for these relational operators ( , >=) for the Circle class. Then, write a test program that creates two instances of the Circle ..

  Write specifications for method which advances date by one

Write specifications for the method which advances any given date by one day. Comprise a statement of purpose, pre- and post-conditions, and description of parameters.

  Method named isallvowels that returns

Write a method named isAllVowels that returns whether a String consists entirely of vowels (a, e, i, o, or u, case-insensitively). If every character of the String is a vowel, your method should return true. If any character of the String is a non-vo..

  Write a generic class

Write a generic class (using Java generics) called Pair that stores two values of the generic type.  It should have a constructor to set them, two methods getfirst() and getsecond() to retrieve them.

  An infix to postfix method for my java program.

an infixToPostFix method for my java program. The method must work with stacks and it should take a string as parameter. The method should be able to convert to postfix mathematic expressions such as: (12 + 4) - 23(9-6)/12. Treat the parenthesis as l..

  Java program that prompts the user to input an integer

Write a Java program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

  Huge integer class

Huge Integer Class) Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods parse, toString, add and subtract. Method parse should receive a String, extract each digit using met..

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Java class to accept a user-s hourly rate of pay

Write a class that accepts a user's hourly rate of pay and the number of hours worked. Display the user's gross pay, the withholding tax (15% of gross pay), and the net pay (gross pay - withholding).

  Web engineering principles

web application development, Web Engineering methodology, UML-based Web Engineering (UWE), models to the expansion of web applications, Object Oriented Web Solutions (OOWS),Web Modeling Language (WebML) methodology, Object-Oriented Hypermedia Design..

  Implement the finite field gf

Write a program called "GF2.java" to implement the finite field GF(p n )where p is a prime number andn is a positive integer.You also need to write four methodsto realize.

  Write java program to accept two words as input

Write a Java program that accepts two words as input and determines if one of them is resulting from changing the order of the others' letters.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd