Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO PART B ONLY Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Notes: Assume that the classes

PLEASE DO PART B ONLY

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

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

2. This question involves validating a String that is being used as an identifier. You will write the isValid method of the following Authenticate class.

public class Authenticate

{

/** Returns true if the input string has some property and returns false

* otherwise

*/

public static boolean hasProperty(String str)

{ /* implementation not shown */ }

/** Returns true if the input string passes validation and returns false otherwise, as

* described in part (a)

* Precondition: input is not null.

*/

public static boolean isValid(String input)

{ /* to be implemented in part (a) */ }

// There may be variables and methods that are not shown.

}

(a) Write the isValid method, which validates an input string and returns a boolean value based on the following conditions. Every non-overlapping 3-character grouping of the input string is verified individually using the helper method hasProperty.

  • If the length of the input string is not divisible by 3, the isValid method returns false.
  • If the input string contains at least two 3-character groupings for which hasProperty returns true, the isValid method returns true.
  • If there are fewer than two 3-character groupings for which hasProperty returns true, the isValid method returns false.

The following table shows some examples of the intended behavior of isValid. Assume that all calls are made from within the Authenticate class.

Call to

isValid

Possible Values Returned by

hasProperty

isValid Return

Value

isValid("butterfly")

hasProperty("but") returns true

hasProperty("ter") returns false

hasProperty("fly") returns false

false
isValid("turtle")

hasProperty("tur") returns true

hasProperty("tle") returns true

true
isValid("bear") false (because the length of "bear" is not divisible by 3)

You must use hasProperty appropriately to receive full credit.

Complete method isValid.

/** Returns true if the input string passes validation and returns false otherwise, as

* described in part (a)

* Precondition: input is not null.

*/

public static boolean isValid(String input)

(b) A programmer wants to modify the Authenticate class so that, in the isValid method, the rules for character groupings of 3 can vary between method calls. For example, in one call to isValid, the rules might check for divisibility by 4 with 4-character groupings, and in another call to isValid, the rule might check for divisibility by 5 with 5-character groupings. The programmer would like to implement this change without making any changes to the signature of the isValid method or overloading isValid.

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

Type or paste question here

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

Students also viewed these Databases questions