Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Extend the Arithmetic example from the course notes to include classes Minus, Mult and Divide. Each of these should override the eval() method to

image text in transcribed
image text in transcribed
1. Extend the Arithmetic example from the course notes to include classes Minus, Mult and Divide. Each of these should override the eval() method to perform the correct operations. For each class, including the ones I've already provided, write a toString() method that returns a String containing a text version of the part of the arithmetic problem in the subtree rooted at the current node. For example, if the expression tree is 0 0 o ooo calling toString() for the "+" node should produce the String ((3 * 5) + (1/4)). The recommended way to do this is the same as the way the eval() method works: recursively. For example, if the top node is a Plus object, it creates a String with an opening parenthesis "C, then calls its left child to return a String (which is then concatenated), then concatenates " + , then calls its right child to return a string (which is concatenated), then concatenates")". The method toString() returns the resultant String. Rewrite the TestArithmetic class so that its main() method produces five random arithmetic problems of opening parenthesis C, then calls its left child to return a String (which is then concatenated), then concatenates + , then calls its right child to return a string (which is concatenated), then concatenates)". The method toString() returns the resultant String. Rewrite the TestArithmetic class so that its main() method produces five random arithmetic problems of length four (i.e., having four Consts separated by three operators). Constants should be integers chosen at random in the range [1, 20). Operators should be chosen randomly from the set {+, -, *,1}. For each arithmetic problem, you should print out the result of your toString() method applied to the root node, then an equals sign, then the result of eval(), producing something like this: ((16.0/ 16.0) - (7.0 * 17.0)) = -118.0 ((4.0+ 17.0)/(12.0/ 12.0)) = 21.0 (14.0 - 4.0) + (1.0 / 20.0)) = 10.05 ((18.0 + 15.0)/(5.0/ 10.0)) = 66.0 ((4.0/5.0) + (5.0 * 7.0)) = 35.8 You might find it convenient (as I did) to create methods randOperator () and randConstant () within TestArithmetic. You should submit your .java files to D2L. 1. Extend the Arithmetic example from the course notes to include classes Minus, Mult and Divide. Each of these should override the eval() method to perform the correct operations. For each class, including the ones I've already provided, write a toString() method that returns a String containing a text version of the part of the arithmetic problem in the subtree rooted at the current node. For example, if the expression tree is 0 0 o ooo calling toString() for the "+" node should produce the String ((3 * 5) + (1/4)). The recommended way to do this is the same as the way the eval() method works: recursively. For example, if the top node is a Plus object, it creates a String with an opening parenthesis "C, then calls its left child to return a String (which is then concatenated), then concatenates " + , then calls its right child to return a string (which is concatenated), then concatenates")". The method toString() returns the resultant String. Rewrite the TestArithmetic class so that its main() method produces five random arithmetic problems of opening parenthesis C, then calls its left child to return a String (which is then concatenated), then concatenates + , then calls its right child to return a string (which is concatenated), then concatenates)". The method toString() returns the resultant String. Rewrite the TestArithmetic class so that its main() method produces five random arithmetic problems of length four (i.e., having four Consts separated by three operators). Constants should be integers chosen at random in the range [1, 20). Operators should be chosen randomly from the set {+, -, *,1}. For each arithmetic problem, you should print out the result of your toString() method applied to the root node, then an equals sign, then the result of eval(), producing something like this: ((16.0/ 16.0) - (7.0 * 17.0)) = -118.0 ((4.0+ 17.0)/(12.0/ 12.0)) = 21.0 (14.0 - 4.0) + (1.0 / 20.0)) = 10.05 ((18.0 + 15.0)/(5.0/ 10.0)) = 66.0 ((4.0/5.0) + (5.0 * 7.0)) = 35.8 You might find it convenient (as I did) to create methods randOperator () and randConstant () within TestArithmetic. You should submit your .java files to D2L

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

New Trends In Databases And Information Systems Adbis 2019 Short Papers Workshops Bbigap Qauca Sembdm Simpda M2p Madeisd And Doctoral Consortium Bled Slovenia September 8 11 2019 Proceedings

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Robert Wrembel ,Mirjana Ivanovic ,Johann Gamper ,Mikolaj Morzy ,Theodoros Tzouramanis ,Jerome Darmont

1st Edition

3030302776, 978-3030302771

More Books

Students also viewed these Databases questions