subject

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)
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.

ansver
Answers: 2

Another question on Advanced Placement (AP)

question
Advanced Placement (AP), 23.06.2019 14:00
Which of the following statements is not true about regions? a.) regions have fixed boundaries that are determined by the region's inhabitants.b.) a region can be defined by physical characteristics, such as climate, landform, and vegetation.c.) regions can change over time.d.) a region can be defined by human characteristics, such as language, government, and economic activity.e.) the same location can be considered part of multiple regions.
Answers: 1
question
Advanced Placement (AP), 25.06.2019 15:30
Which of the following types of stars had the smallest initial mass? which of the following types of stars had the smallest initial mass?
Answers: 1
question
Advanced Placement (AP), 25.06.2019 19:10
If a system of linear equations has the unique solution (2, โˆ’3), then its graph consists of what? parallel lines lines that intersect at a single point identical lines a y-intercept at โˆ’3
Answers: 1
question
Advanced Placement (AP), 25.06.2019 22:30
The annual earnings of a typical investor are question options: $1,000,000 to $10,000,000. $100,000 to $500,000. $60,000 to $200,000. $40,000 to $80,000.
Answers: 1
You know the right answer?
Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Un...
Questions
question
Mathematics, 27.08.2019 06:30
question
English, 27.08.2019 06:30
question
Mathematics, 27.08.2019 06:30
question
History, 27.08.2019 06:30
question
Mathematics, 27.08.2019 06:30
Questions on the website: 13722360