Objectives In this lab you will review using arrays in programs as well as begin
ID: 3782795 • Letter: O
Question
Objectives
In this lab you will review using arrays in programs as well as begin to use the Eclipse environment.
Requirements
Create a program that will declare and initialize an array with the date given below. Your program should then compute and display the total of the elements in the array, the average of the elements in the array, the maximum value in the array and the minimum value in the array. The values you should get from this data are displayed below. The text has examples that you can use as models.
Here is the data:
2683, 2654, 3090, 3325, 4881, 3300, 7456, 3780, 3232, 7270,
7773, 5553, 2850, 7619, 2762, 6965, 4539, 3131, 6066, 5404,
4183, 6830, 2424, 4517, 6599, 3714, 7351, 4243, 6688, 7819,
6449, 3781, 5565, 3283, 7427
Max value
Min value
7819
2424
Total
175206
Average
5005.885714
There are several important points that are general requirements for labs in this course:
§ Use a separate Eclipse project for each lab, unless otherwise instructed.
§ The name of the project should include your name as part of it. For example, I might name the project today 'Lab_1_Sarah'. Note, you specify the name of the project when you create the project.
§ Include a comment at the beginning of each source file you submit that includes your name and the lab date.
§ Names for variables and other program components should be chosen to help convey the meaning of the variable.
Turn in an archive of the entire Eclipse project for each lab. Do not attempt to turn in individual files, some course management systems mangle such files.
6449, 3781, 5565, 3283, 7427
Max value
Min value
7819
2424
Total
175206
Average
5005.885714
Explanation / Answer
//tested on Eclipse
import java.text.DecimalFormat;
public class ArrayOperation {
// This method return max value from array
public int maxvalue(int arr[]) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
// This method return min value from array
public int minValue(int arr[]) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
return min;
}
// This method return total value of array
public int total(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
ArrayOperation opr = new ArrayOperation();
// Array initialization
int arr[] = { 2683, 2654, 3090, 3325, 4881, 3300, 7456, 3780, 3232,
7270, 7773, 5553, 2850, 7619, 2762, 6965, 4539, 3131, 6066,
5404, 4183, 6830, 2424, 4517, 6599, 3714, 7351, 4243, 6688,
7819, 6449, 3781, 5565, 3283, 7427 };
int max = opr.maxvalue(arr);
int min = opr.minValue(arr);
int total = opr.total(arr);
// calculating average value
float avg = (float) total / arr.length;
// decimal format for printing n value after decimal
DecimalFormat _numberFormat = new DecimalFormat("#0.000000");
System.out.println("Max Value " + max);
System.out.println("Min Value " + min);
System.out.println("Total " + total);
System.out.println("Average " + _numberFormat.format(avg));
}
}
/******************************output*********************************/
Max Value 7819
Min Value 2424
Total 175206
Average 5005.885742
Thanks a lot
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.