In: Computer Science
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to a line with spaces in between. Once the contents of the array have been printed to screen, display the smallest number, largest number, and average determined previously. You should ensure that your program design in modular.
The Random class of Java library (java.util.Random) implements a random number generator. To generate random numbers, you construct an object of q the class Random, and then use the method nextInt(n) which returns a number between 0 (inclusive) and n (exclusive).
E.g.
import java.util.Random;
.....
Random generator = new Random();
.....
//generate a random number between 0 and 99 (inclusive)
int nextRand = generator.nextInt(100);
Alternatively use Math.random(), which returns a random value in the range 0.0 to 1.0 (then adjust to the correct range)
Hi,
Please find the java program below:
Program
package random;
import java.util.Random;
import java.util.Scanner;
///////////////////////////////////////
//ArrayDemo.java
// Program design in modular to fill array
// with random numbers 1 - 999 inclusive
///////////////////////////////////////
public class ArrayDemo {
//array and size variables
static int[] array;
static int size=0;
public static void main(String[] args) {
//variables used
Scanner console = new
Scanner(System.in);
int smallest;
int largest;
double average;
try {
//prompt the
user for size
System.out.print("How many numbers to store?=");
size=Integer.parseInt(console.nextLine());
array= new
int[size];
for(int
i=0;i<size;i++) {
array[i]=getRandonNumber();
}
smallest=getSmallest(array);
largest=getLargest(array);
average=getAverage(array);
printContents(array);
System.out.println("\nSmallest="+ smallest);
System.out.println("\nLargest="+ largest);
System.out.println("\nAverage="+ average);
}catch(NumberFormatException nfe)
{
System.out.println("Error: Invalid Size.");
}
}
//Method to get random number
public static int getRandonNumber() {
Random generator = new
Random();
return generator.nextInt(999)+
1;
}
//Method to get smallest number
public static int getSmallest(int[] a) {
int small= Integer.MAX_VALUE;
for(int i=0;i<a.length;i++)
{
if(a[i] <
small)
small=a[i];
}
return small;
}
//Method to get largest number
public static int getLargest(int[] a) {
int large= Integer.MIN_VALUE;
for(int i=0;i<size;i++) {
if(a[i] >
large)
large=a[i];
}
return large;
}
//Method to get average
public static double getAverage(int[] a) {
int sum= 0;
for(int i=0;i<size;i++) {
sum=sum+a[i];
}
return sum/size;
}
//Method to get average
public static void printContents(int[] a) {
int row= 5;
System.out.println("Array
Contents:");
for(int i=0;i<size;i++) {
if(i%row ==0)
{
System.out.println();
System.out.print(a[i] + " ");
}
else
System.out.print(a[i] + " ");
}
}
}
Screenshot
Output:
How many numbers to store?=20
Array Contents:
279 362 333 53 131
880 549 582 823 223
995 653 840 934 288
595 663 604 499 121
Smallest=53
Largest=995
Average=520.0
------------------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.