By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Java Array Constructor - Gradebook

P: 1
What needs to be modified in StudentManager class so that it uses constructor instead of the following lines.

gradeBook[i].setFirstName(fName);
gradeBook[i].setLastName(lName);
gradeBook[i].setTestScores(tstScores);


StudentManager.java


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class StudentManager
  5. {
  6.     public static void main(String[] args) throws FileNotFoundException
  7.     {
  8.         Tests[] gradeBook = new Tests[10];
  9.  
  10.         String fName, lName;
  11.         double[] tstScores = new double[5];
  12.  
  13.         double classAverage = 0.0;
  14.  
  15.         Scanner inFile =
  16.                       new Scanner(new FileReader("StudentData.txt"));
  17.  
  18.         for (int i = 0; i < 10; i++)
  19.             gradeBook[i] = new Tests();
  20.  
  21.         for (int i = 0; i < 10; i++)
  22.         {
  23.             fName = inFile.next();
  24.             lName = inFile.next();
  25.  
  26.             for (int j = 0; j < 5; j++)
  27.                 tstScores[j] = inFile.nextDouble();
  28.  
  29.             gradeBook[i].setFirstName(fName);
  30.             gradeBook[i].setLastName(lName);
  31.             gradeBook[i].setTestScores(tstScores);
  32.         }
  33.  
  34.         System.out.println("First_Name  Last_Name     Test1  Test2  "
  35.                           + "Test3  Test4  Test5   Average Grade");
  36.  
  37.         for (int i = 0; i < 10; i++)
  38.         {
  39.             System.out.println(gradeBook[i]);
  40.  
  41.             classAverage = classAverage + gradeBook[i].getAverage();
  42.         }
  43.  
  44.         classAverage = classAverage / 10;
  45.  
  46.         System.out.println();
  47.  
  48.         System.out.printf("Class average = %.2f%n", classAverage);
  49.     }//end main
  50. }
  51.  
  52.  
Tests.java

Expand|Select|Wrap|Line Numbers
  1. public class Tests
  2. {
  3.     private final int numberOfTests = 5;
  4.  
  5.     private String firstName;
  6.     private String lastName;
  7.     private double[] testScores;
  8.     private double average;
  9.     private char grade;
  10.  
  11.     Tests()
  12.     {
  13.         firstName = "";
  14.         lastName = "";
  15.         testScores = new double[numberOfTests];
  16.         average = 0.0;
  17.         grade = '*';
  18.     }
  19.  
  20.     Tests(String fName, String lName, double[] ts)
  21.     {
  22.         firstName = fName;
  23.         lastName = lName;
  24.         testScores = new double[numberOfTests];
  25.         for (int i = 0; i < numberOfTests; i++)
  26.             testScores[i] = ts[i];
  27.         average = getAverage();
  28.         grade = getGrade();
  29.     }
  30.  
  31.     public void setFirstName(String fName)
  32.     {
  33.         firstName = fName;
  34.     }
  35.  
  36.     public void setLastName(String lName)
  37.     {
  38.         lastName = lName;
  39.     }
  40.  
  41.     public String getFirstName()
  42.     {
  43.         return firstName;
  44.     }
  45.  
  46.     public String getLastName()
  47.     {
  48.         return lastName;
  49.     }
  50.  
  51.     public void setTestScores(double[] ts)
  52.     {
  53.         for (int i = 0; i < numberOfTests; i++)
  54.             testScores[i] = ts[i];
  55.     }
  56.  
  57.     public void setTestScr(double scr, int index)
  58.     {
  59.         if (0 <= index && index < numberOfTests)
  60.             testScores[index] = scr;
  61.         else
  62.             System.out.println("Invalid array index.");
  63.     }
  64.  
  65.     public double getTestScr(int index)
  66.     {
  67.         if (0 <= index && index < numberOfTests)
  68.             return testScores[index];
  69.         else
  70.         {
  71.             System.out.println("Invalid array index.");
  72.  
  73.             return -1.0;
  74.         }
  75.     }
  76.  
  77.     private void calculateAverage()
  78.     {
  79.         for (int i = 0; i < numberOfTests; i++)
  80.             average = average + testScores[i];
  81.  
  82.         average = average / 5;
  83.     }
  84.  
  85.     public double getAverage()
  86.     {
  87.         average = 0.0;
  88.  
  89.         calculateAverage();
  90.  
  91.         return average;
  92.     }
  93.  
  94.     private void calculateGrade()
  95.     {
  96.         double avg = getAverage();
  97.  
  98.         if (avg >= 90.00)
  99.             grade =  'A';
  100.         else if (avg >= 80.00)
  101.             grade =  'B';
  102.         else if (avg >= 70.00)
  103.             grade =  'C';
  104.         else if (avg > 60.00)
  105.             grade =  'D';
  106.         else
  107.             grade =  'F';
  108.     }
  109.  
  110.     public char getGrade()
  111.     {
  112.         calculateGrade();
  113.         return grade;
  114.     }
  115.  
  116.     public String toString()
  117.     {
  118.         average = 0.0;
  119.  
  120.         calculateAverage();
  121.         char grade = getGrade();
  122.  
  123.         String scoreString = "";
  124.  
  125.         for (int i = 0; i < numberOfTests; i++)
  126.             scoreString = scoreString + String.format("%7.2f", testScores[i]);
  127.  
  128.         return String.format("%-11s %-11s %35s %7.2f %5c", firstName,
  129.                              lastName, scoreString, average, grade);
  130.     }
  131. }
  132.  
Aug 23 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
In line 19 of StudentManager.java you call the constructor Tests(); you would have to replace this with Tests(a, b, c) where a, b and c are the arguments you want to pass.
Now, currently you don't get the arguments until after the Test objects have been created so you'll have to switch the order here. So move the lines
Expand|Select|Wrap|Line Numbers
  1. fName = inFile.next();
  2. lName = inFile.next();
  3.  
  4. for (int j = 0; j < 5; j++)
  5.     tstScores[j] = inFile.nextDouble();
to before the constructor.
Aug 23 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.