Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please assist with this task Task Follow these steps: For this task, you are required to refactor the badly written program RPN.java . This program

Please assist with this task

Task

Follow these steps:

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.

A Reverse-Polish Notation calculator is a calculator that will calculate

equations where the operator follows the operands. Therefore, instead of

inputting an equation as 1 + 2, a Reverse-Polish Notation calculator

would take the following input 1 2 +.

A stack is a data structure in which items are added to the

top of the stack and removed from the top of the stack. It is

therefore known as a last-in, first-out (LIFO) data structure.

Stack terminology:

Push is an operation that adds an item to the top

of a stack.

Pop is an operation that removes an item from the top of a stack.

The pseudocode for this program is:

Get an equation (e.g. 2 3 +) from the user as input.

Loop through the string value input by the user.

When you encounter a number (remember that numbers

can include decimal points), add it to the stack (push).

When you encounter an operator (e.g. +, -, /, etc.), pop two

numbers from the stack and perform the appropriate

calculation.

Display the answer of the calculation to the user.

For this task you are required to:

Troubleshoot and debug the code so that it runs correctly.

Fix the indentation and formatting of the code so that it adheres to

the guidelines provided here.

Make sure that all the names of variables, classes, methods, etc.

adhere to the guidelines provided here.

Refactor the code to improve the quality and readability of the code

in other ways highlighted in this task.

Most students who have to resubmit this Task do so due to errors

and styling issues. To help you discover errors and styling issues

quicker (before submitting your Task), please install the linter,

SonarLint, to your IDE or editor. Use SonarLint to identify problems

and clear each one of them until there are no issues being flagged.

By default, its configured to flag issues based on the best practices

weve discussed in this review.

RPN.java Badly written program to be refactored

package com.rpn; import java.util.Scanner; import java.util.StringTokenizer; class StackNode { private StackNode next; private double data; public StackNode(double data, StackNode next) { this.data = data; this.next = next; } } public class RPN { private String command; private StackNode top; public RPN(String command) { this.command = command; this.top = null; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter RPN expression or 'quit' to exit"); while (true) { String line = scanner.nextLine(); if (line.equals("quit")) { break; } else { RPN calc = new RPN(line); System.out.println("Answer is: " + calc.get()); } } scanner.close(); } public void push(double newData) { StackNode newNode = new StackNode(newData, this.top); this.top = newNode; } public double pop() { if (this.top == null) { System.out.println("Stack is empty"); return Double.NaN; } else { double poppedData = this.top.data; this.top = this.top.next; return poppedData; } } public double get() { StringTokenizer tokenizer = new StringTokenizer(this.command, " "); while (tokenizer.hasMoreTokens()) { String temp = tokenizer.nextToken(); if (temp.equals("+")) { double num1 = pop(); double num2 = pop(); push(num2 + num1); } else if (temp.equals("-")) { double num1 = pop(); double num2 = pop(); push(num2 - num1); } else if (temp.equals("*")) { double num1 = pop(); double num2 = pop(); push(num2 * num1); } else if (temp.equals("/")) { double num1 = pop(); double num2 = pop(); push(num2 / num1); } else { push(Double.parseDouble(temp)); } } return this.top.data; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions