113k views
9 votes
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

4. This question involves generating a String that will be used as an identifier. You will write the generateID method of the following Identifier class.

public class Identifier
{
/** Encodes a string as an integer and returns the encoded int value */
public static int encodeToNumber(String str)
{ /* implementation not shown */ }

/** Returns an identifier string based on an input string, as described in part (a)
* Precondition: input is not null.
*/
public static String generateID(String input)
{ /* to be implemented in part (a) */ }

// There may be variables and methods that are not shown.
}
(a) Write the generateID method, which is used to transform an input string into a string that can be used as an identifier. The method creates and returns the identifier string based on the following rules.

If the length of the input string is not divisible by 4 , the method returns the string "error".
Every non-overlapping 4 -character grouping of the input string is encoded as an integer using the helper method encodeToNumber. The sum of all the encoded values is calculated.
If the sum is greater than 100 , the method returns the original input string with "3" appended.
Otherwise, the method returns the original input string with "X" appended.
The following table shows some examples of calls to the generateID method. Assume that all calls occur in the Identifier class.

Call to generateID
Possible Values Returned by

encodeToNumber

generateID

Return Value

generateID("treebook")
encodeToNumber("tree")

returns 17

encodeToNumber("book")

returns 2

"treebookX"​
generateID("doordesklion")
encodeToNumber("door")

returns 56

encodeToNumber("desk")

returns 35

encodeToNumber("lion")

returns 86

"doordesklion3"
generateID("today")
"error" (because

the length of

"today" is not

divisible by 4 )

Complete method generateID. You must use encodeToNumber appropriately to receive full credit.

/** Returns an identifier string based on an input string, as described in part (a)
* Precondition: input is not null.
*/
public static String generateID(String input)
BoldItalicUnderlineBullet listNumbered listImage (12 image limit)
Edit imageView imageDelete image
0 / 10000 Word Limit
Question 2
(b) A programmer wants to modify the Identifier class to keep track of how many times a call to generateID returns "error". The programmer would like to implement this change without making any changes to the signatures of generateID or encodeToNumber or overloading either method.

Write a description of how you would change the Identifier 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.

User Tunisha
by
3.2k points

1 Answer

3 votes

Answer: To modify the Identifier class to keep track of how many times generateID returns "error", a new variable, say errorCount, can be added to the class as a private static integer variable to store the count. This variable can be initialized to 0 in the class constructor.

Next, in the generateID method, before returning "error", increment errorCount by 1. This would keep track of the number of times "error" is returned by the generateID method.

Finally, a new method, say getErrorCount, can be added to the class to return the errorCount value. This method can be a public static method that simply returns the errorCount value.

This way, the programmer can track the number of times generateID returns "error" without making any changes to the signatures of generateID or encodeToNumber or overloading either method.

User Aarthi
by
2.9k points