Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
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
bartleby

Concept explainers

Question
Book Icon
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.
            • Return “0”.
              • Define the method “isNumber()”.
                • If the given expression is number then, return the number itself.
            • 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.
    • Define the “main()” method.
      • Declare the method “launch()” to call the “start()” method eventually.

Blurred answer
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].
Knowledge Booster
Background pattern image
Computer Science
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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT