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

This is a continuation of a program I did earlier as seen below in the pictures

ID: 3779915 • Letter: T

Question

This is a continuation of a program I did earlier as seen below in the pictures which I included for reference.
import java.util.Scanner; import java.util.Random;
public class Lab7b { public static void main (String[] args){
Scanner scnr = new Scanner(System.in); Random rand = new Random(107L); int numInputs = scnr.nextInt();
/* FIXME declare double array called numbers and assign to it the array returned from init(numInputs, rand) call. */
/* FIXME Write a statement that calls getStats passing numbers as the parameter */ /* FIXME Write a statement that calls print method passing numbers as the parameter */    }    /* FIXME Write initArray method here. * Create a new array the size of numInputs. * In a loop, use rand.nextDouble()*100 to fill the array. * Return the array. */       /* FIXME Write getStats method here. * Set min and max to the first value in the numbers array as initial values. * Loop through the array to find/calculate the min, max, average, and range */

/* FIXME Write the print method here that prints out the contents of the array with each element separated by a space. * This should be a familiar task. */    } } import java.util.Scanner; import java.util.Random;
public class Lab7b { public static void main (String[] args){
Scanner scnr = new Scanner(System.in); Random rand = new Random(107L); int numInputs = scnr.nextInt();
/* FIXME declare double array called numbers and assign to it the array returned from init(numInputs, rand) call. */
/* FIXME Write a statement that calls getStats passing numbers as the parameter */ /* FIXME Write a statement that calls print method passing numbers as the parameter */    }    /* FIXME Write initArray method here. * Create a new array the size of numInputs. * In a loop, use rand.nextDouble()*100 to fill the array. * Return the array. */       /* FIXME Write getStats method here. * Set min and max to the first value in the numbers array as initial values. * Loop through the array to find/calculate the min, max, average, and range */

/* FIXME Write the print method here that prints out the contents of the array with each element separated by a space. * This should be a familiar task. */    } }


In the window below are several FixME comments. They describe what to do. For example, if the statement indicates ...cals myMethod passing quakes as a parameter" then you know that portion will look something like myMethod(quakes) Recall that method calls come in two forms: those that are a statement in and of themselves, and those that return something that will be assigned to a variable. Here is the class outline structure: main() declares numlnputs and reads in a value from the user +prints "numlnputs is n', where n is the user input calls the three methods below prints "Returning prints .Using a random number generator to initialze array with n elements where nis the int value passed into into array" get stats prints "In getStats' declares variable for the min, max, average and range then prints out their intial values. print prints "in print" Sample output When input is The output exactly matches when input is 5 num Inputs is 5 Using a random number generator to initialize array with 5 elements Returning array In get stats Maximum 0.0 Minimum 0.0 Average 0.0 Range: 0 In print

Explanation / Answer

I have wrote the program according to your format, and you have already completed the program, but only a few lines were needed to add. I have added those lines and I have included a sample output.Save the file as "Lab7b.java", Compile as "javac Lab7b.java" and run as "java Lab7b".

Also I don't understand why the range variable in getStats was needed. If its the length of array, it was already available. Anyway I have wrote the program in that way. It was ambiguous though.

Hope it helps.

import java.util.Scanner;

import java.util.Random;

public class Lab7b {
public static void main (String[] args){
int numInputs;
Scanner scnr = new Scanner(System.in);
// read input from user
numInputs = scnr.nextInt();
System.out.println(" numInputs is " + numInputs + " ");

// System.out.print("Enter a number: "); /*This could be used as a prompt*/
Random rand = new Random();

/* FIXME declare double array called numbers and assign to it the array returned from init(numInputs, rand) call. */
double numbers[] = initArray(numInputs, rand);

/* FIXME Write a statement that calls getStats passing numbers as the parameter */
getStats(numbers);

/* FIXME Write a statement that calls print method passing numbers as the parameter */
print(numbers);
}

/* FIXME Write initArray method here.
* Create a new array the size of numInputs.
* In a loop, use rand.nextDouble()*100 to fill the array.
* Return the array.
*/
public static double[] initArray(int n, Random rand){
double arr[] = new double[n];
System.out.println("Using a random number generator to initialize array with " + n + " elements ");
for(int i=0;i<n;i++)
{
arr[i] = rand.nextDouble()*100;
}
System.out.println("Returning array ");
return arr;
}

/* FIXME Write getStats method here.
* Set min and max to the first value in the numbers array as initial values.
* Loop through the array to find/calculate the min, max, average, and range
*/
public static void getStats(double arr[]){
System.out.println("In getstats ");
double min = arr[0];
double max = arr[0];
double average;
int range = arr.length; // the range variable is pointless as the range is already given as input
double total = 0; // total sum of the array
for(int i=0;i<arr.length;i++)
{
if(arr[i] < min)
min = arr[i];
if(arr[i] > max)
max = arr[i];
total = total + arr[i];
}
average = total/(arr.length); // average = total sum/number of items
System.out.println("Maximum : " + max + " ");
System.out.println("Minimum : " + min + " ");
System.out.println("Average : " + average + " ");
System.out.println("Range : " + range + " ");
}

/* FIXME Write the print method here that prints out the contents of the array with each element separated by a space.
* This should be a familiar task.
*/
public static void print(double arr[]){
System.out.println("In print ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i] + " ");
}
}

}
  

Sample Output:

5

numInputs is 5

Using a random number generator to initialize array with 5 elements

Returning array

In getstats

Maximum : 79.45002255426658

Minimum : 26.34463358513559

Average : 54.98371850438808

Range : 5

In print

26.34463358513559 52.80472200339179 59.00858825883547 79.45002255426658 57.31062612031099

  
  
  

  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote