我正在尝试创建一个接受3个参数的类

时间:2018-06-16 21:19:38

标签: java class methods constructor private-members

我正在尝试创建一个接受3个参数,应用权重和计算总分的类。我做了一个班级和一个考试班。我在弄清楚如何调用方法来计算最终总数时遇到了麻烦。请帮帮忙?

此类编译正常,没有错误:

public class Grade {
     private double quiz;
     private double assign;
     private double disc;

public Grade(double pquiz, double passign, double pdisc){
    quiz = pquiz;
    assign = passign;
    disc = pdisc;
    }

    public void setQuiz(double pquiz){
        quiz = pquiz;
    }
    public void setAssign(double passign){
        assign = passign;
    }
    public void setDisc(double pdisc){
        disc = pdisc;
    }
    public double getQuiz(){
        return quiz;
    }
    public double getAssign(){
        return assign;
    }
    public double getDisc(){
        return disc;
    }
    public double calcQuiz(Grade quiz1){
        double wgtQuiz = quiz1.getQuiz() * .65;
        return wgtQuiz;
            }
    public double calcAssign(Grade assign1){
        double wgtAssign = assign1.getAssign() * .25;
        return wgtAssign;
            }
    public double calcDisc(Grade disc1){
        double wgtDisc = disc1.getDisc() * .10;
        return wgtDisc;
            }
    public double calcTotal(double wgtQuiz, double wgtAssign, double wgtDisc){
        double overall = wgtQuiz + wgtAssign + wgtDisc;
        return overall; 
        }
    }

请原谅这些缩进。到目前为止,这是测试类:

public class TestGrade{
  public state void main (String [] args){
    Grade quiz1 = new Grade(.95);
    Grade assign1 = new Grade(.91);
    Grade disc1 = new Grade(.87);

    double pQuiz = quiz1.getQuiz();
    double pAssign = assign1.getAssign();
    double pdisc = disc1.getDisc();

    double wgtQuiz = quiz1.calcQuiz(quiz1);
    double wgtAssign = assign1.calcAssign(assign1);
    double wgtDisc = disc1.calcDisc(disc1);

    double overall = ?????

请帮我完成这件事。

2 个答案:

答案 0 :(得分:0)

您班级的两项变更。

添加默认构造函数。保持字段私密,添加getter和setter。

    public Grade(){
     }

修改CalcTotal方法,不需要参数,使用字段应用日志来计算总数。

    public double calcTotal(){
        double overall = wgtQuiz + wgtAssign + wgtDisc;
         return overall; 
     }

创建成绩对象。在测试中使用成绩对象调用setter,getters和calcTotal方法。

   Grade grade = new Grade();
   // call setters on grade.
   // call calcTotal method and assert the value.

答案 1 :(得分:0)

成绩只是一个双倍,所以你不需要一个新课程,但如果你想要,你可以用双字段创建一个新的成绩等级......

的变化:

public class CalculateGrade {
    private double quiz;
    private double assign;
    private double disc;

    public CalculateGrade(double pquiz, double passign, double pdisc) {
        quiz = pquiz;
        assign = passign;
        disc = pdisc;
    }

    public double calcQuiz() {
        return quiz * .65;
    }

    public double calcAssign() {
        return assign * .25;
    }

    public double calcDisc() {
        return disc * .10;
    }

    public double calcTotal() {
        return calcQuiz() + calcAssign() + calcDisc();
    }
}


public class TestGrade {
    public static void main(String[] args) {
        CalculateGrade calculator = new CalculateGrade(.95, .91, .87);
        System.out.println("total: " + calculator.calcTotal()); // total: 0.9319999999999999
    }
}