-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule6.java
74 lines (67 loc) · 2.48 KB
/
Module6.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Donald Trowbridge
* Module 6 Programming Project
* Part A
*
* This program takes creates an Array of 100 random float numbers and gets the
* average of those numbers
*/
package module6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collection;
import java.util.Random;
import java.util.List;
import java.util.Iterator;
public class Module6 {
public static void main(String[] args)
{
//Random object for creating random numbers.
Random ran = new Random();
//Float array for 100 float numbers.
float[] fl = new float[100];
//for loop to fill each Array element with a random float number
for(int i = 0; i < 100; i++)
{
fl[i] = ran.nextFloat()*100;
}
//Declare ArrayList for float values
List<Float> floatList = new ArrayList();
//add numbers from float Array to ArrayList
for(float f: fl)
floatList.add(f);
//Display absolute average of list
System.out.printf("The absolute average is: %f.%nThis was calculate by "
+ "adding all numbers and then"
+ " dividing by the number of elements in the list.%n",
absoluteAverage(floatList));
//Display approximate average of list
System.out.printf("%nThe approximate average of the list is: %f%n"
+ "This was calculated by adding the maximum value of the list and "
+ "the minimum value of the list and then divided by 2.%n"
, approximateAverage(floatList));
}
//Method to calculate abosolute average of a list
public static float absoluteAverage(List<Float> list)
{
float sum = 0;
float average;
//Iterator object to cycle through the List
Iterator<Float> it = list.iterator();
while(it.hasNext())//adds all numbers together
{
sum = sum + (float) it.next();
}
average = sum/(float) list.size();//calculates average of list
return average; //returns average
}
//Method to calculate approximate average of a list
public static float approximateAverage(List<Float> list)
{
float max = Collections.max(list);//gets highest number
float min = Collections.min(list);//gets lowest number
float result= (min + max)/2;//calculates average
return result;//returns average
}
}