Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab 10
- // Programmer: Daniel Shen
- // Editor(s) used: XP Notepad
- // Compiler(s) used: Java 1.6.0_12
- //
- import java.io.*;
- import java.util.*;
- import java.text.*;
- public class MyList
- {
- static double getAverage(int[] score)
- {
- int sum = 0;
- int i = 0;
- for (i = 0; i < score.length; i++)
- sum += score[i];
- double average = (double)sum / score.length;
- return average;
- } // getAverage
- static int countScoresGreater(int[] score, double x)
- {
- int nGreater = 0;
- int i; // loop counter
- for (i = 0; i < score.length; i++)
- if (score[i] >= x) nGreater++;
- return nGreater;
- } // countScoresGreater
- public static void main(String[] argv) throws Exception
- {
- BufferedReader cin;
- cin = new BufferedReader(new InputStreamReader(System.in));
- int size;
- System.out.print("What is the size? ");
- size = new Double(cin.readLine()).intValue();
- int[] score = new int[size];
- // read and save the scores
- int i;
- for (i = 0; i < score.length; i++)
- {
- System.out.print("Please enter score[" + i + "]: ");
- score[i] = new Double(cin.readLine()).intValue();
- }
- Arrays.sort(score);
- System.out.print("Sorted order low to high is: ");
- for (i = 0; i < score.length; i++)
- System.out.print("" + score[i] + ' ');
- System.out.println();
- // find the max AND min integers
- int max = score[0];
- int min = score[0];
- for (i = 1; i < score.length; i++)
- {
- if (max < score[i]) max = score[i];
- if (min > score[i]) min = score[i];
- } // for
- System.out.println("The max is " + max);
- System.out.println("The min is " + min);
- System.out.print("Average = ");
- System.out.println(new DecimalFormat("#.0").format(getAverage(score)));
- System.out.println("" + countScoresGreater(score, 90)
- + " scores are A's.");
- System.out.println("" + countScoresGreater(score, 80)
- + " scores are B's.");
- System.out.println("" + countScoresGreater(score, 70)
- + " scores are C's.");
- } // main
- } // public class
Add Comment
Please, Sign In to add comment