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.

Question 2 – Methods and Control Structures

Allotted time: 15 minutes (plus 5 minutes to submit)

This question involves the use of check digits, which can be used to help detect if an error has occurred when a number is entered or transmitted electronically.

The CheckDigit class is shown below. You will write one method of the CheckDigit class.

public class CheckDigit

{

/** Returns the check digit for num

* Precondition: The number of digits in num is between one and

six, inclusive.

* num >= 0

*/

public static int getCheck(int num)

{ /* implementation not shown */ }

/** Returns true if numWithCheckDigit is valid, or false

otherwise,

* as described in part (a)

* Precondition: The number of digits in numWithCheckDigit

is

* between two and seven, inclusive.

* numWithCheckDigit >= 0

*/

public static boolean

isValid(int numWithCheckDigit)

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

// There may be variables and methods not shown.

}

(a) Write the isValid method. The method returns true if its parameter numWithCheckDigit, which represents a number containing a check digit, is valid, and false otherwise. The check digit is always the rightmost digit of numWithCheckDigit.

The following table shows some examples of the use of isValid.

Method Call Return

Value Explanation

getCheck(159) 2 The check digit for 159 is 2.

isValid(1592) true


The number 1592 is a valid

combination of a number (159) and its

check digit (2).

isValid(1593) false

The number 1593 is not a valid

combination of a number (159) and its

check digit (3) because 2 is the check

digit for 159.Complete method isValid below. You must use getCheck appropriately

to receive full credit.

/** Returns true if numWithCheckDigit is valid, or false

* otherwise, as described in part (a)

* Precondition: The number of digits in numWithCheckDigit is

* between two and seven, inclusive.

* numWithCheckDigit >= 0

*/

public static boolean isValid(int numWithCheckDigit)

(b) A programmer wants to modify the CheckDigit class to keep track

of how many times a call to isValid is made with an incorrect check

digit. Any time a call to isValid is made with an incorrect check

digit, the count should be increased by one. 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 CheckDigit class

in order to support this modification. Do not write the program code

for this change

Respuesta :

Otras preguntas

Q&A Education