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
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 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
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...
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...
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
A) write a program the computes nx and store the result into y You can use...
A) write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to opportunity retry the entry the user must be able to quit prior to entering...
In this project, you will write a Java program that randomly generates annual salaries for professors...
In this project, you will write a Java program that randomly generates annual salaries for professors in the Computer Science and Engineering Technology (CSET) program at the University of Toledo in the past 10 years. Assume that there are five CSET professors. You should have a twodimensional array of values representing salaries. The first dimension represents the professors and the second dimension represents the years of the salaries. You should have a constructor that takes two integers representing the number...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT