0

I need help converting my code into an ArrayList. We are only supposed to use an ArrayList and not an array. I didn't see that part of my project and not too good with using ArrayList. Please help me figure this out.

public class Transcript {
    private Course[] courses = new Course[10];
    private int studentID;
    private String studentName;
    private int count;

    public Transcript(int studentID, String studentName)
    {
        this.studentID = studentID;
        this.studentName = studentName;
        count = 0;
    }

    public void addCourse(String courseID, String letterGrade)
    {
        courses[count] = new Course(courseID,letterGrade);
        count++;
    }

    public void updateCourse(String courseID, String letterGrade)
    {   
        for(int i = 0;i<count;i++)
        {
            if(courseID.equals(courses[i].getCourseID()))
            {
                courses[i].updateGrade(letterGrade);
                break;
            }
        }
    }

    public double getGPA()
    {
        return calculateGPA();
    }

    private double calculateGPA()
    {
        double GPA = 0;
        double total = 0;
        for(int i = 0;i<count;i++)
        {
            total = total + courses[i].getNumericGrade();
        }
        GPA = total/count;
        return GPA;
    }

    public String getCourse(String courseID)
    {
        String str = courseID + " not found!";
        for(int i = 0;i<count;i++)
        {
            if(courseID.equals(courses[i].getCourseID()))
            {
                str = courses[i].toString();
                break;
            }
        }
        return str;
    }

    @Override
    public String toString()
    {
        String str = "ID:" + studentID + "\nName:" + studentName + "\nGPA:" + getGPA() + "\n";
        for(int i = 0;i<count;i++)
        {
            str = str +  courses[i].toString() + "\n";
        }
        return str;
    }
}
2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by bguild
0

An ArrayList is even easier to use than an array for a project like this, so you should expect that your project will be made simpler if you do it correctly. The first step is to read the documentation for ArrayList.

It is difficult and error-prone to attempt to use a class without reading the documentation, but when you read high-quality documentation it becomes easy. ArrayList has high-quality documentation, so you have nothing to worry about. You should probably read all of the documentation eventually, but good documentation allows you to read just the parts for the methods that you want to call and in your case that will probably be: add, get, size, and the constructors.

Edited by bguild

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.