Please write comments on this program explaining what each method array and sent
ID: 3865636 • Letter: P
Question
Please write comments on this program explaining what each method array and sentinel does/is. Thank you.
import java.util.Scanner;
public class Lab12
{
public static void main(String[] args) {
final int pSize = 10;
int[] sized = { -51, -1, 5, 21, 36, 58, 64, 128, 256, 512};
int[] sent = { 1, 3, 5, 7, 11, 13, 17, 0, 6, 8 };
System.out.print(" The sized array: ");
printSizedArray(sized, pSize);
System.out.print(" The same sized array reversed: ");
revArray(sized, pSize);
printSizedArray(sized, pSize);
System.out.print(" The sentinel terminated array: ");
printSentArray(sent, 0);
System.out.print(" The same sentinel terminated array doubled: ");
doubArray(sent);
printSentArray(sent, 0);
System.out.println("");
}
public static void printSizedArray(int[] arr, final int eSize) {
for (int i = 0; i < eSize; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
}
public static void printSentArray(int[] arr, int sentVal) {
for (int i = 0; arr[i] != sentVal; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
}
public static void revArray(int[] arr, final int eSize) {
for (int i = 0, j = eSize - 1; i <= eSize / 2; ++i, --j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
public static void doubArray(int[] arr) {
for (int i = 0; arr[i] != 0; i++)
arr[i] = 2 * arr[i];
}
}
Explanation / Answer
import java.util.Scanner;
public class Lab12
{
public static void main(String[] args) {
final int pSize = 10;
int[] sized = { -51, -1, 5, 21, 36, 58, 64, 128, 256, 512};
int[] sent = { 1, 3, 5, 7, 11, 13, 17, 0, 6, 8 };
System.out.print(" The sized array: ");
printSizedArray(sized, pSize);
System.out.print(" The same sized array reversed: ");
revArray(sized, pSize);
printSizedArray(sized, pSize);
System.out.print(" The sentinel terminated array: ");
printSentArray(sent, 0);
System.out.print(" The same sentinel terminated array doubled: ");
doubArray(sent);
printSentArray(sent, 0);
System.out.println("");
}
// prints the array from index 0 to eSize
public static void printSizedArray(int[] arr, final int eSize) {
for (int i = 0; i < eSize; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
}
// prints the array from index 0 to sentVal
public static void printSentArray(int[] arr, int sentVal) {
for (int i = 0; arr[i] != sentVal; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
}
//reverses the array from middle of the array
public static void revArray(int[] arr, final int eSize) {
for (int i = 0, j = eSize - 1; i <= eSize / 2; ++i, --j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
//doubles the value of every array element
public static void doubArray(int[] arr) {
for (int i = 0; arr[i] != 0; i++)
arr[i] = 2 * arr[i];
}
}