Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 21, 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 functions are ones that repeat themselves repeatedly.
Chapter 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 21.1 - Prob. 21.2CPCh. 21.1 - Prob. 21.3CPCh. 21 - Prob. 1MCCh. 21 - Prob. 2MCCh. 21 - Prob. 3MCCh. 21 - Prob. 4MCCh. 21 - Prob. 5MCCh. 21 - Prob. 6MCCh. 21 - Prob. 7MCCh. 21 - Prob. 8MC
Ch. 21 - Prob. 9MCCh. 21 - Prob. 10MCCh. 21 - Prob. 11TFCh. 21 - Prob. 12TFCh. 21 - Prob. 13TFCh. 21 - Prob. 14TFCh. 21 - Prob. 15TFCh. 21 - Prob. 16TFCh. 21 - Prob. 17TFCh. 21 - Prob. 18TFCh. 21 - Prob. 19TFCh. 21 - Prob. 20TFCh. 21 - Prob. 21TFCh. 21 - Prob. 1FTECh. 21 - Prob. 2FTECh. 21 - Prob. 3FTECh. 21 - Prob. 1SACh. 21 - Prob. 2SACh. 21 - Prob. 3SACh. 21 - Prob. 4SACh. 21 - What is a priority queue?Ch. 21 - Prob. 6SACh. 21 - Prob. 7SACh. 21 - Prob. 1AWCh. 21 - Prob. 2AWCh. 21 - Prob. 3AWCh. 21 - Prob. 4AWCh. 21 - Prob. 5AWCh. 21 - Prob. 6AWCh. 21 - Prob. 7AWCh. 21 - Prob. 4PCCh. 21 - 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
- *C Language The greatest common divisor of integers x and y is the largest integer that divides both x and y. Write a recursive function GCD that returns the greatest common divisor of x and y. The GCD of x and y is defined as follows: If y is equal to zero, then GCD(x, y) is x; otherwise GCD(x, y) is GCD(y, x % y) where % is the remainder operator.arrow_forwardExponent y Catherine Arellano mplement a recursive function that returns he exponent given the base and the result. for example, if the base is 2 and the result is 3, then the output should be 3 because the exponent needed for 2 to become 8 is 3 (i.e. 23 = 8) nstructions: 1. In the code editor, you are provided with a main() function that asks the user for two integer inputs: 1. The first integer is the base 2. The second integer is the result 2. Furthermore, you are provided with the getExponent() function. The details of this function are the following: 1. Return type - int 2. Name - getExponent 3. Parameters 1. int - base 2. int - result 4. Description - this recursive function returns the exponent 5. Your task is to add the base case and the general case so it will work Score: 0/5 Overview 1080 main.c exponent.h 1 #include 2 #include "exponent.h" 3 int main(void) { 4 int base, result; 5 6 printf("Enter the base: "); scanf("%d", &base); 7 8 9 printf("Enter the result: ");…arrow_forwardRecursive 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…arrow_forward
- PythonDesign a recursive function that accepts an integer argument, n, and prints the number 1 up through n.arrow_forward7. Recursive Power Method In Python, design 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 the exponent is a nonnegative integer.arrow_forwardDemonstratic the concept of pointer arithmaticarrow_forward
- Recursive 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_forwardMagic 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_forward
- Python Language Prime funcationarrow_forwardPYTHON RECURSIVE FUNCTION Write a python program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia then the output is (must match the below ordering): Julia Lucas Mia Julia Mia Lucas Lucas Julia Mia Lucas Mia Julia Mia Julia Lucas Mia Lucas Juliaarrow_forwardExplain differences between recursive and non recursive functions.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 Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning