Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the expression evaluation example so that an expression with the following operators +, -, *, /, (, and ) can be properly evaluated for

Extend the expression evaluation example so that an expression with the following operators +, -, *, /, (, and ) can be properly evaluated for integer values and double values. Note that your program needs to preserve integer division correctness. For example, 5/10 is 0 instead of 0.5.

Code:

import java.util.Stack; import java.util.Scanner;

public class EvaluateExpression2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an arithmetic expression: "); String expr = input.nextLine(); System.out.println(expr + " = " + evaluateExpression(expr)); } //TODOs: Adjust the two methods in the example to adapt to possible double operands

public static String insertBlanks(String s) { String result = "";

for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(' || s.charAt(i) == ')' || s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '/') result += " " + s.charAt(i) + " "; else result += s.charAt(i); }

return result; } }

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_2

Step: 3

blur-text-image_3

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

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions

Question

develop a Four Links Analysis of the organisations co-operators;

Answered: 1 week ago

Question

Write messages that are used for the various stages of collection.

Answered: 1 week ago