Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Scanner ReadTheseNumbers;
- public static void main (String[] args) throws Exception
- {
- File file = new File( "ReadThis.txt");
- ReadTheseNumbers = new Scanner(file);
- List<Double> grades = new LinkedList<Double>();
- while (ReadTheseNumbers.hasNextInt()) {
- grades.add((double) ReadTheseNumbers.nextInt());
- }
- System.out.println("The average of the numbers in the file is: " + avg(grades));
- }
- public static double avg(List<Double> grades) {
- double sum = 0;
- for (Double i : grades) {
- sum += i;
- }
- return (sum / grades.size());
- }
- public static double median (List<Double> grades) {
- Arrays.sort(grades);
- if (grades.length % 2 == 0) {
- return (grades[grades.length/2]+grades[grades.length/2-1])/2.0;
- }
- else {
- return grades[grades.length/2];
- }
- System.out.println("The median is " + median (grades));
- }
- public static int mode( grades[]) {
- int maxValue, maxCount;
- for (int i = 0; i < grades.length; ++i) {
- int count = 0;
- for (int j = 0; j < grades.length; ++j) {
- if (grades[j] == grades[i]) ++count;
- }
- if (count > maxCount) {
- maxCount = count;
- maxValue = grades[i];
- }
- }
- return maxValue;
- }
- System.out.println("The mode is " + mode(numbers));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement