Skip to content

Instantly share code, notes, and snippets.

@LordNairu
Last active December 28, 2015 15:19
Show Gist options
  • Save LordNairu/7520491 to your computer and use it in GitHub Desktop.
Save LordNairu/7520491 to your computer and use it in GitHub Desktop.
Average, tallest and smallest heights.
package qub.ac.uk.practical.week5;
public class PracticalFiveQuestionThree {
/**
* A method which calculates various statistics of students from an array
* @param args
*/
public static void main(String[] args) {
// Declaring vars to hold average height, tallest and smallest
double average=0;
double tallest=0;
double smallest=0;
// Populate array with student heights
double [] stuHeightArray = { 1.4, 1.9, 1.31, 1.2 };
// Calculate average height
for (double i : stuHeightArray){
average += i/stuHeightArray.length;
}
// Calculate tallest student
for (int counter=0; counter<stuHeightArray.length;counter++){
if (stuHeightArray[counter] > tallest){
tallest = stuHeightArray[counter];
}
}
// Calculate smallest student
smallest = stuHeightArray[0];
for (int counter=0; counter<stuHeightArray.length; counter++){
if (stuHeightArray[counter] < smallest){
smallest = stuHeightArray[counter];
}
}
// Printing values
System.out.println("Average height of the class is: "+Math.round(average*100.0)/100.0);
System.out.println("The height of the tallest student is: "+tallest);
System.out.println("The height of the smallest student is: "+smallest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment