· 7 years ago · Dec 18, 2018, 10:12 PM
1 /**
2 * Takes three arguments:-
3 * claimValue - an int value representing the value claimed by the user in pounds
4 * verificationCode - A String representing the verification code printed on the card
5 * secretKey - a String representing the secret key used to validate the claim
6 *
7 * Iterates over the characters in the Strings adding the position of matches
8 * and subtracting the position of mis-matches to determine the prize value
9 * The claim is valid if the amount claimed is equal to the prize value determined.
10 * Returns a boolean value true if the claim is valid and false otherwise.
11 */
12 public boolean verifyWin(int claimValue, String verificationCode, String secretKey)
13 {
14 //create and initialise the prize variable to zero here
15 int prize = 0;
16 boolean result = false;
17 int iterations = 0;
18 // insert your loop here
19 if (verificationCode.length() > secretKey.length())
20 {
21 iterations = secretKey.length();
22 }
23 else
24 {
25 iterations = verificationCode.length();
26 }
27 for (int i = 0; i < iterations; i++)
28 {
29 if (verificationCode.charAt(i) == secretKey.charAt(i))
30 {
31 prize = prize + i++;
32 }
33 else
34 {
35 prize = prize - i++;
36 }
37 }
38 // and return your value here
39 if (prize == claimValue)
40 {
41 result = true;
42 }
43 return result;
44 }