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…
Demonstratic the concept of pointer arithmatic
Magic 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].
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 programming Language Recursion Question 1: Write c program to print the sum of the array elements using recursive function. You should have the main function + function to find the summation. Question 2: Write recursive function that returns the value of the following recursive definition: f(x) = 0 if x<= 0 f(x-1 )+ 2 otherwise You should have the main function + the recursive function.arrow_forwardC languagearrow_forwardDefine the term " operator subtract " .arrow_forward
- C Language Explain the terms infix expression, prefix expression, and postfix expression. Convert the following infix expressions to their postfix and prefix equivalents: (a) A * B + C (b) A / B * C - D (c) A + (B - C) / D * (E + F) Thank youarrow_forwardTask using C language One common way of verifying if the data integrity of a file was preserved during a copy or transmission is to verify if the checksum matches. The checksum is a small piece of data computed from the original data. Your task is to compute a recursive function that maps an integer into a single digit to be used as checksum. Given an input integer in the range from 0 to 1012, the checksum is the sum of the digits of the input number. While the resulting sum has multiple digits, the checksum will be the sum of its digits instead. For instance: if the input is 34, the checksum is 7 (3+4); if the input is 99, the sum of its digits is 18 (9+9), so the checksum is 9 (1+8); if the input is 99999999999, the sum of its digits is 99 (9+9+9+9+9+9+9+9+9+9+9), whose sum of digits is 18 (9+9), so the checksum is 9 (1+8). Requirements Follow the format of the examples below. Make sure your variables and parameters have the correct data types. You must implement a recursive…arrow_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
- PROGRAMMING LANGUAGE: C++ How do we differentiate between prefix and postfix increment operator while overloading them?arrow_forwardpointers as Arguments:In the C programming language there is no pass-by-reference syntax to passa variable by reference to a function. Instead a variable is passed by pointer(just to be confusing, sometimes passing by pointer is referred to as pass byreference). This Practice Program asks you to do the same thing as C.Here is the header for a function that takes as input a pointer to an integer:1. void addOne (int ∗ptrNum )Complete the function so it adds one to the integer referenced by ptrNum.Write a main function where an integer variable is defined, give it an initialvalue, call addOne, and output the variable. It should be incremented by 1.arrow_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_forward
- Question 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_forwardHOME WORK 1-Write a steps to search for a node contain a given value in a S.L.L.L. its head is given by pointer variable ( First ) 2-Write Recursive function to count number of nodes in a given S.L.L.L. 3-Write a steps to count number of nodes contain odd number in a given S.L.L.L. its head is given by pointer variable ( First ). 4-Write a steps to test values stored in S.L.L.L. if it is in ascending order or not .arrow_forwardDefine the following funtion using C languagearrow_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