Reputation: 7066
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?
Upvotes: 0
Views: 173
Reputation: 12805
Just a little addendum to make it even simpler to read than D Stanley's answer:
theStudentList.Add(new Student {
nameFirst = inputValues[1],
nameLast = inputValues[2],
studentID = inputValues[0]
});
Upvotes: 1
Reputation: 152501
Well, I don't see a constructor defined, so I'm assuming you didn't define one that takes paramters. If that's the case, you can either construct the object first, then set the properties:
Student s = new Student();
s.nameFirst = inputValues[1];
s.nameLast = inputValues[2];
s.studentID = inputValues[0];
or use initialization syntax
Student s = new Student() {
nameFirst = inputValues[1]
nameLast = inputValues[2]
studentID = inputValues[0]
};
If you did define a constriuctor (say one that takes the three properties you mentioned), then just use the constructor:
Student s = new Student(inputValues[1], inputValues[2], inputValues[0]);
then just add the created student:
theStudentList.Add(s);
Upvotes: 5