In: Computer Science
A lottery company sells scratch cards. In addition to the game itself, each contains a verification code. When a user wishes to claim a win, they enter the value they are claiming and the verification code into the system. The system then uses a secret key to determine the validity of the claim.
You are to write the verification method which will be passed the data entered by the claimant together with the secret key.
The verification method verifyWin takes three arguments, the claimValue in pounds as an int, the verificationCode from the card as a String and the secretKey as a String. The prizes available are £1, £3 and £5. It returns a Boolean value, true if the claim is valid, false if not.
To determine whether a verificationCode is valid, it is compared to the secretKey character by character.
A local variable prize (which you must create) is initially set to 0 and will represent the prize value in the verificationCode once the processing has completed.
Next an appropriate loop is entered to compare the characters in the verificationCode and the secretKey, one at a time in the order left to right.
Each time the code in the body of the loop executes, it examines the next pair of characters. If the characters at the current position match, the numerical position of the match is added to the prize. If they do not match, the numerical position is subtracted. (We take position to start at 1, whereas strings are indexed from 0.)
When all of the characters have been compared the value in prize contains the value of a valid claim for this card. It is compared to the claimValue to determine if the claim is valid. If they match, return true. If not return false.
The keys may be different lengths, but the secretKey will always be shorter than the verificationCode. Compare only as many positions as the shorter string
You will need the charAt() method to access the correct character.
You should assume that the inputs are always valid.
*********************************************************************************
/**
*Takes three arguments:-
* claimValue - an int value representing the value claimed by the
user in pounds
* verificationCode - A String representing the verification code
printed on the card
* secretKey - a String representing the secret key used to validate
the claim
*
* Iterates over the characters in the Strings adding the position
of matches
* and subtracting the position of mis-matches to determine the
prize value
* The claim is valid if the amount claimed is equal to the prize
value determined.
* Returns a boolean value true if the claim is valid and false
otherwise.
*/
public Boolean verifyWin(int claimValue, String verificationCode,
String secretKey)
{
//create and initialise your local variable prize here
// insert your loop here
// and return your value here
}
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions. Thank You ! ======================================================================
public boolean verifyWin(int claimValue, String
verificationCode, String secretKey) {
//create and initialise your local variable prize here
int prize = 0;
// insert your loop here
for (int i = 0; i < secretKey.length(); i++) {
if (verificationCode.charAt(i) == secretKey.charAt(i)) {
prize += i + 1;
} else {
prize -= (i + 1);
}
}
// and return your value here
return prize == claimValue;
}
}
===================================================================