Question

In: Computer Science

package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...

package mac286.LinkedLists;

public class Postfix {
        private rStack<String> S;
        private String inSt;
        private ourLinkedList<String> inList, outList;
        public Postfix(String s) {
                S = new rStack<String>();
                inSt = s;
                outList = new ourLinkedList<String>();
                inList = new ourLinkedList<String>();
        }
        public void reset(String s) {
                S = new rStack<String>();
                inSt = s;
                outList = new ourLinkedList<String>();
                inList = new ourLinkedList<String>();
        }
        private boolean isOperator(char c) {
                if(c=='-'||c=='+'||c=='*'||c=='/')
                        return true;
                return false;
        }
        private boolean isParenthesis(char c) {
                if(c == '(' || c==')')
                        return true;
                return false;
        }
        private int precedence(char c) {
                if(c=='-'||c=='+')
                        return 1;
                else
                        return 2;
        }
        private void makeList() {
                //go through the string and extrat operands and operators
                for(int i = 0; i<inSt.length(); i++) {
                        if(inSt.charAt(i) == ' ')
                                continue;
                        else if (Character.isDigit(inSt.charAt(i))) {
                                String num = "";
                                while(i<inSt.length() && Character.isDigit(inSt.charAt(i))) {
                                        num += inSt.charAt(i);
                                        i++;
                                }
                                i--;
                                //add the number to the list 
                                inList.add(num);
                        } else if(isOperator(inSt.charAt(i))) {
                                String num = ""+inSt.charAt(i);
                                inList.add(num);
                        }else if(isParenthesis(inSt.charAt(i))) {
                                String num = ""+inSt.charAt(i);
                                inList.add(num);
                        }else {
                                System.out.println("Something is wrong !!");
                        }
                }
        }
        public void verify() {
                System.out.println(inList.toString());
        }
        public String convert() {
                this.makeList();
                
                while(inList.isEmpty() != true) {
                        String token = inList.remove();
                        if(Character.isDigit(token.charAt(0))){
                                //this is an operand append it to the out list
                                outList.add(token);
                        }else if(token.charAt(0) == '(') {
                                //push it to the stack
                                S.push(token);
                        }else if(token.charAt(0) == ')') {
                                String temp;
                                while(!(temp = S.pop()).equals("(")) {
                                        //append temp to outList
                                        outList.add(temp);
                                }
                        }else {
                                //token is an operator 
                                while(!S.isEmpty() && this.isOperator(S.peek().charAt(0)) && this.precedence(token.charAt(0)) <= this.precedence(S.peek().charAt(0))) {
                                        outList.add(S.pop());
                                }
                                S.push(token);
                        }
                }
                //empty the stack into outputList
                while(!S.isEmpty()) {
                        outList.add(S.pop());
                }
                
                String st = "";
                for(int i = 0; i < outList.size(); i++) {
                        st += outList.elementAt(i) + " ";
                }
                return st;
                
        }
        public void evaluate() {
                //evaluate the expression in outList. 
        }
        

}

Can you please do the evaluate the expression in outList. thank you. complete the evaluate.

Solutions

Expert Solution

public class Postfix {
        private rStack<String> S;
        private String inSt;
        private ourLinkedList<String> inList, outList;
        public Postfix(String s) {
                S = new rStack<String>();
                inSt = s;
                outList = new ourLinkedList<String>();
                inList = new ourLinkedList<String>();
        }
        public void reset(String s) {
                S = new rStack<String>();
                inSt = s;
                outList = new ourLinkedList<String>();
                inList = new ourLinkedList<String>();
        }
        private boolean isOperator(char c) {
                if(c=='-'||c=='+'||c=='*'||c=='/')
                        return true;
                return false;
        }
        private boolean isParenthesis(char c) {
                if(c == '(' || c==')')
                        return true;
                return false;
        }
        private int precedence(char c) {
                if(c=='-'||c=='+')
                        return 1;
                else
                        return 2;
        }
        private void makeList() {
                //go through the string and extrat operands and operators
                for(int i = 0; i<inSt.length(); i++) {
                        if(inSt.charAt(i) == ' ')
                                continue;
                        else if (Character.isDigit(inSt.charAt(i))) {
                                String num = "";
                                while(i<inSt.length() && Character.isDigit(inSt.charAt(i))) {
                                        num += inSt.charAt(i);
                                        i++;
                                }
                                i--;
                                //add the number to the list 
                                inList.add(num);
                        } else if(isOperator(inSt.charAt(i))) {
                                String num = ""+inSt.charAt(i);
                                inList.add(num);
                        }else if(isParenthesis(inSt.charAt(i))) {
                                String num = ""+inSt.charAt(i);
                                inList.add(num);
                        }else {
                                System.out.println("Something is wrong !!");
                        }
                }
        }
        public void verify() {
                System.out.println(inList.toString());
        }
        public String convert() {
                this.makeList();
                
                while(inList.isEmpty() != true) {
                        String token = inList.remove();
                        if(Character.isDigit(token.charAt(0))){
                                //this is an operand append it to the out list
                                outList.add(token);
                        }else if(token.charAt(0) == '(') {
                                //push it to the stack
                                S.push(token);
                        }else if(token.charAt(0) == ')') {
                                String temp;
                                while(!(temp = S.pop()).equals("(")) {
                                        //append temp to outList
                                        outList.add(temp);
                                }
                        }else {
                                //token is an operator 
                                while(!S.isEmpty() && this.isOperator(S.peek().charAt(0)) && this.precedence(token.charAt(0)) <= this.precedence(S.peek().charAt(0))) {
                                        outList.add(S.pop());
                                }
                                S.push(token);
                        }
                }
                //empty the stack into outputList
                while(!S.isEmpty()) {
                        outList.add(S.pop());
                }
                
                String st = "";
                for(int i = 0; i < outList.size(); i++) {
                        st += outList.elementAt(i) + " ";
                }
                return st;
                
        }
        public void evaluate() {
                string token= outlist.remove();

        }
        

}

Related Solutions

this won't compile package com.test; public class CatalogItem { private String title; private double price; public...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public CatalogItem(String title, double price) { super(); this.title = title; this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } } //Book.java package com.test; public class Book extends CatalogItem { private String author; private int ISBN; public Book(String title, double price, String author, int iSBN) { super(title, price); this.author = author; ISBN = iSBN; } public String...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){...
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){ ....... } public static int getInt( String prompt ){ ....... } } Using these method you see above to create a main method in your class called MyExam in the different package to get input and output. The output will tell the user what number is entered. If you entered a 5, the output will be: "The number you entered is 5.". QUESTION 12...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;   ...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;    private double vegetablePrice;    private double fruitPrice;    private double vegetableOrdered;    private double fruitOrdered;       //declared constants    public static final double SERVICE_RATE =0.035;    public static final double DELIVERY_FEE=5;       public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)    {        this.vegetableName = vegetableName;        this.fruitName = fruitName;        this.vegetablePrice = vegetablePrice;        this.fruitPrice...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT