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

How to call the method of HashMap from another class?

user9964
1#
user9964 Published in September 19, 2018, 11:32 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();
}
share|improve this question
  • What errors ? at compile time or at run time ? – Bentaye Apr 16 at 12:45
  • method addMark is not static, so you need to call it via an object, not a class name – HBo Apr 16 at 12:45
  • 1
    also you can't do this.weight in Assessment, it does not have a weight property – Bentaye Apr 16 at 12:47
  • your Assessment class is abstract, you will need to extend it to use it. – Bentaye Apr 16 at 12:49
  • Use IllegalArgumentException, not Exception. (Forcing callers to catch Exception will cause the many unchecked exceptions which indicate programmer errors to be suppressed.) – VGR Apr 16 at 13:46

2 Answers 2

active oldest votes
up vote 3 down vote accepted
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO