Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1: Write a method called testArray that accepts a one-dimensional array of any

ID: 3823397 • Letter: Q

Question

Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints the following summary information: a) all array elements at an even index b) every fifth element c) all elements but the first and last Write a program that demonstrates this method by using an array of 20 random integers from 100 to 200.

Q2: Write a method called displayGrid that accepts a two-dimensional array of integer values from 0 to 99 and prints this array an evenly spaced set of rows and columns. Write a program that demonstrates this method by using 6 row by 4 column array of random integers from 0 to 99.

Q3: Edit your displayGrid method so that it accepts a two-dimensional array of double values and a threshold value and displays a two-dimensional grid of only those values that are greater than the given threshold. Any values not over this threshold should be printed as spaces. Write a program that demonstrates this method by using 6 row by 4 column array of random integers from 0 to 99. For example, display only elements with a value greater than the 10

Explanation / Answer

1.

public class Tweet{
   public static void testArray (int arr[]){
       System.out.println("All elements at even index: ");
       for(int i = 0; i < arr.length; i += 2){
           System.out.print(arr[i] + " ");
       }
       System.out.println(" Every 5th element: ");
       for(int i = 0; i < arr.length; i += 5){
           System.out.print(arr[i] + " ");
       }
       System.out.println(" All elements except first and last: ");
       for(int i = 1; i < arr.length - 1; i++){
           System.out.print(arr[i] + " ");
       }
   }
  
   public static void main(String args[]){
       int arr[] = new int[20];
       for(int i = 0; i < 20; i++){
           arr[i] = (int) (Math.random() * 100 + 100);
       }
       testArray(arr);
   }
}

2.

public class Tweet{
   public static void displayGrid(int arr[][]){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               if(arr[i][j] < 10) System.out.print(" ");
               System.out.print(arr[i][j] + " ");
           }
           System.out.println();
       }
   }
  
   public static void main(String args[]){
       int arr[][] = new int[6][4];
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               arr[i][j] = (int) (Math.random() * 100);
           }
       }
       displayGrid(arr);
   }
}

3.

public class Tweet{
   public static void displayGrid(int arr[][], int threshold){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               if(arr[i][j] > threshold){
                   if(arr[i][j] < 10) System.out.print(" ");
                   System.out.print(arr[i][j] + " ");
               }
               else{
                   System.out.print(" ");
               }
           }
           System.out.println();
       }
   }
  
   public static void main(String args[]){
       int arr[][] = new int[6][4];
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               arr[i][j] = (int) (Math.random() * 100);
           }
       }
       displayGrid(arr, 40);
   }
}