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

The code template contains tasks numbered 1 through 5. Fill in the missing Java

ID: 3932754 • Letter: T

Question

The code template contains tasks numbered 1 through 5. Fill in the missing Java code.

public class Lab5a {
public static void main(String[] args) {
// System.out.println("Program 5, Your Name, mascxxxx"); // Replace with your name and mascID

//--------------------------------------------------------------------------------
//1. Display the value of element 6 of array f
//--------------------------------------------------------------------------------
int[] f = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
  
//TODO: Print element 6 of array f
  
System.out.println();
  
//--------------------------------------------------------------------------------
// 2. Initialize each element of g to 8
//--------------------------------------------------------------------------------
int[] g = new int[7];
for (int i=0; i < g.length; ++i)
// TODO: Fill array g

for (int i=0; i < g.length; ++i)
System.out.println("g[" + i + "] is " + g[i]);
System.out.println();

//--------------------------------------------------------------------------------
// 3. Total 100 elements of floating point array c filled with values 1 to 100
//--------------------------------------------------------------------------------
float[] c = new float[100];
double total = 0.0;
  
//TODO: Fill and sum array c
  
System.out.println("Total is " + total);
System.out.println("Average is " + (total / c.length));
System.out.println();
  
//--------------------------------------------------------------------------------
// 4. Copy 11-element array c into the first 11 positions of 34-element array b.
//--------------------------------------------------------------------------------
  
// TODO: create array b and copy c's elements into it
  
for (int i=0; i < b.length; ++i)
System.out.print(b[i] + " ");
System.out.println();
System.out.println();
  
//--------------------------------------------------------------------------------
// 5. Determine and print the largest and smallest values contained in 99-element
// floating-point array w
//--------------------------------------------------------------------------------
Random rand = new Random(1025L);
  
//TODO: Declare array w with 99 elements
  
double min = 5001;
double max = -5001;
// Filling array w with random numbers between -5000 and +5000
for (int i=0; i < w.length; ++i)
w[i] = rand.nextDouble() * 10000 - 5000 ;

// TODO: Find min and max
  
System.out.println("Smallest value is " + min);
System.out.println("Largest value is " + max);
}
}

Explanation / Answer

Question 1:

//--------------------------------------------------------------------------------
//1. Display the value of element 6 of array f
//--------------------------------------------------------------------------------
int[] f = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
  
//TODO: Print element 6 of array f

System.out.println(f[2]);
  
System.out.println();

Question 2:

//--------------------------------------------------------------------------------
// 2. Initialize each element of g to 8
//--------------------------------------------------------------------------------
int[] g = new int[7];
for (int i=0; i < g.length; ++i)
// TODO: Fill array g

g[i] = 8;

for (int i=0; i < g.length; ++i)
System.out.println("g[" + i + "] is " + g[i]);
System.out.println();

Question 3:

//--------------------------------------------------------------------------------
// 3. Total 100 elements of floating point array c filled with values 1 to 100
//--------------------------------------------------------------------------------
float[] c = new float[100];
double total = 0.0;
  
//TODO: Fill and sum array c

for(int i=0; i<100; i++){

c[i] = i+1;

}
  
System.out.println("Total is " + total);
System.out.println("Average is " + (total / c.length));
System.out.println();

Question 4:

   //--------------------------------------------------------------------------------
// 4. Copy 11-element array c into the first 11 positions of 34-element array b.
//--------------------------------------------------------------------------------
  
// TODO: create array b and copy c's elements into it
int b[] = new int[11];

  for (int i=0; i < b.length; ++i){

b[i] = c[i];
}

for (int i=0; i < b.length; ++i)
System.out.print(b[i] + " ");
System.out.println();
System.out.println();
  

Question 5:

//--------------------------------------------------------------------------------
// 5. Determine and print the largest and smallest values contained in 99-element
// floating-point array w
//--------------------------------------------------------------------------------
Random rand = new Random(1025L);
  
//TODO: Declare array w with 99 elements
  
double min = 5001;
double max = -5001;
// Filling array w with random numbers between -5000 and +5000
for (int i=0; i < w.length; ++i)
w[i] = rand.nextDouble() * 10000 - 5000 ;

// TODO: Find min and max
for (int i=0; i < w.length; ++i){

if(max < w[i] )

max = w[i] ;

if(min > w[i] )

min = w[i] ;

}
      

System.out.println("Smallest value is " + min);
System.out.println("Largest value is " + max);
}
}