I have an assignment to read a file of student records, then calculate and display the final grade for each course. As an old procedural programmer, I'm having a hard time with figuring out the "proper" (e.g., most OO) program flow, given the UML from which we're supposed to start. A big problem is that this is my first time implementing Array Lists, and I'm still a little fuzzy on practical examples, even after perusing the Java Tutorials and a bunch of online samples. I think the end-of-semester brain fuzz is preventing me from applying those examples to this assignment.
Here's where I am so far:
My main method consists of
FinalStudentGrade finalGradeReport = new FinalStudentGrade();
finalGradeReport.MainMethod();
which calls Students.PopulateStudents(@"grades.txt") to display the grade report. PopulateStudents() reads the data file, creating a student object through the constructor, and based on the following data member:
string nameFirst;
string nameLast;
string studentID;
ArrayList Earned;
ArrayList Possible;
float average;
string letterGrade;
The PopulateStudents method reads the file, creates a Student object for each line in the file, including a calculated average and letter grade. Each of these students then need to be added to List<Student> theStudentList;
class Students
{
List<Student> theStudentList;
public bool PopulateStudents(string @sourceData)
{
String sourcePath = sourceData;
theStudentList = new List<Student>();
bool success = true;
try
{
StreamReader inputReader = new StreamReader(sourcePath);
while (inputReader != null)
{
String inputLine = inputReader.ReadLine();
char delim = ',';
String[] inputValues = inputLine.Split(delim);
.... and I'm Stuck ....
I was about to define nameFirst = inputValues[1];, nameLast = inputValues[2];, studentID = inputValues[0]; etc., but then I realized that I'm not using the constructor to create the Student object.
Do need to create Student.nameFirst=..., then populate theStudentList with those objects? Or can I just define theStudentList[recordCounter].nameFirst (defining a recordCounter first, of course) because theStudentList is being instatiated as List<Student>();? What's the best way to proceed with that, and am I visualizing the program flow between classes in a good way?
Studentobjects to add totheStudentList. @DStanley's answer is what you need - as part of your loop.