Arrays Assignment

advertisement
Programming & Arrays Assignment
1. You will be writing the method firstLast6. This method takes in an array and will return true if 6
appears as either the first or last element in the array. The array will be length 1 or more. The class is
named ArrayProblems1 (found on the website
Examples:
firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({3, 2, 1}) → false
2. You will be writing the method sameFirstLast. This method takes in an array and will return true if the
first number in the array and the last number are the same. The array will be length 1 or more. The
class is named ArrayProblems2 (found on the website)
Examples:
sameFirstLast({1, 2, 1}) → true
sameFirstLast({3, 1, 2, 3}) → true
sameFirstLast({3, 2, 1}) → false
3. You will be writing the method arraySum. This method takes in an array of integers and will return the
sum of all the integers in the array. The array length will be length 1 or more. The class is named
ArrayProblems3 (found on the website)
Examples:
arraySum({1, 2, 6}) → 9
arraySum ({6, 1, 2, 3}) → 12
arraySum ({3, 2, 1}) → 6
4. You will be writing the method countEvens. This method takes in an array of integers and will return
the number of even numbers that appear in the array. The array length will be 1 or more. The class is
named ArrayProblems4 (found on the website).
Note: the mod operator % could be useful in this situation.
5%2=1
4%2=0
19 % 2 = 1
20 % 2 = 0
Examples:
countEvens({1, 2, 6}) → 2
countEvens ({6, 1, 2, 3, 7, 10}) → 3
countEvens ({3, 2, 1}) → 1
5. Input an integer called size. Determine the primes from 2 through size by using the Sieve of
Eratosthenes (uses the first number found and look for multiples to cancel out. Continue until we run
out of numbers to check). Output all the primes found at the end.
 Use a boolean array called primes.
 Initialize all elements of the array to true (t).
 Don't use the elements primes[0] or primes[1].
 Mark non-prime numbers as false.
Here are the first few lines of the process:
... the array is initiallized with all elements having the value true:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
T T T T T T T T T T T T T T T T T T T T T T T T
... array elements which are multiples of 2 are set to the value false:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
T T f T f T f T f
T f
T f
T f
T f
T f
T f
T f
T
... array elements which are multiples of 3 are set to the value false:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
T T f T f T f f f
T f
T f
f
f
T f
T f
f
f
T f
T
... array elements which are multiples of 5 are set to the value false:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
T T f T f T f f f
T f
T f
f
f
T f
T f
f
f
T f
f
... ultimately the entire array has been adjusted so that only primes remain as true. Output the
numbers that correspond to these elemenmts:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
T T f T f T f f f
T f
T f
f
f
T f
T f
f
f
T f
f
Implement this procedure in the main method. Name the class and file ArrayProblem5
6. Four-digit numbers are used to transmit target coordinate data over an insecure phone line. Your
program encrypts the four-digit numbers using the following protocol:
 replace each digit with the result of adding 7 to the digit and getting the remainder after dividing
the new value by 10.
 swap the first digit with the third
 swap the second digit with the fourth
 decryption is achieved by reversing this process
The user actually types a five-digit number:
 If the first digit entered is a 1, then the rest of the number is to be encrypted and the four-digit
result is output.
 If the first digit entered is a 2, the the rest of the number is to be decrypted and the four-digit result
is output.
 Tell the user if their input is invalid.
 Stop encrypting/decrypting when 99999 is entered.
Use the java file ArrayProblems6 (found on the website). You will implement the encrypt and the
decrypt functions as well as the main method that will accept the input and perform the desired
encrypt or decrypt function.
Download