Re: how to search an array of objects?



Lew wrote:
public class Student
{
private String name;
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
private int module;
public int getModule() { return module; }
public void setModule( int module ) { this.name = module; }
private float mark;
public float getMark() { return mark; }
public void setMark( float mark ) { this.name = mark; }
}

Ewww. Student is a value object with no behavior and no likely implementation changes save to add or remove fields, so it should really just be

public class Student {
public final String name;
public int module; // Consider making this an object too
public float mark; // Consider making this an int, 1-100,
// or even an enum with A, B, C, D, F
public Student (String name, int module, float mark) {
this.name = name;
this.module = module;
this.mark = mark;
}
}

Name made final since it's used as a map key.

Of course, a student is actually likely to have marks in many classes, which suggests

public class Student {
public final String name;
// And address, and GPA, and other stuff
// equals() and hashCode() methods
}

public class Course { whatever } // or Module or whatever

public class SchoolInfo {
private Map<String, Student> students;
private Map<Student, List<Course>> whosTakingWhat;
private Map<Course, List<Student>> whosInWhat;
private static class StudentInCourse {
public final Student student;
public final Course course;
public StudentInCourse (Student student,Course course) {
this.student = student;
this.course = course;
}
// obvious equals() and hashCode() go here
}
private Map<StudentInCourse, Integer> marks;
// whatever
}

Or perhaps even:

public class Course {
private Map<Student, Integer> marks;
public Set<Student> getTakers () { return marks.keySet(); }
public boolean contains (Student student) {
return marks.containsKey(student);
}
/**
* @throws NPE if student isn't taking this course
*/
public int getMark (Student student) {
return marks.get(student).intValue();
}
public int setMark ...
}

and SchoolInfo just has Map<String, Student> students and Map<String, Course> courses, with Course responsible for listings its students and SchoolInfo able to supply an inner class instance giving a Set view of the courses a given student is taking by using the objects in "courses" and their knowledge of their students (via "contains")...
.



Relevant Pages

  • Re: Looking for vid of Wing Tsun being used in actual fight
    ... all over him, wouldn't you, Mark? ... The challenge was answered by the junior student to the elder student. ... With no frame of reference with which to set ... old video, with crowds and pedigrees in tow. ...
    (rec.martial-arts)
  • Re: OT: Everyone born after a certain date is ignorant and can be dismissed
    ... that what matters in an assessment is not the mark, ... the non-exam work of a student, ... If the assessment is merely to be made on the exam then were are back to where we began, ie, how to assess if random scoring can score. ... Raising the pass mark does not help you to distinguish between everyone getting A* grades. ...
    (uk.sport.cricket)
  • Re: OT: Everyone born after a certain date is ignorant and can be dismissed
    ... that what matters in an assessment is not the mark, ... the non-exam work of a student, ... I said "with the ability being assessed" not "with the ... worth pointing out that most pass marks for exams in the UK are pretty ...
    (uk.sport.cricket)
  • Re: Finding concentration of caffeine
    ... Mark wrote: ... > A student wants to find the concentration of caffeine in coffee beans. ... we have instruments to do these analyses these ...
    (sci.chem)
  • Re: XmlSerialization base classes
    ... was important because of who the XmlSerializer works. ... /// Provides public access to the private ... I used the particular overload is because Student is derived from Person... ... named InternationalPhone derives from it. ...
    (microsoft.public.dotnet.framework.aspnet)