-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckavg.java
More file actions
141 lines (118 loc) · 3.06 KB
/
Copy pathcheckavg.java
File metadata and controls
141 lines (118 loc) · 3.06 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright (c) 2014 CA. All rights reserved.
*
* This software and all information contained therein is confidential and
* proprietary and shall not be duplicated, used, disclosed or disseminated
* in any way except as authorized by the applicable license agreement,
* without the express written permission of CA. All authorized reproductions
* must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT
* WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN
* NO EVENT WILL CA BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY
* LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS INTERRUPTION,
* GOODWILL, OR LOST DATA, EVEN IF CA IS EXPRESSLY ADVISED OF SUCH LOSS OR
* DAMAGE.
*/
package practice1;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
/**
* DOCUMENT ME!
*/
public class checkavg
{
public static double[] last15 = new double[15];
public static Queue<Double> q = new LinkedList<Double>();
private static double modifyAverage(double val)
{
double avg = 0;
int lensofar = 0;
for (int i = 0; i < last15.length; i++)
{
if (last15[i] > -1)
{
avg += last15[i];
++lensofar;
}
else
{
last15[i] = val;
break;
}
}
print(last15);
avg /= lensofar;
System.out.println("lensofar = " + lensofar + " avg=" + avg);
return avg;
}
/**
* DOCUMENT ME!
*
* @param arr DOCUMENT ME!
*/
public static void print(double[] arr)
{
System.out.print("[ ");
for (int i = 0; i < arr.length; ++i)
{
System.out.print(arr[i] + " ");
}
System.out.println("]");
}
private static double computeAvg(double lastval)
{
double avg = 0;
//Queue<Double> q = new LinkedList<Double>();
if (q.size() < 15)
{
q.add(lastval);
}
else
{
q.poll();
q.add(lastval);
}
//compute the avg
Iterator<Double> itr = q.iterator();
while (itr.hasNext())
{
avg += itr.next();
}
avg /= q.size();
return avg;
}
private static void printQueue(Queue<Double> q)
{
Iterator<Double> itr = q.iterator();
System.out.print("[ ");
while (itr.hasNext())
{
System.out.print(itr.next() + " ");
}
System.out.print("]");
}
/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
*/
public static void main(String[] args)
{
for (int i = 0; i < 15; i++)
{
last15[i] = -1;
}
for (double val = 0; val < 30; ++val)
{
//modifyAverage(val);
double avg = computeAvg(val);
printQueue(q);
System.out.println(" avg=" + avg);
}
}
}