Grading Method Points (0-2) FillWithRandomNumbers printArray computeAverage fold
ID: 3912015 • Letter: G
Question
Grading Method Points (0-2) FillWithRandomNumbers printArray computeAverage foldArray main Total: (10 points)* 10 100 Grading: Missing, does not compile, or mostly wrong method: 0 point Methods that is partly right: 1 point Methods that compile and is correct except for very small logical error: 20 points PROBLEM Complete the following program by implementing the given methods public class LabExam Int@ list new int[10); an array of 10 random integers with values in the range 10 to 50 (inclusive) // Generate /I public static filWithRAndomNumbers(int list) / ills the array list with random integers fillWithRandomNumbers(list); list" array (5 values per line separated by blank space) ll public static void printAray(int] Ist) i prints the values in the array list // Print "Explanation / Answer
Program
import java.util.Random;
public class RandomNumber {
public static void main(String[] args)
{
int[] list = new int[10];
fillWithRandomNumbers(list);
System.out.println("The original array is :");
printArray(list);
System.out.println("Average is: " + computeAverage(list));
int[] newList = foldArray(list);
System.out.println("The folded array is :");
printArray(newList);
}
public static void fillWithRandomNumbers(int[] values)
{
int min = 10;
int max = 50;
Random generator = new Random();
for (int i = 0; i < 10; i++)
{
values[i] = generator.nextInt(max-min+1)+min;
}
}
public static void printArray(int[] list)
{
for (int i = 0; i < list.length / 2; i++)
{
System.out.print(" "+list[i]);
}
System.out.println();
for (int i = list.length / 2; i < list.length ; i++)
{
System.out.print(" "+list[i]);
}
System.out.println();
}
public static double computeAverage(int[] list)
{
double avg=0.0,sum=0.0;
for (int i = 0; i < 10; i++)
{
sum+=list[i];
}
avg=sum/10;
return avg;
}
public static int[] foldArray(int[] list)
{
int[] tmp;
if (list.length % 2 == 0) {
tmp = new int[list.length / 2];
for (int i = 0; i < list.length / 2; i++) {
tmp[i] = list[i];
}
} else {
tmp = new int[list.length / 2 + 1];
for (int i = 0; i < list.length / 2 + 1; i++) {
tmp[i] = list[i];
}
}
for (int i = 0; i < list.length / 2; i++) {
tmp[i] += list[list.length - i - 1];
}
return tmp;
}
}
Output
The original array is :
21 23 27 35 49
13 43 16 18 40
Average is: 28.5
The folded array is :
61 41
43 78 62
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.