Results 1 to 7 of 7

Thread: assignment help

  1. #1
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default assignment help

    In my assignment, I need to identify students enrolled in Independent Study and enrolled in at least one Regular Course. I was able to identify those students, but am not able to figure out how to re-run the course list for those students. When I have the following, I get compilation errors. How do I go through the course list of that student again without getting compilation errors or told it is duplicative?

    //get student list and initialize variables a and b to zero where a counts the number of regular course and b counts the number of independent study classes

    for (Student stu : studentList) {
    ArrayList<Course> check = stu.getCourseList();

    int a = 0;
    int b = 0;

    //Identify students enrolled in Independent Study and also enrolled in at least one Regular Course

    for (Course course : check) {
    if (course instanceof RegularCourse) {
    a++;
    } else if (course instanceof IndependentStudy){
    b++;
    } else System.out.println("something is wrong");
    if (b > 0 && a > 0) {
    for (Course stulist: courseList){
    System.out.println(stu.getName() + " has " + course.getTitle());

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You might help people explain what the compiler messages mean if you post the exact messages. ANd the code they refer to.

  3. #3
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    I was actually missing a bracket, which is why i got the error. but when i run it, i get the following output:

    student2 has course3
    student2 has course3
    student2 has course3
    student2 has course3
    student2 has course3
    student3 has course4
    student3 has course4
    student3 has course4
    student3 has course4
    student3 has course4
    student5 has course5
    student5 has course5
    student5 has course5
    student5 has course5
    student5 has course5

    courses 3-5 are independent courses, but I want to list all the courses student 2 has, all the courses student 3 has, and all the courses student 5 has. but so far I can only get it to print the independent course number.

  4. #4
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    also, if helps this is the code:

    public void course() {
    ArrayList<Course> courseList = new ArrayList();
    ArrayList<Student> studentList = new ArrayList();

    // create the courses and add the course into the course ArrayList

    RegularCourse course1 = new RegularCourse("1", "course1",
    "instructor1", "spring", "");
    courseList.add(course1);
    RegularCourse course2 = new RegularCourse("2", "course2",
    "instructor2", "spring", "");
    courseList.add(course2);
    IndependentStudy course3 = new IndependentStudy("3", "course3",
    "instructor3", "spring", "");
    courseList.add(course3);
    IndependentStudy course4 = new IndependentStudy("4", "course4",
    "instructor4", "spring", "");
    courseList.add(course4);
    IndependentStudy course5 = new IndependentStudy("5", "course5",
    "instructor5", "spring", "");
    courseList.add(course5);

    // create the students and add the students into the student ArrayList
    Student student1 = new Student("1", "student1");
    studentList.add(student1);
    Student student2 = new Student("2", "student2");
    studentList.add(student2);
    Student student3 = new Student("3", "student3");
    studentList.add(student3);
    Student student4 = new Student("4", "student4");
    studentList.add(student4);
    Student student5 = new Student("5", "student5");
    studentList.add(student5);

    // add the students to the course objects
    course1.addStudent(student1);
    course1.addStudent(student2);
    course1.addStudent(student3);
    course1.addStudent(student5);

    course2.addStudent(student1);
    course2.addStudent(student2);
    course2.addStudent(student4);
    course2.addStudent(student5);

    course3.setStudent(student2);

    course4.setStudent(student3);

    course5.setStudent(student5);

    // add the courses to the student objects
    student1.addCourse(course1);
    student1.addCourse(course2);

    student2.addCourse(course1);
    student2.addCourse(course2);
    student2.addCourse(course3);

    student3.addCourse(course1);
    student3.addCourse(course4);

    student4.addCourse(course2);

    student5.addCourse(course1);
    student5.addCourse(course2);
    student5.addCourse(course5);

    // call the printInfo method
    printInfo(courseList, studentList);
    }

    public void printInfo(ArrayList<Course> courseList,
    ArrayList<Student> studentList) {
    for (Course course : courseList) {
    //check if course is a Regular Course
    if (course instanceof RegularCourse) {
    System.out.println(course.getTitle() + " is a regular course");
    ArrayList<Student> stuList = ((RegularCourse) course)
    .getAllStudents();
    for (Student student : stuList) {
    System.out.println(student.getName() + " is a student");

    }
    //check if course is Independent Study
    } else if (course instanceof IndependentStudy) {
    System.out.println(course.getTitle()
    + " is an independent course");
    if (course.getClass() == IndependentStudy.class) {

    System.out.println(((IndependentStudy) course).getStudent()
    .getName() + " is a student");
    }
    } else {
    System.out.println("something is wrong");
    }
    }
    //get student list and initialize variables a and b to zero where a counts the number of regular course and b counts the number of independent study classes
    for (Student stu : studentList) {
    ArrayList<Course> check = stu.getCourseList();

    int a = 0;
    int b = 0;
    //Identify students enrolled in Independent Study and also enrolled in at least one Regular Course
    for (Course course : check) {
    if (course instanceof RegularCourse) {
    a++;
    } else if (course instanceof IndependentStudy){
    b++;
    } else System.out.println("something is wrong");
    if (b > 0 && a > 0) {
    for (Course stulist: courseList){
    System.out.println(stu.getName() + " has " + course.getTitle());

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In each case it is printing the student and their first independent study course. And it is doing this five times.

    Java Code:
    //get student list and initialize variables a and b to zero where a counts the number of 
    //regular course and b counts the number of independent study classes
    for (Student stu : studentList) {
        ArrayList<Course> check = stu.getCourseList();
    
        int a = 0;
        int b = 0;
        //Identify students enrolled in Independent Study and also enrolled in at least one Regular Course
        for (Course course : check) {
            if (course instanceof RegularCourse) {
                a++;
            } else if (course instanceof IndependentStudy){
                b++;
            } else System.out.println("something is wrong");
            if (b > 0 && a > 0) {
                for (Course stulist: courseList){
                    System.out.println(stu.getName() + " has " + course.getTitle()); 
                }
            }
        }
    }

    The last for loop explains why the output appears 5 times. "for (Course stulist: courseList){" means that the loop will be executed once for every item in courseList - which is a full list of all five subjects.

    As for the course that is printed, that's because you say "course.getTitle()". Ie course is the same every time and it is the independent study course (the one that makes the "b>0&&a>0" condition true). If you add a student to two independent study courses you will see that the condition is true when either are being checked and you will end up with 10 lines of output for that student.

    Most likely you mean this for loop to loop over something more sensible. Something that will allow you to get at all the courses of this student. And perhaps it should go in a different place, like after the check has completely finished.

  6. #6
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    Thank you. This morning, with fresh eyes, I realized that I had to close my last for loop and move my student list out of the for loop...that fixed problem. :-)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Need Help With Assignment
    By zeo in forum New To Java
    Replies: 10
    Last Post: 02-25-2011, 12:09 AM
  2. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 11:32 PM
  3. Need with my assignment ...
    By allergy01 in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 08:33 AM
  4. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 01:27 AM
  5. I am looking for help with an assignment
    By nanoo51969 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •