• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Student schedule

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help with a program that will end up being a GUI student schedule. In this gui there will be two lists: one for students and another for courses. Clicking on a student will show the courses hes in; clicking on a course will show the students in the course. The students are read in from a text file, and the course are read in from a separate text file. Here is the code I got so far:

Im stumped on how to actually match the student with courses and vice versa.
Here is the info in the files just in case:
Students:
12411,Wegner,Tim,CS1337,ENGL1121,PHYS1211,MATH1304,CHEM1304
12412,Kowalski,Brian,CS1337,ENGL1121,PHYS1211,CHEM1304
12417,Ploski,Karen,CS1337,MATH2306,PHYS1211,ENGL1211,CHEM1304
12423,Selemon,Carol,CS1337,MATH2306,PHYS1211,CHEM1304
12427,Simpson,David,CS4340,MATH2306,PHYS1211,ENGL1211,CHEM1304
12429,Winter,Alyssa,CS1337,ENGL1121,MATH2306,CHEM104,PHYS1211
12432,Millen,Bob,CS2336,ENGL1121,MATH2306,CHEM104,PHYS1211
12436,Holley,Nancy,CS2336,ENGL1121,MATH2306,CHEM104,PHYS1211
12438,Barber,Chris,CS1337,ENGL1121,MATH1304,CHEM104,PHYS1211
12440,Ruiz,Victor,CS2336,ENGL1121,MATH2306,CHEM104,PHYS1211
12441,Jain,Ashok,CS2336,ENGL1121,MATH2306,CHEM104,PHYS1211
12447,Walker,Alan,CS2336,ENGL1121,MATH2306,CHEM104,PHYS1211
12451,Noble,Rich,CS2336,MATH1304,PHYS1211,ENGL1211,CHEM1304
12453,Rich,Joseph,CS1337,MATH2306,PHYS1211,ENGL1211,CHEM1304
12455,Chen,Alan,CS2336,MATH2306,PHYS1211,ENGL1211,CHEM1304

Courses:
CS1337,Computer Science I,MW12:30-1:45
ENGL1211,Rhetoric,MW9:00-10:15
PHYS1211,Basic Physics I,MW10:30-11:45
CHEM1304,Intro to Chemistry I,TTH9:30-10:45
MATH1304,Calculus I,TTH11:00-12:15
MATH2306,Differential Equations I,TTH11:00-12:15
CS4340,Computer Architecture,MW12:30-1:45
CS2336,Computer Science II,MW4:00-5:15
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some advice:
  • Use java.util.Lists instead of arrays; what if you get more than 100 students?
  • Use a List for your courses.

  • Now, instead of storing just the Course ID in your Student, you can store the Courses themselves (in a List). Since you have a List, you can add and remove Courses as needed.

    You'd also like to have an auxiliary method for finding a Course from an array or List based on the ID. Then you first read all the Courses, store them in a List. Next you read all the Students, and for each Course you look up the matching Course object and store that in a List inside the Student.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    what do you mean by java.util.Lists. arraylists?
    How would I store each course in a list.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is the way im thinking of how to do it. In the course class, I read the student file again; then I match the courseCode with the courseCode's in the student file. However, I dont understand how to make it reference what students each course was found for. Then I would attempt the same thing in the student class except the other way around.
     
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    List<Course> courses = new ArrayList<Course>();

    Worth reading the bit in the Java� Tutorials; it tells you about Lists.

    And welcome to JavaRanch
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I understand whats being said but I cant put it into motion without some visual reference of how to do it.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Can I do this program the way I got it set up?
     
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It looks like a sparse matrix will fit the bill well for this requirement.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is this going the right direction?
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's one way to do it.

    I wouldn't read the entire file over and over again though. Once you've read the students, you can iterate over the Student objects instead.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Rob is there a better way to get the courseCode rather than using a token. Because before I can run the code through all students, the courseCode has already changed.
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's how I would do it, in two parts.

    1. Create the courses: basically how you initially did it, except you use a List (ArrayList) to store them instead of an array.

    2. Create the students + link to course:

    All that you would need is to implement the linking between students and courses. Give either class a List with references to the other, whichever way you prefer.

    With this approach you only go through the student file once.
    [ November 04, 2008: Message edited by: Rob Prime ]
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so no ArrayList for the student? just for the course? also I see no creation of a Course class do I need one?
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    wait i think i see what you mean.
    ArrayList<Student> students = new ArrayList<Student>();
    ArrayList<Course> courses = new ArrayList<Course>();
    correct?
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    But probably better to be less specific in the declarations.

    List<Student> studentList = new ArrayList<Student>();
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Jerry Carpenter:
    also I see no creation of a Course class do I need one?


    You still need it, but it's the same as your original code. I was just lazy again and didn't feel like copying the code
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Im sorry I keep bothering you on this.
    But where are you telling me to put this code


    and then this code:

    Also I see you have references of students and courses. Should those be ArrayList as well to student and course
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is my new code put together; I dont know if its right or not.

    Now the linking process needs to be for both as in, I click on student and it shows their courses; clicking on the course will show the students in the course. So, my question is do I still only have to do the link only once in the student section?
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You only need the link in either. However, storing the information in both will be a lot easier. Course gets a List of Students that follow it and Student gets a List of Courses he/she follows.

    Of course you will need to ensure that both Lists are in synch - if a Student drops a Course, that Student also needs to be removed from the Course.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well all I need it to do is when I click on a student it show his courses. And when clicking on a course it show the students in that specific course. So, I dont need it to add or drop courses. Can this be done with the way its set up. Obviously, Im going to have two separate JList's.
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sure, no problem. The pseudo code "<link course to student>;" will then add the Student to the List of the Course, and vice versa.

    Then when a Student is selected in a List you can simply retrieve its Courses and vice versa.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok cool now this part gave me an error (course.id.equals(id)) so I changed it to (course.courseCode.equals(id)) should i have left that alone. Also, can I store the ArrayList students in a JList and the ArrayList courses in a JList as well.
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have corrected that mistake quite well. Sometimes when I type parts of code I make some mistakes, especially when I can't compile the whole of it.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Im not sure how to create the link, and how it would work. This will work in a gui interface right?
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Im still not sure what is meant by placing the student in the list of the course. How would I do that.
    Here is what I got for that code


    then if it does indeed match I would place that student with the course. I just dont understand how to make it happen. I dont even know if this code is how you get it done. Also, how is this going to work in the gui.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did a little more and I created another arraylist named sc

    is this the general idea or not?
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How are you going to get "course" equal to "course.courseCode?"

    You need to choose a different pair to check equality on.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so would I say course.equal(courseId)
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Jerry Carpenter:
    so would I say course.equal(courseId)

    No.

    And the method is called equals not equal. Read about it in the Object class; it takes Object as its parameter, but the two objects would have to be the same type (or very similar types).
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well I still dont understand what is meant by adding the student to the course "<link course to student>;". And how I would be able to represent that in a gui interface. Im sure you fella's are getting tired of my questions by now LOL
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Can't remember; it wasn't me who said that!

    And what does the equals() method in the Course class do?
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well it reads the courseId from the student and sends it to the findCourse method which iterates through the courses and if it finds a match it returns the course. Now evidently I am supposed to make a link between the two and thats where I am just flat out lost.
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And does it find the course? What format is the id in? Is it a primitive? Does course have a method which returns it? When you iterate through your List of courses, can you test for equality between the course id sought and the course id found?

    That is the one bit I have noticed. But if you get a bit to work you can put the whole thing together. You can build a large application from small parts, even copying parts over to make up the larger application.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    well my code above shows the layout of the program. and it does return a value
    and it is an object.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How exactly would I go about giving a class references to the other.
    code:
    [edit]Disable smilies. CR[/edit]
    [ November 11, 2008: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One way is to implement Student#enrolOnCourse(Course c) and Student#dropCourse(Course c) methods.

    You have some very old-fashioned code there; you are better off using a Scanner than StringTokenizer (read this) and you ought to parameterise your Lists.

    Instead of

    ArrayList courses = new ArrayList();

    write

    List<Course> courses = new ArrayList<Course>();

    Then you can forget all about the class casts.
     
    Jerry Carpenter
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Great after much headaches for me and probably you guys. I have finally got it all working. I appreciate all the advice, it really helped. This program relied heavily on objects which I was not that familiar with. But after over looking how this thing really works; I am really beginning to understand it.
    thanks fellas.
     
    Campbell Ritchie
    Marshal
    Posts: 79943
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done It is good to see somebody learning the concepts.
     
    What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic