Question

In: Computer Science

● For this task you are required to refactor the badly written program RPN.java. This program...

● For this task you are required to refactor the badly written program RPN.java. This program is a Reverse-Polish Notation calculator which uses a stack.

● You should also fix the indentation of the code.

I AM USING ECLIPSE. THANK YOU IN ADVANCE

import java.util.Scanner;

public class StackNode {
   public StackNode(double data, StackNode underneath) {
       this.data = data;
       this.underneath = underneath;
}
   public StackNode underneath;
  
   public double data;
}

public class RPN {
  
   public void into(double new_data) {
       StackNode new_node = new StackNode(new_data, top);
       top = new_node;
   }
  
   public double outof( ) {
      
       double top_data = top.data;
       top = top.underneath;
       return top_data;
   }
      
   public RPN(String command) {
       top = null;
       this.command = command;
           }
      
   public double get( ) {
       double a, b;
       int j;
          
       for(int i = 0; i < command.length( ); i++) {
           if(Character.isDigit(command.charAt(i))) {// if it's a digit
               double number;
              
       String temp = "";// get a string of the number
      
       for(j = 0; (j < 100) && (Character.isDigit(command.charAt(i)) || (command.charAt(i) == '.')); j++, i++) {
          
           temp = temp + String.valueOf(command.charAt(i));
       }
// convert to double and add to the stack
       number = Double.parseDouble(temp);
  
       into(number);
       }else if(command.charAt(i) == '+') {
           b = outof( );
           a = outof( );
           into(a + b);
       }else if(command.charAt(i) == '-') {
           b = outof( );
           a = outof( );
           into(a - b);
       }else if(command.charAt(i) == '*') {
           b = outof( );
           a = outof( );
           into(a * b);
       }else if(command.charAt(i) == '/') {
           b = outof( );
           a = outof( );
           into(a / b);
       }else if(command.charAt(i) == '^') {
           b = outof( );
           a = outof( );
           into(Math.pow(a, b));
       }else if(command.charAt(i) != ' ') {
           throw new IllegalArgumentException( );
       }
       }

       double val = outof( );

       if(top != null) {
           throw new IllegalArgumentException( );
       }

       return val;
       }

   private String command;
   private StackNode top;


   public static void main(String args[]) {/* main method */
       while(true) {
           Scanner in = new Scanner(System.in);
           System.out.println("Enter RPN expression or \"quit\".");
           String line = in.nextLine( );
       if(line.equals("quit")) {
           break;
       }else {
           RPN calc = new RPN(line);
           System.out.printf("Answer is %f\n", calc.get( ));
}
}
}

Solutions

Expert Solution

CODE

import java.util.Scanner;

class StackNode {

public double data;

public StackNode next;

public StackNode(double data) {

this.data = data;

this.next = null;

}

}

public class RPN {

private String command;

private StackNode top;

public RPN(String command) {

top = null;

this.command = command;

}

public void push(double new_data) {

StackNode new_node = new StackNode(new_data);

if (top == null) {

top = new_node;

return;

}

new_node.next = top;

top = new_node;

}

public double pop() {

if (top == null) {

throw new IllegalArgumentException();

}

double top_data = top.data;

top = top.next;

return top_data;

}

public double get( ) {

double a, b;

int j;

for(int i = 0; i < command.length( ); i++) {

if(Character.isDigit(command.charAt(i))) {// if it's a digit

double number;

String temp = "";// get a string of the number

for(j = 0; (j < 100) && (Character.isDigit(command.charAt(i)) || (command.charAt(i) == '.')); j++, i++) {

temp = temp + String.valueOf(command.charAt(i));

}

// convert to double and add to the stack

number = Double.parseDouble(temp);

push(number);

}else if(command.charAt(i) == '+') {

b = pop( );

a = pop( );

push(a + b);

}else if(command.charAt(i) == '-') {

b = pop( );

a = pop( );

push(a - b);

}else if(command.charAt(i) == '*') {

b = pop( );

a = pop( );

push(a * b);

}else if(command.charAt(i) == '/') {

b = pop();

a = pop();

push(a / b);

}else if(command.charAt(i) == '^') {

b = pop( );

a = pop( );

push(Math.pow(a, b));

}else if(command.charAt(i) != ' ') {

throw new IllegalArgumentException( );

}

}

double val = pop( );

if(top != null) {

throw new IllegalArgumentException( );

}

return val;

}

public static void main(String args[]) {/* main method */

while(true) {

Scanner in = new Scanner(System.in);

System.out.println("Enter RPN expression or \"quit\".");

String line = in.nextLine( );

if(line.equals("quit")) {

break;

}else {

RPN calc = new RPN(line);

System.out.printf("Answer is %f\n", calc.get( ));

}

}

}

}


Related Solutions

Task 2 (P1.2) For this task you are required to summarize the use of microcontrollers in...
Task 2 (P1.2) For this task you are required to summarize the use of microcontrollers in each of the following applications. Indicate the general purpose and the main advantage of using a microcontroller. Arcade games Mechanical dolls (for example, Tickle Me Elmo) Toy drones Each of these must be at least one written paragraph. You are required to cite your sources.
Task 4: Develop Strategies for Improved Cultural Safety As part of this task, you are required...
Task 4: Develop Strategies for Improved Cultural Safety As part of this task, you are required to develop strategies for improved cultural safety when working with the Aboriginal and Torres Strait Islander people in your region (in relation to providing the information session). Activities you need to complete are provided in the questions below. Your answers must be 30-40 words each and specific to the Aboriginal and Torres Strait Islander people in your region and the information session being planned....
this task will require you to reflect upon and analyse a written healthcare scenario from an...
this task will require you to reflect upon and analyse a written healthcare scenario from an ethico -legal perspective, using Driscoll’s reflective model as a guide. It is expected that you willdraw upon the unit content, personal experiences and relevant literature and learning resources to inform your reflection and analysis. demonstrates an understanding of the ethico-legal complexities inherent in the situation, and considers the implications of different courses of action (40%) demonstrates familiarity with key concepts of ethical practice (covered...
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis...
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis of Data. The Analysis, 1 ½ to 2 pages in length (typed and double spaced), is worth 300 points. The maximum number of points you can obtain from the written assignment is 300. For this assignment you will be using be using both your creative writing and your analytical skills. Below you will find two sets of data set forth in two different tables....
For this task you are required to produce a poster or flyer appropriate for display in...
For this task you are required to produce a poster or flyer appropriate for display in a community service facility which identifies at least four different opportunities for community participation and social inclusion for people with disabilities, in your local area. For each of the opportunities you have identified for community participation, describe the interests, abilities and requirements that are associated with the activities, as well as any other information that interested clients would like to know. Also note if...
Your task is to solve the following problems. For each, you must provide a written response...
Your task is to solve the following problems. For each, you must provide a written response and a supporting Punnett square. Your Punnett square must be drawn by hand. The ability to roll the tongue sideways (A) is dominant over the inability to roll one's tongue (a). In humans, non-attached earlobes (B) are dominant over attached earlobes (b). Two parents with attached earlobes can roll their tongues. They are both heterozygous for the tongue rolling trait. What is the genotype...
Detailed answered required In written form Q#01: You are required to identify the set of roles...
Detailed answered required In written form Q#01: You are required to identify the set of roles you can perform in a Multimedia Project development.
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Task Individual Presentation To accomplish this CW you are required to take on a role of...
Task Individual Presentation To accomplish this CW you are required to take on a role of a consultant. The firm you are employed by has a number of both international and local clients requesting assistance and professional advice on various matters. One client request is listed below, with a description of the company background and difficulties encountered. This is an individual assignment. The consultant has to work out solutions and suggestions to the client. All ideas, arguments and recommendations MUST...
This program is to be written in Java Language. Thank you A College has conducted a...
This program is to be written in Java Language. Thank you A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response. Program 1 You are to write a program that reads...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT