如何从另一个类调用HashMap的方法?

时间:2018-04-16 12:42:31

标签: java class methods hashmap

在我的评估课中,我有一个名为addMark的方法,它将studentid和mark放入名为marks的hashmap中。我试图使用student class将studentid和mark放入hashmap,但它一直在给我错误。我是否错误地打电话给评估班?有人可以帮忙吗?

学生班:

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

评估班:

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

2 个答案:

答案 0 :(得分:3)

Assessment是一个抽象类,所以如果你想使用它,你需要扩展它:

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

然后你可以在这个新实现上调用addMark

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

此方法也会抛出Exception,因此您需要尝试/捕捉它

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

最后,你的weight类中没有Assessment属性,所以如果你想要编译类,你应该以某种方式定义一个属性。

private int weight = 0;

答案 1 :(得分:1)

我无法得到确切的问题,但通过您的代码,我找到了。

  1. 您需要扩展抽象类Assessment ex。 AssessmentManager

    class AssessmentManager extends Assessment
    
  2. 创建AssessmentManager的对象,然后使用它。

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