• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic