• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two classes course and sutdent. This is the code for course and would like to add a course mark for each student in the course class. Any idea is very much appreciated.
import java.util.HashMap;
/**
* Record details of courses offered in the University.
* This class also maintains students registered in a course.
*
* @author Mulugeta Maru
* @version 2003.03.03
*/
public class Course
{
private String courseCode;
private String description;
private int maxClassSize;
private HashMap students;
/**
* Constructor for objects of class Course.
* @param Course code.
* @param Course description.
* @param Maximum number of students that can register in the course.
*/
public Course(String courseCode, String description, int maxClassSize)
{
this.courseCode = courseCode;
this.description = description;
this.maxClassSize = maxClassSize;
students = new HashMap();
}
Thank you.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well at the moment you don't have any referance to the "student" class in this one.
like to add a course mark for each student

To do that I would suggest you add a private attribute to the "student" class with public get and set methods.
 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did consider your suggestion adding student mark in the student class, however, a student may be registered in more than one course. If I have a private attribute in the student class it will only store one value for each student object.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given two classes; "Student" and "Class", you could have "Class" contain one or more "Student"s OR "Student" contain one or more "Class"es.
But from your questions it sounds like you need more classes. (How can you ever have too many classes?)
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my last post didn't help in any way please post a description of the problem, and or more code. Thanks.
 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me clarify the problem. I have two classes STUDENT AND COURSE.As you can see below I am storing student objects in class COURSE using a HashMap � i.e. students registered in the course. How can I store marks for each student in a course in COURSE class?
public class Course
{
private String courseCode;
private String description;
private int maxClassSize;
private HashMap students;
Thank you.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you are going to have a difficult time of it if you are restricting yourself to just these two classes, and you want them to both have information about each other.
The only thing I can think of is to have an inner class in the COURSE class. This inner class could be called something like "StudentGrade" and would consist of 1] a STUDENT instance , and 2] a grade. The COURSE class would than store one or more "StudentGrade"s in the hashmap, instead of STUDENT. Does that make any sense?
 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. I was hoping that there is a way to do it without using an innner class or a separate class. I am going to create a separate class called COURSEGRADES which will have two variables studentid and mark and I can store the studentid and the coursegrades object in the HashMap. Does this make sense?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that sounds like a ok solution. I guess I still don't understand why you were reluctent to have another class.
With only two classes you were trying to do a "many to many" relationship which isn't really doable.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, if it isn't too late, you might want to have even more classes:
Student - any info about the student
-->Name, student number, address, etc.
CourseMaster - about a course( can be offered anytime )
-->CourseID, course name, course description, length ( maybe effective dates )
CourseOffering - a course that is offered for students to register in
-->CourseOfferId, Course_ID, start date, finish date, maximum enrollment number, minimum enrollment, fees, campus, ( maybe even room assignment and professor assignment if this is a java course assignment )
StudentCourse - a student's status in a particlur course offering
-->Student_ID, Offer_id, status( registered, withdrawn, etc )
StudentGrade - a student's mark in a course
-->Student_ID, CourseID, grade
This may be a little overboard, but I think you have too many things embedded within one class. It won't allow any flexibility, and the class would be hard to maintain.
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic