0

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?

1
  • I don't see any code that creates Student objects to add to theStudentList. @DStanley's answer is what you need - as part of your loop. Commented Dec 16, 2013 at 16:40

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

9 Comments

+1 for initialization syntax. OP just needs to make sure he creates a default constructor.
No he doesn't - a parameterless constructor will be available by default unless a different constructor is defined. To be fair I added an option if a constructor is defined.
I was assuming he already created a constructor he mentioned a constructor within the original post. I was under the impression if you already have a constructor defined for your class, then you need to create a default one to use object initialization syntax.
OK...that all makes sense then. I do have a constructor already defined. I assume I instantiate the new Student() just INSIDE my while (inputReader != null) loop, and theStudentList.Add(s) just before the closing brace of the while? Does that make sense for progtram flow?
@dwwilson66 - theStudentList is defined as a field member of the class, so you can reference it within any method of that class. And you need to construct it outside the loop otherwise you'd be blowing it away with each iteration.
|
1

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]
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.