zkirkland
zkirkland

Reputation: 12815

How do I create a list where each element has a name and several lists within the element?

I am trying to write a program in java that allows me to add courses to a list and within each course element, I can add a list of assignments such as homework, exams, etc. Within each assignment I will have "homework 1" grade, "homework 2" grade, etc... How do I implement this in java?

Its hard to explain what I wish to do so hopefully this will help:

Courses:
  History:
    Homework:
      Homework 1 89
      Homework 2 98
    Exams:
      Exam 1 90
      Exam 2 87
  Science:
    Homework:
      Homework 1 88
    Exams:
      Exam 1 86

So, a list of courses, with each course having a list of assignments, with each assignment having a list of grades.

Thanks, Zach

Upvotes: 0

Views: 57

Answers (1)

StackFlowed
StackFlowed

Reputation: 6816

This is a good point to start learning about Classes.

You can do something like

public class Course {
    private String name;
    private List<Integer> homeWorkScores;
    public Course(String courseName){
        this.name = courseName;
        homeWorkScores = new ArrayList<Integers>();
    }

    public String getCourseName(){
        return name;
    }

    public boolean addHomeWorkScore(Integer score){
        return homeWorkScores.add(score);
    }
} 

Then instantiate it like :

public static void main(String [] args) {
    List<Course> courses = new ArrayList<Course>();
} 

Upvotes: 2

Related Questions