Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question involves computing an integer based on two input strings. You will write the compute method of the following Operator class. public class Operator

This question involves computing an integer based on two input strings. You will write the compute method
of the following Operator class.
public class Operator
{
/** Returns an integer value based on the input string */
public static int transform(String str)
{/* implementation not shown */}
/** Returns an integer based on two input strings, as described in part
(a)
* Precondition Precondition: input and op are not null.
* The length of input is even.
* op is either "$$","^^", or "##".
*/
public static int compute(String input, String op)
{/* to be implemented in part (a)*/}
// There may be variables and methods that are not shown.
}
(a) Write the compute method of the Operator class. The method calculates an integer value based on an
input string with even length. The first half and the second half of input are transformed into numeric values
by calls to transform. The numeric values are then used in a calculation based on the mathematical operation
represented by the string, op. The method returns the calculated result.
The helper method transform has been provided. The method returns an integer value based on its String
parameter. For example, the method call transform("pear") might return the int value 3.
The mathematical operations represented by op are described in the following table. Assume that value1 is
the numeric value obtained by calling transform on the first half of input and value2 is the numeric
value obtained by calling transform on the second half of input.
Test Booklet
Mock Exam Part 2
Page 10 of 14 AP Computer Science A
Value of op Mathematical Operation Description
"$$" Addition value1 plus value2
"^^" Multiplication value1 times value2
"##" Subtraction value1 minus value2
For example, assume that calls to transform are made from the Operator class and that
transform("orang") returns 2 and transform("epear") returns 6. Then the method call
Operator.compute("orangepear","$$") should return 8.
As another example, the method call Operator.compute("orangepear","##") should return -4.
Complete method compute. You must use transform appropriately to receive full credit.
/** Returns an integer based on two input strings, as described in part
(a)
* Precondition Precondition: input and op are not null.
* The length of input is even.
* op is either "$$","^^", or "##".
*/
public static int compute(String input, String op)
(b) A programmer wants to modify the Operator class so that the compute method has a single String
parameter containing both input and op.
Write a description of how you would change the Operator class in order to support this modification. Do not
write the program code for this change.
Make sure to include the following in your response.
Identify any new or modified variables or methods.
Describe, for each new or revised variable or method, how it would change or be implemented, including
visibility and type.

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

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

Describe ERP and how it can create efficiency within a business

Answered: 1 week ago

Question

10-9 How have social technologies changed e-commerce?

Answered: 1 week ago