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

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 02:30
Experimental data that is expressed using numbers is said to be
Answers: 1
question
Computers and Technology, 23.06.2019 04:10
2pointswho was mikhail gorbachev? oa. a russian leader who opposed a coupob. a polish leader who founded the labor union "solidarityoc. a soviet leader who called for a closer relationship with the unitedstates, economic reform, and a more open societyd. a soviet leader who called for more oppression in the soviet union
Answers: 3
question
Computers and Technology, 23.06.2019 19:00
Write a program that displays the following menu: geometry calculator 1. calculate the area of a circle 2. calculate the area of a rectangle 3. calculate the area of a triangle 4. quit enter your choice (1-4): if the user enters 1, the program should ask for the radius of the circle and then display its area. use the following formula: area = ď€(the square of r) use 3.14159 for ď€ and the radius of the circle for r. if the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area. use the following formula: area = length * width if the user enters 3, the program should ask for the length of the triangle’s base and its height, and then display its area. use the following formula: area = base * height * .5 if the user enters 4, the program should end. input validation: display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. note: if the user enters an improper menu choice (1-4), the program prints "the valid choices are 1 through 4. run the program again and select one of those." if the user enters a negative radius, the program prints "the radius can not be less than zero." if the user enters a negative value for height or base, the program prints "only enter positive values for base and height."
Answers: 1
question
Computers and Technology, 24.06.2019 14:30
When workers demonstrate patience, are able to manage there emotions, and get along with other employees, which skills are being displayed?
Answers: 1
You know the right answer?
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
No...
Questions
question
Mathematics, 08.12.2020 17:50
question
Health, 08.12.2020 17:50
question
History, 08.12.2020 17:50
question
World Languages, 08.12.2020 17:50
Questions on the website: 13722367