473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Java Array Constructor - Gradebook

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
1 2473
Nepomuk
3,112 Expert 2GB
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

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

Similar topics

5
by: lallous | last post by:
Hello, Probably I am not seeing it now, but is it possible for constructor(int) to call constructor() ? class A { private: int x, y; public:
1
by: Eugene | last post by:
Hi All, I am having a hard time figuring out how to pass a ("true") java array, like int, from a jdbc application in to a DB2 stored procedure or UDF (can be a java or SQL proc or a table UDF)....
2
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.......
1
by: pompeez | last post by:
Hi, I need to write a Java Array/ArrayList/List to a text file in Jython. I used the .write() function, but it is letting me write only a string. Thanks! Pompeez
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
1
ck9663
by: ck9663 | last post by:
Hi guys, CK here from SQL Server forum. Is there a function that will automatically populate a two-dimensional array coming from a SQL Server table? Or should I do it the old fashion "WHILE NOT...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
1
by: niner | last post by:
I have been working on the following code for about 6 hours now. I am attempting to write a program in Java that finds the highest, lowest, sum, and mean of an array. The program should also end...
1
by: sensure | last post by:
why java array starts with zero and why pascal array stars with one?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.