Last active
December 28, 2015 15:19
-
-
Save LordNairu/7520491 to your computer and use it in GitHub Desktop.
Average, tallest and smallest heights.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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