Home How to call the method of HashMap from another class?
Reply: 0

How to call the method of HashMap from another class?

user2507
1#
user2507 Published in July 20, 2018, 1:18 am

In my assessment class, I have a method called addMark and it puts both studentid and mark into the hashmap called marks. I'm trying to put studentid and mark into the hashmap using student class but it keeps on giving me errors. Am I calling the assessment class wrongly? Can someone please help?

Student class:

public class Student {
 public void createStudents() {
   Mark m1 = new Mark(30, "Bad");
   Assessment.addMark("11", m1);
}

Mark class:

public class Mark {

private int mark;
private String comment;

public Mark(int mark){

}

public Mark(int mark, String comment){

}

public void setMark(int mark) throws Exception {
    /*
     * Start Preconditions
     */
    // Precondition: firstArgumentNonNegative
    if (mark < 0){
        throw new Exception("Precondition violated:"
                + " firstArgumentNonNegative");
    }
    /*
     * End Preconditions
     */
    this.mark = mark;
}

public int getMark() {
    return this.mark;
}

public void setComment(String comment){
    this.comment = comment;
}

public String getComment(){
    return this.comment;
}
}

Assessment class:

import java.util.HashMap;
import java.util.Map;

public abstract class Assessment {
private Map<String, Mark> marks = new HashMap<String, Mark>();


public String getAllStudentMarks(){
    String outString = "Student ID:\tMark:\tComment:";
    for (Map.Entry<String, Mark> entry : marks.entrySet()){
        String studentId = entry.getKey();
        Mark mark = entry.getValue();
        int value = mark.getMark();
        String comment = mark.getComment();
        outString += String.format("\n%s\t%d\t%s", studentId, value, comment);
    }
    return outString;
}

public boolean hasCompleted(String studentId){
    return marks.containsKey(studentId);
}


public void addMark(String studentId, Mark mark) throws Exception{
    /*
     * Start Preconditions
     */
    // Precondition: markNotGreaterThanWeight
    if (mark.getMark() > this.weight){
        throw new Exception("Precondition violated: "
                + "markNotGreaterThanWeight");
    }
    /*
     * End Preconditions
     */
    marks.put(studentId, mark);
}

public int getMark(String studentId){
    return marks.get(studentId).getMark();
}

public String getComment(String studentId){
    return marks.get(studentId).getComment();
}   

public abstract String description();
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323317 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO