
Evaluation of Expression
Program plan:
Filename: “Constant.java”
This program file is used to define a class “Constant”. In the code,
- Define a class “Constant”.
- Define the constructor “Constant()”.
- Set the value of “number”.
- Define the method “value()”.
- Return the value of “number”.
- Define the constructor “Constant()”.
Filename: “Variable.java”
This program file is used to define a class “Variable”. In the code,
- Define a class “Variable”.
- Define a variable “letter”.
- Define the constructor “Variable ()”.
- If the “letter” is not equal to “x”,
- Throw an exception.
- Set the letter.
- If the “letter” is not equal to “x”,
- Define the method “value()”.
- Return the value of “x”.
Filename: “Expression.java”
This program file is used to define an interface “Expression”. In the code,
- Define an interface “Expression”.
- Define the method “value()” with argument “x”.
Filename: “Difference.java”
This program file is used to define a class “Difference”. In the code,
- Define a class “Difference”.
- Define the class members “leftOp” and “rightOp”.
- Define the constructor “Difference ()”.
- Set the value of class members.
- Define the method “value()” to calculate the difference of two expression.
- Return the difference.
Filename: “Product.java”
This program file is used to define a class “Product”. In the code,
- Define a class “Product”.
- Define the class members “leftOp” and “rightOp”.
- Define the constructor “Product()”.
- Set the value of class members.
- Define the method “value()” to calculate the product of two expression.
- Return the product.
Filename: “Quotient.java”
This program file is used to define a class “Quotient”. In the code,
- Define a class “Quotient”.
- Define the class members “leftOp” and “rightOp”.
- Define the constructor “Quotient()”.
- Set the value of class members.
- Define the method “value()” to calculate the quotient of two expression.
- Return the quotient.
Filename: “Sum.java”
This program file is used to define a class “Sum”. In the code,
- Define a class “Sum”.
- Define the class members “leftOp” and “rightOp”.
- Define the constructor “Sum()”.
- Set the value of class members.
- Define the method “value()” to calculate the sum of two expression.
- Return the sum.
Filename: “ExpressionTokenizer.java”
This program file is used to define a class “ExpressionTokenizer”. In the code,
- Define a class “ExpressionTokenizer”.
- Define the class members “input”, “start” and “end”.
- Define the constructor “ExpressionTokenizer()”.
- Set the values of “input”, “start”, “end” and find the first token using “nextToken()”.
- Define the method “peekToken()”.
- If the “start” is greater than “input.length()”.
- Return “null”.
- Else,
-
- Return the substring.
- Return “null”.
- If the “start” is greater than “input.length()”.
- Define the method “nextToken()”.
- Call the method “peekToken()” to get the token.
- Assign the value of “end” to “start”.
- If the value of “start” is greater than or equal to length of the input,
- Return the value of “r”.
- If the character at “start” is a digit,
-
- Set “end” equal to “start+1”,
- Iterate a “while” loop,
- Increment the “end” by 1.
- Set the value of “end”.
- Increment the “end” by 1.
- Return the value of “r”.
- Return the value of “r”.
Filename: “Evaluator.java”
This program file is used to define a class “Evaluator”. In the code,
- Define a class “Evaluator”.
- Define the class members “tokenizer” which is an object of class “ExpressionTokenizer”.
- Define the constructor “Evaluator()”.
- Define “tokenizer”.
- Define the method “getExpressionValue()”.
- Get terms using “getTermValue()” to “value”.
- Define a Boolean value “done” and assign “false” to it.
- While “true”,
- Call “peekToken()” to get the token and assign to “next”.
- If the value of “next” is “+” or “-”.
- Get the next token using “nextToken()”.
- Get next term to “value2”.
- If the “next” is “+”,
-
- Calculate the sum of “value” and “value2” and assign to “value”.
- Else,
-
- Calculate the difference of “value” and “value2” and assign to “value”.
- Else,
- Set “done” equal to “true”.
-
-
- Return “value”.
-
- Define the method “getTermValue()”.
- Get factors using “getFactorValue()” to “value”.
- Define a Boolean value “done” and assign “false” to it.
- While “true”,
- Call “peekToken()” to get the token and assign to “next”.
- If the value of “next” is “*” or “/”.
- Get the next token using “nextToken()”.
- Get next factor to “value2”.
- If the “next” is “*”,
-
- Calculate the Product of “value” and “value2” and assign to “value”.
- Else,
-
- Calculate the Quotient of “value” and “value2” and assign to “value”.
- Else,
- Set “done” equal to “true”.
-
-
- Return “value”.
-
- Define the method “getFactorValue()”.
- Declare “value”.
- Call “peekToken()” to get the token and assign to “next”.
- If the value of “next” is “(”.
- Get the next token using “nextToken()”.
- Discard “(” using “nextToken()”.
- Get next expression to “value”.
- Discard “)” using “nextToken()”.
-
-
- Else,
- If the “next” is “x”,
- Get the variable “x” to “value”.
- Else,
- Get the next token to “value”.
- If the “next” is “x”,
- Return “value”.
- Else,
-
-
Filename: “ExpressionCalculator.java”
This program file is used to define a class “ExpressionCalculator”. In the code,
- Define a class “ExpressionCalculator”.
- Define the method “main()”.
- Define the object “in” of “Scanner”.
- Prompt the user to enter the expression.
- Read the lines using “nextLine()”.
- Evaluate the expression “input”.
- Prompt the user to enter the value for “x”.
- Get the value of expression “input” to “value”.
- New line
- Print “value”.
- Define the method “main()”.

Want to see the full answer?
Check out a sample textbook solution
Chapter 13 Solutions
Big Java Late Objects
- Programming Problems 9.28 Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a C program that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as follows: ./addresses 19986 Your program would output: The address 19986 contains: page number = 4 offset = 3602 Writing this program will require using the appropriate data type to store 32 bits. We encourage you to use unsigned data types as well. Programming Projects Contiguous Memory Allocation In Section 9.2, we presented different algorithms for contiguous memory allo- cation. This project will involve managing a contiguous region of memory of size MAX where addresses may range from 0 ... MAX - 1. Your program must respond to four different requests: 1. Request for a contiguous block of memory 2. Release of a contiguous block of memory 3. Compact unused holes of memory into one single block 4.…arrow_forwardusing r languagearrow_forwardWrite a function to compute a Monte Carlo estimate of the Beta(3, 3) cdf, and use the function to estimate F(x) for x = 0.1,0.2,...,0.9. Compare the estimates with the values returned by the pbeta function in R.arrow_forward
- You are given a class that processes purchases for an online store. The class receives calls to: • Retrieve the prices for items from a database • Record the sold items • Update the database • Refresh the webpage a. What architectural pattern is suitable for this scenario? Illustrate your answer by drawing a model for the solution, showing the method calls/events. b. Comment on how applying this pattern will impact the modifiability of the system. c. Draw a sequence diagram for the update operation.arrow_forwardThe images I have uploaded are the part 1 to 4 and questions below are continue on the questions uploaded 5. C++ Class Template with Method Stubs #pragma once #include <iostream> #include <string> #include <stdexcept> #include <vector> template <typename T> class HashTable { private: struct Entry { std::string key; T value; bool isOccupied; bool isDeleted; Entry() : key(""), value(), isOccupied(false), isDeleted(false) {} }; Entry* table; size_t capacity; size_t size; double loadFactorThreshold; size_t customHash(const std::string& key) const { size_t hash = 5381; for (char c : key) { hash = ((hash << 5) + hash) + c; } return hash; } size_t probe(const std::string& key, bool forInsert = false) const; void resize(); public: // Constructor HashTable(size_t initialCapacity = 101); // Big…arrow_forwardthis project is NOT for graded(marks) purposes, please help me with the introduction. give me answers for the project. i will include an image explaining everything about the project.arrow_forward
- Java Graphics (Bonus In this lab, we'll be practicing what we learned about GUIs, and Mouse events. You will need to implement the following: A GUI with a drawing panel. We can click in this panel, and you will capture those clicks as a Point (see java.awt.Point) in a PointCollection class (you need to build this). The points need to be represented by circles. Below the drawing panel, you will need 5 buttons: O о о ○ An input button to register your mouse to the drawing panel. A show button to paint the points in your collection on the drawing panel. A button to shift all the points to the left by 50 pixels. The x position of the points is not allowed to go below zero. Another button to shift all the points to the right 50 pixels. " The x position of the points cannot go further than the You can implement this GUI in any way you choose. I suggest using the BorderLayout for a panel containing the buttons, and a GridLayout to hold the drawing panel and button panels. Regardless of how…arrow_forwardalso provide the number of moves(actions) made at state A and moves(actions) made state B. INCLUDE Java program required(this question is not graded)arrow_forwardYou are given a class that processes purchases for an online store. The class receives calls to: • Retrieve the prices for items from a database • Record the sold items • Update the database • Refresh the webpage a. What architectural pattern is suitable for this scenario? Illustrate your answer by drawing a model for the solution, showing the method calls/events. b. Comment on how applying this pattern will impact the modifiability of the system. c. Draw a sequence diagram for the update operation.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





