Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!