aspose file tools*
The moose likes Beginning Java and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
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

Joined: Mar 16, 2001
Posts: 984

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.


Please ignore post, I have no idea what I am talking about.
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
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

Joined: Mar 16, 2001
Posts: 984

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

Joined: Mar 16, 2001
Posts: 984

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

Joined: Jan 20, 2003
Posts: 68
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

Joined: Mar 16, 2001
Posts: 984

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

Joined: Jan 20, 2003
Posts: 68
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

Joined: Mar 16, 2001
Posts: 984

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

Joined: Jul 09, 2001
Posts: 1879

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap
 
Similar Threads
retrieving array
Returning values from a one method in a class....to another method in a class?
maps of classes of enums
[Resolved]java.util.NoSuchElementException problem after displaying the information from text file
Student schedule