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

  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!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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