subject

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

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 12:00, dani19cano
The following function returns a string of length n whose characters are all 'x'. give the order of growth (as a function of n) of the running time. recall that concatenating two strings in java takes time proportional to the sum of their lengths. public static string f(int n) { if (n == 0) return ""; if (n == 1) return "x"; return f(n/2) + f(n - n/2); } options: a) constant b) logarithmic c) linear d) linearithmic e)quadratic f)cubic g) exponential
Answers: 2
image
Computers and Technology, 23.06.2019 07:50, kmart4062
Apython programming question: assume s is a string of lower case characters. write a program that prints the number of times the string 'bob' occurs in s. for example, if s = 'azcbobobegghakl', then your program should print number of times bob occurs is: 2
Answers: 3
image
Computers and Technology, 23.06.2019 09:00, opgbadwolf5
What provides an array of buttons for quick access to commonly used commands and tools
Answers: 1
image
Computers and Technology, 23.06.2019 18:30, emmaishere69
List 3 items that were on kens resume that should have been excluded
Answers: 1
You know the right answer?
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
No...

Questions in other subjects:

Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01
Konu
Mathematics, 16.09.2020 01:01