Hal
Hal

Reputation: 372

How to call the method of HashMap from another class?

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();
}

Upvotes: 0

Views: 1193

Answers (2)

eigenharsha
eigenharsha

Reputation: 2231

I'm not able to get the exact question but through your code, I found.

  1. You need to extend abstract class Assessment ex. AssessmentManager

    class AssessmentManager extends Assessment
    
  2. Create an object of AssessmentManager and then use it.

    AssessmentManager _assessment = new AssessmentManager();
    _assessment.addMark(Student_ID, Marks)
    

Upvotes: 1

Bentaye
Bentaye

Reputation: 9756

Assessment is an abstract class so you will need to extend it if you want to use it:

class MyAssessment extends Assessment {
    @Override
    public String description() {
       return "My assessment";
    } 
}

Then you can call addMark on this new implementation

MyAssessment myAssessment = new MyAssessment();
myAssessment.addMark("11", m1);

Also this method throws an Exception so you need to try/catch it

try {
    new MyAssessment().addMark("11", m1);
} catch(Exception e) {
    e.printStackTrace();
}

And finally you have no weight property in your Assessment class so you should somehow define one if you want the class to compile.

private int weight = 0;

Upvotes: 3

Related Questions