Advertisement
Shavit

P. 90 Ex. 11.11

Feb 11th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class ClassGrades {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.         Grades obj = new Grades();
  13.        
  14.         System.out.print("How many students are there in your class? ");
  15.         int classSize = in.nextInt();
  16.        
  17.         obj.classSize(classSize);
  18.        
  19.         for(int i = 0; i < classSize; i++)
  20.         {
  21.             System.out.printf("Enter the math grade for student number %d: ", i + 1);
  22.             int currentMath = in.nextInt();
  23.            
  24.             System.out.printf("Enter the hebrew grade for student number %d: ", i + 1);
  25.             int currentHebrew = in.nextInt();
  26.            
  27.             obj.setGrade(i, currentMath, currentHebrew);
  28.         }
  29.        
  30.         System.out.printf("The average grade for math is %.2f\n", obj.mathAvg());
  31.         System.out.printf("The average grade for hebrew is %.2f\n", obj.hebrewAvg());
  32.        
  33.         in.close();
  34.     }
  35. }
  36.  
  37. // Next class
  38.  
  39. public class Grades
  40.  
  41. {
  42.     private int size = 20; // Default value
  43.    
  44.     public void classSize(int size)
  45.     {
  46.         this.size = size;
  47.     }
  48.    
  49.     private int[] math = new int[size];
  50.     private int[] hebrew = new int[size];
  51.    
  52.     public void setGrade (int id, int math, int hebrew)
  53.     {
  54.         this.math[id] = math;
  55.         this.hebrew[id] = hebrew;
  56.     }
  57.    
  58.     public double mathAvg()
  59.     {
  60.         double sum = 0;
  61.         for(int i = 0; i < size; i++)
  62.         {
  63.             sum += math[i];
  64.         }
  65.         sum /= size;
  66.         return sum;
  67.     }
  68.    
  69.     public double hebrewAvg()
  70.     {
  71.         double sum = 0;
  72.         for(int i = 0; i < size; i++)
  73.         {
  74.             sum += hebrew[i];
  75.         }
  76.         sum /= size;
  77.         return sum;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement