java program: urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niranjanapte
    New Member
    • Feb 2008
    • 1

    java program: urgent

    hello
    In my assignment we asked to create classes like student, grade, semester, course
    all of them have constructors & methods.
    We have to create hashmap that contains key as student id & object of studentRecord(a nother class which contains student data)
    then we have to create treeset of grades in the student record so that all the grades will be sorted by semester wise.
    I am not able to create a treeset because it accept the grade as raw type when I did tht I can't call the iterator
    Set<Grade> t = new TreeSet<Grade>( );
    t.add(g1)
    Iterator <key>i = m.iterator<key> (); -> asks for expression.
    I created the generic method printmap like the following
    public <key> void printSet(key g, TreeSet<key> m)
    {
    m.add(g);
    System.out.prin tln("TreeSet of Grades: ");
    Iterator <key>i = m.iterator<key> ();
    }
    }

    In the class called StudentDatabase [contains main() ] hashmap is created <string, studentRecord> then its printed
    then I want to call a method printSet to print treeset

    SO how to use iterator of reference or raw types (like Object)
    please reply soon, it's urgent
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Does the grade class implement the Comparable interface? If not, you need to since TreeSet/TreeMap both use compareTo to sort. For the HashMaps, make sure you write hashCode() and equals() methods since those are how the HashMap sorts. More information on the latter two is available in the Howtos section (this is important), and the API has full information all 3.

    Comment

    Working...