Skip to content

Instantly share code, notes, and snippets.

@singhrahuldps
Created April 29, 2020 19:52
Show Gist options
  • Save singhrahuldps/e2fba3c63143d27c6839aba4e9e66985 to your computer and use it in GitHub Desktop.
Save singhrahuldps/e2fba3c63143d27c6839aba4e9e66985 to your computer and use it in GitHub Desktop.
// Program to return average of numbers
public static double doAverage(ArrayList<Integer> marks) {
// double for more precision than Integer during division
double avg = 0.0;
for(int mark: marks) {
avg += mark;
}
// size() method returns the number of elements in array
avg = avg/marks.size();
return avg;
}
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>(List.of(95,96,98));
// static methods can be accessed directly without creating a class object
// Here class name is Methods
double avg = Methods.doAverage(marks);
System.out.println(avg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment