Advertisement
Nick-O-Rama

TestScores Exception

May 11th, 2015
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1.  
  2. public class TestScores {
  3.    
  4.     private int[] scores;
  5.    
  6.     public TestScores(int[] arr) {
  7.         this.scores = new int[arr.length];
  8.        
  9.         for (int i = 0; i < arr.length; i++){
  10.             if (arr[i] > 100 || arr[i] < 0)
  11.                 throw new IllegalArgumentException("Invalid argument: " + arr[i]);
  12.             else
  13.                 this.scores[i] = arr[i];
  14.         }
  15.     }
  16.    
  17.     public double getAverage(){
  18.         double sum = 0;
  19.         for (int i = 0; i < this.scores.length; i++)
  20.             sum += scores[i];
  21.         return sum / scores.length;
  22.            
  23.     }
  24.    
  25.    
  26.     public static void main(String[] args) {
  27.         int[] arr = {-8,2,3,4,5,5,600};
  28.         try {
  29.             TestScores ts = new TestScores(arr);
  30.             System.out.println(ts.getAverage());
  31.         }
  32.         catch (IllegalArgumentException e){
  33.             System.out.println(e.getMessage());
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement