-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeight.java
More file actions
105 lines (92 loc) · 2.6 KB
/
Weight.java
File metadata and controls
105 lines (92 loc) · 2.6 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
import java.io.*;
/**
* TODO Write a one-sentence summary of your class here. TODO Follow it with
* additional details about its purpose, what abstraction it represents, and how
* to use it.
*
* @author TODO Your Name
* @version TODO date
*
* @author Period - TODO Your period
* @author Assignment - Chapter 48 Arrays as Parameters Exercises 1-3
*
* @author Sources - TODO your sources
*/
public class Weight
{
private int[] data;
/**
* Constructs a Weight object that contains an array of the weight
* of an individual taken on successive days for one month.
*
* @param init array of weights for one month
*/
public Weight(int[] init)
{
// Construct the array the same length
// as that referenced by init.
data = new int[init.length];
// Copy values from the
// input data to data.
for (int j = 0; j < init.length; j++)
{
data[j] = init[j];
}
}
/**
* TODO Description
*/
public int average()
{
// TODO complete method
int sum = 0;
for (int i = 0; i < data.length; i++)
{
sum+=data[i];
}
sum = sum / data.length;
return sum; // FIX THIS!!
}
/**
* TODO Description
*/
public int subAverage( int start, int end )
{
int day_avg = 0;
for (int i = start; i<end+1; i++){
day_avg+=data[i];
}
day_avg = day_avg/ (end-start + 1);
return day_avg;
}
/**
* TODO Description
*/
public void print()
{
for (int j = 0; j < data.length; j++)
{
System.out.println(data[j]);
}
}
/**
* Testing method: instantiates a Weight object and then invokes
* each method.
*
* @param args command line parameters (not used)
*/
public static void main( String[] args )
{
int[] values = { 98, 99, 98, 99, 100, 101, 102, 100, 104, 105, 105, 106,
105, 103, 104, 103, 105, 106, 107, 106, 105, 105, 104, 104, 103,
102, 102, 101, 100, 102 };
Weight june = new Weight( values );
june.print();
int avg = june.average();
System.out.println( "average = " + avg );
int avg1stHalf = june.subAverage(0, ( values.length - 1) / 2 );
System.out.println( "average of first half of month = " + avg1stHalf );
int avg2ndHalf = june.subAverage(values.length / 2 , values.length - 1);
System.out.println( "average of second half of month = " + avg2ndHalf );
}
}