Initialize a Numerical Keypad
We wrote a Java method called initializedKeypad
that initializes a 2D array to represent a numerical keypad.
class NumericalKeypad {
private static int[][] keypad = //had to be removed because of liquid error but it would normally be the keypad;
public static void main(String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
if (keypad[i][j] >= 0) {
System.out.print(keypad[i][j] + " ");
} else {
System.out.print(" ");
}
if (keypad[i][j] == 0) {
break;
}
}
System.out.println();
}
}
}
NumericalKeypad test = new NumericalKeypad();
NumericalKeypad.main(null);
1 2 3
4 5 6
7 8 9
0
Reverse an Array
class ArrayReversal {
public static void reverseArray(int[] numberArray) {
for (int i = 0; i < 10; i++) {
System.out.print(numberArray[i] + " ");
}
for (int i = 0; i < 10; i++) {
System.out.print(numberArray[9-i] + " ");
}
}
public static void main(String[] args) {
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
reverseArray(numbers);
}
}
ArrayReversal.main(null);
10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
class RandomIntegers {
private static ArrayList<Integer> initializeRandomList(int n, int min, int max) {
ArrayList<Integer> randomList = new ArrayList<Integer>(); // initialize ArrayList
for (int i = 0; i < n; i++) {
int randomNumber = (int) (Math.random() * (max - min + 1)) + min;
randomList.add(randomNumber);
}
return randomList;
}
public static void main(String[] args) {
System.out.println(initializeRandomList(5, 1, 7));
System.out.println(initializeRandomList(10, 20, 100));
System.out.println(initializeRandomList(3, 3000, 100000));
}
}
RandomIntegers.main(null);
[2, 2, 4, 3, 2]
[49, 29, 29, 90, 73, 38, 85, 86, 29, 47]
[82352, 30836, 86627]