Write (in Java) a complete class based on the description below: 1. Write a clas
ID: 3601546 • Letter: W
Question
Write (in Java) a complete class based on the description below:
1. Write a class named Ex
2. Instantiate a private integer array called array with the values {2,3,1,4,5,7,1}
3. Write an integer method called findSum which will find the sum of an array
4. Write a double method called findAverage which will find the average of an array
5. Write a class named ExClient
6. Write the main method for ExClient
7. Write the code to create an Ex object called answer
8. Write the code so that the output would look like: "Average of all elements in the array is 3.286"
Explanation / Answer
ExClient.java
public class ExClient {
public static void main(String[] args) {
Ex answer = new Ex();
System.out.printf("Average of all elements in the array is %.3f ",answer.findAverage());
}
}
Ex.java
public class Ex {
private int array[] ={2,3,1,4,5,7,1};
public int findSum() {
int sum = 0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
return sum;
}
public double findAverage() {
return findSum()/(double)array.length;
}
}
Output:
Average of all elements in the array is 3.286
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.