Concept explainers
Modify the provided starter code by completing the two methods, evalAS(String e) and
evalMD(String e), adding the ability to evaluate arithmetic expressions with + and – or * and /
operators and multi-digit integer operands to the program. An example method has been written for
you in the starter code that does + and – operations with single digit operands. Use that example to
come up with your code to complete the other two methods.
Test your program with the math expressions provided in the comments of the starter code. Then try
other longer valid expressions you come up with for testing. You can assume that the provided
expressions do not have spaces or other characters in them other than valid math operators ( +, - , *, /)
and integers. You can also assume that all expressions begin with a positive integer.
import java.util.Scanner;
public class Lab9{
//if e = "1-2+3+4-5", return 1.
//if e = "9", return 9
//convert
//'0' -> 0,
//'1' -> 1,
//
//'9' -> 9
//this method assumes a positive number to start
public static int evalAS1(String e) {
int r = e.charAt(0)-'0';
for (int i = 1; i < e.length(); i+=2)
if (e.charAt(i) == '+')
r += e.charAt(i+1)-'0';
else
r -= e.charAt(i+1)-'0';
return r;
}
//add a method here for multi-digit numbers using addition and subtraction only
//if e = "123-24+3+467-50", return 519
public static int evalAS(String e){
//add your code here
}
//add a method here for multi-digit numbers using multiply and division only
//integer division is being used here so 123/24 = 5 in the example below
//if e = "123/24*3*467/50", return 140
public static int evalMD(String e) {
//add your code here
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
String again;
do{
System.out.println("Enter your math expression.");
String problem = input.nextLine();
System.out.println("1. My expression contains single digit numbers with addition and subtraction.");
System.out.println("2. My expression contains multi-digit numbers with only addition and subtraction.");
System.out.println("3. My expression contains multi-digit numbers with only multiplication and division.");
System.out.println("4. Exit.");
System.out.println("Pick an option from the menu.");
choice = input.nextInt();
input.nextLine();
switch(choice){
case 1:
System.out.println("The answer is "+evalAS1(problem));
break;
case 2:
System.out.println("The answer is "+evalAS(problem));
break;
case 3:
System.out.println("The answer is "+evalMD(problem));
break;
case 4:
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("You entered an invalid menu option.");
System.out.println("Please try again.");
}
System.out.println("Would you like to enter a new expression?");
again=input.nextLine();
}while(again.toLowerCase().charAt(0)!='n');
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images
- I need help with this question.arrow_forwardPlease use Java to complete the problem in the attatchment. Thank you!arrow_forwardIn this assignment you will demonstrate your knowledge of debugging by fixing the errors you find in the program below. Fix the code, and paste it in this document, along with the list of the problems you fixed.This example allows the user to display the string for the day of the week. For example, if the user passed the integer 1, the method will return the string Sunday. If the user passed the integer 2, the method will return Monday. This code has both syntax errors and logic errors. Hint: There are two logic errors to find and fix (in addition to the syntax errors).Inport daysAndDates.DaysOfWeek;public class TestDaysOfWeek {public static void main(String[] args) {System.out.println("Days Of week: ");for (int i = 0;i < 8;i++) {System.out.println("Number: " + i + "\tDay Of Week: " + DaysOfWeek.DayOfWeekStr(i) )}}}package daysAndDatespublic class DaysOfWeek {public static String DayOfWeekStr(int NumberOfDay) {String dayStr = ""switch (NumberOfDay) {case 1:dayStr =…arrow_forward
- Write an improved version of the chaos . py program from Chapter 1 thatallows a user to input two initial values and the number of iterations, and then prints a nicely formatted table showing how the values changeover time. For example, if the starting values were . 25 and . 26 with 10iterations, the table might look like this: index 0.25 0.26----------------------------1 0 . 731250 0 . 7503602 0 . 766441 0 . 7305473 0 . 698135 0 . 7677074 0 . 821896 0 . 6954995 0 . 570894 0 . 8259426 0 . 955399 0 . 5606717 0 . 166187 0 . 9606448 0 . 540418 0 . 1474479 0 . 968629 0 . 49025510 0.118509 0 . 974630arrow_forwardGive a concrete example of a function, where blackbox testing might give the impression that “everything’s OK,” while whitebox testing might uncover an error. Give another concrete example of a function where the opposite is true. You need to support both examples with test cases.arrow_forwardCSC108H Loops and Strings Lab 1 For-Loops and Strings It's common to write loops that process characters of strings. Remember that you can use a regular for-loop if all you need are the characters, and a range for-loop if you need the indices. 1. Assign the string "superconductivity" to a variable s. 2. Write a loop that prints each character in s on a separate line. 3. Write a loop that prints each character in s on the same line, with a comma and a space after each character: s, u, p, e, r, C, etc. Notice that there is a comma after the last character. We won't worry about this. To do this, build a new string that includes the appropriate characters, then print that new string. 2 Solving a Problem Now let's solve a problem from the Chapter 3 exercises. The problem is called English or French: https://dmoj.ca/problem/ccc11s1 Let's do some analysis on this problem to give us a plan that we can follow. 3 1. Notice that the English/French data is spread over N lines. Which of the three…arrow_forward
- I need help with Java code. I'm not sure how to start...arrow_forwardImage one is my code, I wanted to make some change to the blue part's code. The code I written in red(see image 1) is the method to check that an integer has exactly 5 digits is provided for you (fiveDigits). You just need to call it appropriately in your code(the part in blue square)! Notice that the fiveDigits method returns a boolean - think carefully about how you should use this return type to reprompt the user until they enter a valid zip code. And I want the same output be like (see image 2) for the invalid zip code part: I should only reprompt the user with the message "Invalid zip-code, enter valid zip-code: " until they enter a valid zip code. Similarly, if the user enters an invalid pain level, you should reprompt the user with the message "Invalid pain level, enter valid pain level (1-10): " until they enter a valid pain level. For example(image2)arrow_forwardYOU SHOULD NOT USE LOOPS (for, while,….) OR CONDITIONALS (if..else,….),For this question, you should use Math function for doing the “power” and “π”. When printingthe volume and the area values, DON’T ENTER THEM MANUALLY. Only print 2 numbers after the decimal points.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education