import java.util.Scanner; public class Lab12 { //This method takes an array of i
ID: 3568381 • Letter: I
Question
import java.util.Scanner; public class Lab12 { //This method takes an array of integers and the size of the array as parameters //and returns how the elements of the elements of the array separated by commas inside brackets. public String arrayToString(int[] a, int size) { String s = "["; for (int i=0; i<size; i++) if (i==size-1) s += a[i]; else s += a[i] + ", "; return s + "]"; } //This method takes an array of integers and the size of the array as parameters // and returns the minimum element in the array public int min(int[] a, int size) { int min; if (size > 0) min = a[0]; else min = 0; for (int i=1; i<size; i++) if (a[i]<min) min=a[i]; return min; } //WRITE THE FOLLOWING METHOD int max(int[], int) //See the min method above as an example public ??? ???(int[] a, int size) { int max; if (size > 0) max = a[0]; else max = 0; for (int i=1; i<size; i++) // Check if the <i>th value of <a> is greater than <max>, if (??? > ???) // Assign <i> th value of <a> to <max> max=???; // return max; //--> } //This method takes an array of integers and the size of the array as parameters // and returns the sum of the elements in the array public int sum(int[] a, int size) { int sum=0; for (int i=0; i<size; i++) sum += a[i]; return sum; } public double average(int[] a, int size) { //If the <size> is zero if(???){ // return zero //--> } else { //return the sum of elements (call the sum() method above) divided by <size>. return (double)???/???; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); Lab12 myObject= new Lab12(); //Create an array of integers called 'a' of length 10 that will hold user input. ??? ???= new int[10]; //Declare an integer called 'size' and initialize it to 0. This will be the number //integers the user enters. //--> //A boolean for rather the user has entered all the numbers they want into the array. boolean quit = false; // The conditions for this loop are: // 1. Check if <i> is less than 10 and // 2. Check if <quit> is false for(int i=0;??? & ???;i++) { System.out.print("Enter an integer into the array (0 to quit): "); //Use the Scanner to read in the next integer and store it into the <i>th position of the array <a> ???= scan.nextInt(); //If the number at index <i> of <a> is not zero then increase <size> by 1 if(??? != 0){ //--> } //Otherwise set the <quit> to true else{ //--> } } //Call arrayToString method below with the integer array <a> and <size> as inputs. System.out.println(" The array: " + myObject.???); System.out.println(" Computing the statistics of the array"); System.out.println("----------------------------------------"); // Print out the minimum by calling min method with <a> and <size> as inputs System.out.println("The smallest number in the array: " + myObject.???); //Print out the largest number as above by calling myObject.max method with <a> and <size> as inputs System.out.println(???); //Print out the average to 2 decimal points by calling the average method with <a> and <size> as inputs System.out.println("The average of the numbers in the array is: " + myObject.???); } }
Explanation / Answer
Here you go :)
//Lab12 class
import java.util.Scanner;
public class Lab12 {
public String arrayToString(int[] a, int size) {
String s = "[";
for (int i = 0; i < size; i++)
if (i == size - 1)
s += a[i];
else
s += a[i] + ", ";
return s + "]";
}
public int min(int[] a, int size) {
int min;
if (size > 0)
min = a[0];
else
min = 0;
for (int i = 1; i < size; i++)
if (a[i] < min)
min = a[i];
return min;
}
public int max(int[] a, int size) {
int max;
if (size > 0)
max = a[0];
else
max = 0;
for (int i = 1; i < size; i++)
if (a[i] > max)
max = a[i];
return max;
}
public int sum(int[] a, int size) {
int sum = 0;
for (int i = 0; i < size; i++)
sum += a[i];
return sum;
}
public double average(int[] a, int size) {
if (size == 0) {
return 0;
} else {
return (double) sum(a, size) / size;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Lab12 myObject = new Lab12();
int a[] = new int[10];
int size = 0;
boolean quit = false;
for (int i = 0; i < 10 && !quit; i++) {
System.out.print("Enter an integer into the array (0 to quit): ");
a[i] = scan.nextInt();
if (a[i] != 0) {
size++;
}
else {
quit = true;
}
}
System.out.println(" The array: " + myObject.arrayToString(a, size));
System.out.println(" Computing the statistics of the array");
System.out.println("----------------------------------------");
System.out.println("The smallest number in the array: "
+ myObject.min(a, size));
System.out.println("The biggest number in the array: "
+ myObject.max(a, size));
System.out.println("The average of the numbers in the array is: "
+ myObject.average(a, size));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.