Java Reference
In-Depth Information
1. Prompt and read the length of the secret code.
2. Create an array of appropriate length to store the secret code.
3. Read and store the secret code into an array.
4. Read the length of the copy.
5. If the length of the secret code and its copy are the same, compare
the codes. Otherwise, print an error message.
To simplify the definition of the method main , let us write the method, readCode ,
to read the secret code and another method, compareCode , to compare the codes.
Next, we describe these two methods.
readCode The method readCode reads and stores the secret code in an array. This method has
one parameter: an array to store the secret code. The definition of the method
readCode is as follows:
public static void readCode( int [] list)
{
System.out.print("Enter the secret code: ");
for ( int count = 0; count < list.length; count++)
list[count] = console.nextInt();
System.out.println();
}
compareCode This method compares the secret code with its copy and prints an appropriate
message. Therefore, it must have access to the array containing the secret code.
Thus, this method has one parameter: the array containing the secret code. This
discussion translates into the following algorithm for the method compareCode :
a. Declare the variables.
b. Set a boolean variable codeOk to true .
c. Read the length of the copy of the secret code.
d. If the length of the secret code and its copy are not the same,
output an appropriate error message and terminate the method.
e. Output the heading: Code Digit Code Digit Copy
f. For each digit in the secret code:
i. Read the next digit of the copy of the secret code.
ii. Output the corresponding digits from the secret code and its
copy.
iii.
If the corresponding digits are not the same, output an error
message and set the boolean variable codeOk to false .
 
Search WWH ::




Custom Search