Advertisement
Sheero

TestScores

Apr 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. //Zehra Baig
  2. //CSC-162-01
  3. //Lab 6-A
  4.  
  5. import java.text.DecimalFormat;
  6.  
  7. public class TestScores
  8. {
  9.     private double[] testScores;
  10.     private double average;
  11.     DecimalFormat avg = new DecimalFormat("###.00");
  12.    
  13.     public TestScores()
  14.     {
  15.         testScores = new double[0];
  16.     }
  17.    
  18.     public TestScores(double[] scores) throws IllegalArgumentException
  19.     {
  20.         testScores = scores;
  21.         setAverage(testScores);
  22.     }
  23.    
  24.     public void setAverage(double[] testScores) throws IllegalArgumentException
  25.     {
  26.         double sum = 0.0;
  27.         for(int i = 0; i < testScores.length; i++)
  28.         {
  29.             if(testScores[i] < 0 || testScores[i] > 100)
  30.             {
  31.                 throw new IllegalArgumentException("Invalid score found"
  32.                                                  + "\nPosition: " + i
  33.                                                  + "\nScore value: "
  34.                                                  + testScores[i]
  35.                                                  + "\n");
  36.             }
  37.             sum += testScores[i];
  38.         }
  39.         average = sum / testScores.length;
  40.     }
  41.    
  42.     public double getAverage()
  43.     {
  44.         return average;
  45.     }
  46.    
  47.     public String toString()
  48.     {
  49.         return "The average is " + avg.format(getAverage()) + "\n";
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement