Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Good-day Please assist with correcting my code for this task as per markdown comments below. TASK Follow these steps: For this task, you are required

Good-day

Please assist with correcting my code for this task as per markdown comments below.

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.

MY CODE

package com.rpn; import java.util.Scanner; import java.util.StringTokenizer; class StackNode { public StackNode underneath; public double data; public StackNode(double data, StackNode underneath) { this.data = data; this.underneath = underneath; } } public class RPN { private String command; private StackNode top; public RPN(String command) { top = null; this.command = command; } /* main method */ public static void main(String args[]) { 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 ", calc.get()); } } } public void into(double new_data) { StackNode new_node = new StackNode(new_data, top); if (top == null) { top = new_node; } else { StackNode temp = top; top = new_node; new_node.underneath = temp; } } public double outof() { double popped = Integer.MIN_VALUE; if (top != null) { popped = top.data; top = top.underneath; } return popped; } public double get() { // StringTokenizer class in Java is used to break a string into tokens(String). StringTokenizer stringTokenizer = new StringTokenizer(command," "); while (stringTokenizer.hasMoreTokens()) { String temp = stringTokenizer.nextToken(); if(temp.equals("quit")) { break; } else if(temp.equals("+")){ double num1 = outof(); double num2 = outof(); into(num1+num2); }else if(temp.equals("-")){ double num1 = outof(); double num2 = outof(); into(num2-num1); }else if(temp.equals("*")){ double num1 = outof(); double num2 = outof(); into(num1*num2); }else if(temp.equals("/")){ double num1 = outof(); double num2 = outof(); into(num2/num1); }else{ into(Double.parseDouble(temp)); } } double val = outof(); if (top != null) { throw new IllegalArgumentException(); } return val; } }

CODE REVIEWER MARK DOWN COMMENT

Unfortunately, you have not done enough refactoring/ made enough of the recommended changes for this task to be marked as complete. Please ensure: - Your variable names are descriptive Here is an external resource on using descriptive variable names: https://www.techrepublic.com/blog/software-engineer/variable-names-should-usually-bedescriptive/ - Your naming sticks to camelCase convention - You do not pad your variables declarations and code blocks with blank lines - you do not separate the same set of conditionals with blank lines

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

More Books

Students also viewed these Databases questions

Question

=+16-6 Explain the func- tion of sensory adaptation.

Answered: 1 week ago

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

Discuss the steps in the progressive discipline approach.

Answered: 1 week ago