Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 22, Problem 4PC
Program Plan Intro
Prefix Calculator
Program plan:
- Include necessary header files.
- Define the class “PreficCalc”.
- Declare the necessary variables.
- Define the method “start()”.
- Create labels and text fields to enter the prefix expression.
- Create a button “evaluate” to submit and calculate the resultant value.
- Define the class “CmdTextListener”.
- Define the method “handle()”.
- Declare the string variable to get a prefix expression.
- Declare the method “buildExpr()” to read and build a binary tree.
- Declare the method “eval()” to evaluate the tree expression.
- Display the result into the resultant text field.
- Define the method “eval()” to evaluate the expression.
-
- The “if” statement check, left and right child of tree is equal to null.
- True, return the integer value.
- Otherwise,
- Call the method “eval()” and assign the result into “leftOp”.
- Call the method “eval()” and assign the result into “rightOp”.
- The “if” statement check, tree value is equal to “*”.
- True, return the multiplied value.
- The “if” statement check, tree value is equal to “+”.
- True, return the added value.
- The “if” statement check, tree value is equal to “+”.
- True, return the multiplied value.
- Return “0”.
- Define the method “isNumber()”.
- If the given expression is number then, return the number itself.
- Define the method “isNumber()”.
- Define the class “Node”.
- Declare the necessary variables.
- Define the parameterized constructor.
- Define the method “buildExpr()” to read and build a binary tree.
- Read the input from the user.
- The “if” statement check the expression is number.
- True, return the node itself.
- Otherwise, build a non-leaf tree.
- Call the method “Node()” and return a value.
- Otherwise, build a non-leaf tree.
- True, return the node itself.
- The “if” statement check, left and right child of tree is equal to null.
- Define the method “handle()”.
- Define the “main()” method.
- Declare the method “launch()” to call the “start()” method eventually.
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
JAVA LANGUAGE CODE
Postfix Calculator
by CodeChum Admin
One good implementation of computing infix expressions is to transform them to postfix and then evaluate via the postfix expression.
Infix expressions is the common way of writing arithmetic expressions. The binary operator come between them as shown below:
2 * 5 + 9 - 10 / 20
In postfix expressions, the operands come first before the operator:
2 5 * 9 + 10 20 / -
A stack can be used to evaluate postfix expressions. The operands are pushed onto the Stack and when an operator is found two operands are popped and the operation is performed and finally the result is pushed back onto the Stack.
The final answer will be the lone element of the Stack.
Input
The first line contains a positive integer n representing the number of postfix expressions. What follows are n postfix expressions themselves.
5
10 20 +
30 15 2 * +
100 20 30 + /
90 20 10 + + 0 /
9 3 - 10 + 2 *
Output
A single line containing the result of…
Recursive PrintingDesign a recursive function that accepts an integer argument,n , and prints the numbers 1 up through n .
Recursive Syntax
The recursive structure i.e of natural language like English can be expressed in syntax rules written in the format known as BNF (Bachus-Naur Form). While BNF is ordinarily used as a guide for parsing (that is, determining whether and how a given string follows the syntax rules), An example of this can be found in the sample program SimpleRandomSentences.
You should write a similar program that implements the following rules:<sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]<simple_sentence> ::= <noun_phrase> <verb_phrase><noun_phrase> ::= <proper_noun> |<determiner> [ <adjective> ]. <common_noun> [ who <verb_phrase> ]<verb_phrase> ::= <intransitive_verb> |<transitive_verb> <noun_phrase> |is <adjective> |believes that <simple_sentence><conjunction> ::= and | or | but | because<proper_noun> ::= Fred | Jane | Richard Nixon | Miss…
Chapter 22 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Ch. 22.1 - Prob. 22.2CPCh. 22.1 - Prob. 22.3CPCh. 22 - Prob. 1MCCh. 22 - Prob. 2MCCh. 22 - Prob. 3MCCh. 22 - Prob. 4MCCh. 22 - Prob. 5MCCh. 22 - Prob. 6MCCh. 22 - Prob. 7MCCh. 22 - Prob. 8MC
Ch. 22 - Prob. 9MCCh. 22 - Prob. 10MCCh. 22 - Prob. 11TFCh. 22 - Prob. 12TFCh. 22 - Prob. 13TFCh. 22 - Prob. 14TFCh. 22 - Prob. 15TFCh. 22 - Prob. 16TFCh. 22 - Prob. 17TFCh. 22 - Prob. 18TFCh. 22 - Prob. 19TFCh. 22 - Prob. 20TFCh. 22 - Prob. 21TFCh. 22 - Prob. 1FTECh. 22 - Prob. 2FTECh. 22 - Prob. 3FTECh. 22 - Prob. 1SACh. 22 - Prob. 2SACh. 22 - Prob. 3SACh. 22 - Prob. 4SACh. 22 - What is a priority queue?Ch. 22 - Prob. 6SACh. 22 - Prob. 7SACh. 22 - Prob. 1AWCh. 22 - Prob. 2AWCh. 22 - Prob. 3AWCh. 22 - Prob. 4AWCh. 22 - Prob. 5AWCh. 22 - Prob. 6AWCh. 22 - Prob. 7AWCh. 22 - Prob. 4PCCh. 22 - Prob. 6PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Demonstratic the concept of pointer arithmaticarrow_forwardRecursive Power FunctionWrite a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. SAMPLE RUN #0: ./recursiveExponent Hide Invisibles Highlight: Show Highlighted Only 2^3=8↵ 2^4=16↵ 3^3=27↵ 6^3=216↵ 7^7=823543↵ 10^9=1000000000↵arrow_forwardMagic Number of coding-:A number is said to be a magic number,if summing the digits of the number and then recursively repeating this process for the given sumuntill the number becomes a single digit number equal to 1. Example: Number = 50113 => 5+0+1+1+3=10 => 1+0=1 [This is a Magic Number] Number = 1234 => 1+2+3+4=10 => 1+0=1 [This is a Magic Number] Number = 199 => 1+9+9=19 => 1+9=10 => 1+0=1 [This is a Magic Number] Number = 111 => 1+1+1=3 [This is NOT a Magic Number].arrow_forward
- Magic Number coding question---1. A number is said to be a magic number,if summing the digits of the number and then recursively repeating this process for the given sumuntill the number becomes a single digit number equal to 1. Example: Number = 50113 => 5+0+1+1+3=10 => 1+0=1 [This is a Magic Number] Number = 1234 => 1+2+3+4=10 => 1+0=1 [This is a Magic Number] Number = 199 => 1+9+9=19 => 1+9=10 => 1+0=1 [This is a Magic Number] Number = 111 => 1+1+1=3 [This is NOT a Magic Number].arrow_forwardC languagearrow_forwardUML Design for Turing Machine Description: An electro-mechanical device used by the British cryptologists to help decipher German Enigma-machine-encrypted secret messages during World War II. A Turing Machine is a very simple abstract model of computation. The machine has an infinite tape that is divided into cells (or squares). Each cell contains a symbol (or character) chosen from some small set of possible symbols. Most of the cells are blank, that is, they contain the blank symbol. The number of non-blank cells is finite. The machine is located on one of the cells, and it can only "see" the symbol in the cell where it is located. It can write a new value to that cell. It can move left or right on the tape, one cell at a time. The machine is in one of a fixed, finite number of possible states. One of the states is the halt state. A Turing Machine has a table of rules. The table of rules is the program for the machine. A rule tells the machine what action to take when it is in a…arrow_forward
- c programarrow_forwardMatrix Problem Code this in C Write a program that reads two matrices of integers. It then generates the sum, difference, and product of the two matrices. The sum of the two matrices of the same size is a matrix with elements that are the sums of the corresponding elements of the two given matrices. Addition is not defined for matrices of different sizes. If A and B are matrices of the same size, then we define subtraction as follows: A – B = A + (-B). Thus, to subtract matrix B from matrix A, we simply subtract corresponding elements. To multiply a matrix by another matrix we need to do the "dot product" of rows and columns. The "Dot Product" is where we multiply matching members, then sum up: (1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58 We match the 1st members (1 and 7), multiply them, likewise for the 2nd members (2 and 9) and the 3rd members (3 and 11), and finally sum them up. Use the following definitions:#define ROW 6#define COL 6void add(int matrixResult[][COL],int…arrow_forwardQuestion 5: 2.1 Sum Problem View Past Answers Given a positive number n, the sum of all digits is obtained by adding the digit one-by-one. For example, the sum of 52634 is 5+ 2 +6 +3 + 4 = 20. Write a recursive and iterative function sum (n) to compute the sum of all the digits in n. You may assume that n > 0. template.py 1 def sum R(n): if len(str(n)) == 1: 2- return n else: return 20 7. def sum_I(n): add = 0 8 while n: 10 add += n % 10 n //= 10 return add 11 12arrow_forward
- Python Need help with code A rule has three parts: •name –a simple name for the rule (e.g., up, left, down, right) •precondition function –a Boolean function that accepts a state and returns true if the rule is applicable to state •action function –a function that accepts a state and returns the successor state obtained by applying the rule. You can use these rules to implement functions such as applicable-rule, which returns a list of the rules applicable to a given state, and successor-state, which returns the successor state for a given state and rule. Question: Encode the rules for the sliding-tile puzzle. Remember that it is easiest to consider moving the empty space up, left, down, or right. Using these rules, write routines to determine the rules applicable to a state and the successor state given a state and rule to apply. Note that you could implement these as iterators or have them return lists (or vectors) or rules and states.arrow_forwardCodeWorkout Gym Course Search exercises... Q Search kola shreya@colum X459: Review- Fibonacci In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, characterized by the fact that every number after the first two is the sum of the two preceding ones: e, 1, 1, 2, 3, 5, 8, 13, Write a recursive function that the returns the nth fibonacci number. Examples: fibonacci(0) -> 0 fibonacci(1) -> 1 fibonacci(7) -> 13 Your Answer: 1 public int fibonacci(int n) { 2 3} 4 CodeWorkout © Virginia Tech About License Privacy Contactarrow_forwardWhich data structures are applied when dealing with a recursive function?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT