Question

Write a program in Java that creates an array of doubles. The array must have size...

Write a program in Java that creates an array of doubles. The array must have size 1000. Fill this array with random numbers between 0 and 1. Then compute and print the total value of all the elements of the array, the mean, the minimum, and the maximum.

Filling the array with random numbers must be done by a method that you define and implement. Computing and printing the statistics must be done by another method, which again you must define and implement. This second method must use an enhanced for loop.

Homework Answers

Answer #1
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Working of the program

  • Random class is imported to generate random numbers
  • A double array of size 1000 is declared
  • fillArray() method is called to fill array with random numbers
  • In fillArray() method, a for loop is used to iterate through the array
  • nextDouble() method of Random class is used to generate a random number between 0 and 1
  • generated random numbers are stored into array.
  • In main() method computeStatistics() method is called to perform various calculation and display its result.
  • In computeStatistics() method, min,max variables are initialized with first element of the array
  • An enhanced for loop is used to iterate through the array
  • inside the loop, each element's value is added to variable total.
  • Element is compared with max and min values.
  • If the element is greater than max,then set it as the new max
  • If the element is lesser than min,then set it as the new min
  • Mean is calculated by total divided by number of elements
  • Results are displayed

Source code

//importing Random class to generate random numbers
import java.util.Random;
public class Main {
    public static void main(String[] args) {
        //declaring an array of 1000 doubles
        double doubles[] = new double[1000];
        //calling fillArray() method to fill array with random values
        double doublesFilled[] = fillArray(doubles);
        //calling computeStatistics() 
        computeStatistics(doublesFilled);
    }

    public static double[] fillArray(double d[]) {
        //creating instance of Random class
        Random rand = new Random();
        //iterate through array
        for (int i = 0; i < 1000; i++) {
            //generating random double value in between 0 and 1
            double randomValue = rand.nextDouble();
            //storing the random value into array
            d[i] = randomValue;
        }
        return d;
    }

    public static void computeStatistics(double arr[]) {
        //declaring variables
        double min, max, total = 0, mean;
        //setting min and max to arr[0]
        min = max = arr[0];
        //iterate each double value in the array using enhanced for loop
        for (double i : arr) {
            //finding total
            total = total + i;
            //checking the element is greater than max,if yes set element as new max
            if (i > max)
                max = i;
            //checking the element is smaller than min,if yes set element as new min
            if (i < min)
                min = i;
        }
        //calculating mean
        mean = total / 1000;
        //printing results
        System.out.println("Total value: " + total);
        System.out.println("Mean of the elements: " + mean);
        System.out.println("Minimum of elements: " + min);
        System.out.println("Maximum of elements: " + max);
    }
}

Screen shot of the code

Screen shot of the output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write Java code that creates an array of primitive integers named odds and stores all odd...
Write Java code that creates an array of primitive integers named odds and stores all odd numbers between -6 and 38 using a for loop. (Your code will populate the array.) Your code should also enable the array’s size to be exactly large enough to store the numbers. (You will not put a number for the size, your code will determine the number.) (5 pt)
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them. Deliverables A listing of the fully commented, working source code of the Java program Test data for the code A screen shot of the application in execution Step 1 Create a new project. Name it "Assignment_2_1". Step 2 Build a solution. Write the Java source code necessary to build a solution for the problem below:You have just...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Please make a Java program that has a menu method and includes dialog boxes. The program...
Please make a Java program that has a menu method and includes dialog boxes. The program will call the proper method, but each method will be completed later. Your program will keep track of information for a Hospital. The program will be menu-driven. Your menu is to look something like the following: UPMC Medical Center Add/Modify Patient Information Add/Modify Physician Information Add/Modify Medical Information Hospital Report Section Exit the Medical System Please Make your selection > In this first phase,...
Hi, I need this program written in Java for my intro CS class. Thank you in...
Hi, I need this program written in Java for my intro CS class. Thank you in advance. (also here is the description given in class for the code) Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period. - You should use the method isLeap which returns "true" if the year is a leap year, otherwise it should return...