Working with Java, NetBeans IDE 8.2 Q1: Write a method called testArray that acc
ID: 3823452 • Letter: W
Question
Working with Java, NetBeans IDE 8.2
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
HI, I have implemeneted all parts of Q1. and tested as well as.
Please repost others questions in separate post.
import java.util.Random;
public class TestArray {
public static void testArray(int[] arr){
//a.
System.out.println("printing all elements at even index");
for(int i=0; i<arr.length; i=i+2)
System.out.print(arr[i]+" ");
System.out.println();
//b
System.out.println("printing every 5th elements");
for(int i=0; i<arr.length; i=i+5)
System.out.print(arr[i]+" ");
System.out.println();
//c
System.out.println("printing all elements except first and last");
for(int i=1; i<arr.length-1; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String[] args) {
Random rand = new Random();
int[] arr = new int[20];
for(int i=0; i<arr.length; i++)
arr[i] = rand.nextInt(101) + 100; //100-200
testArray(arr);
}
}
/*
Sample run:
printing all elements at even index
153 163 178 183 176 179 158 183 136 131
printing every 5th elements
153 105 179 164
printing all elements except first and last
127 163 107 178 105 183 118 176 138 179 147 158 165 183 164 136 117 131
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.