user2998316
user2998316

Reputation: 23

Calculation coming out 0.00

I am trying to make a simple grade calculation program. But the problem is that it won't print out a value except 0.0. I have tried several different ways, and it just won't work. I would appreciate if someone looks through the code and gives me at least a hint of what I am doing wrong.

//SUBCLASS OF GRADED ACTIVITY
public class Essay {
    private double grammar;
    private double spelling;
    private double correctLength;
    private double content;
    private double score = 0;

    public void setScore(double gr, double sp, double len, double cnt) {
        grammar = gr;
        spelling = sp;
        correctLength = len;
        content = cnt;
    }

    public void setGrammer(double g) {
        this.grammar = g;
    }

    public void setSpelling(double s) {
        this.spelling = s;
    }

    public void setCorrectLength(double c) {
        this.correctLength = c;
    }

    public void setContent(double c) {
        this.content = c;
    }

    public double getGrammar() {
        return grammar;
    }

    public double getSpelling() {
        return spelling;

    }

    public double getCorrectLength() {
        return correctLength;

    }

    // CALCULATE THE SCORE
    public double getScore() {
        return score = grammar + spelling + correctLength + content;
    }

    public String toString() {
        //
        return "Grammar : " + grammar + " pts.\nSpelling : " + spelling + " pts.\nLength : " + correctLength +
                       " pts.\nContent : " + content + " pts." + "\nTotal score" + score;
    }
}


//Main demo
public class Main {
    public static void main(String[] args) {
        Essay essay = new Essay();
        essay.setGrammer(25);
        essay.setSpelling(15);
        essay.setCorrectLength(20);
        essay.setContent(28);
        System.out.println(essay.toString());
    }
}

Upvotes: 2

Views: 88

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

You aren't calling your getScore method anywhere, so the calculated value isn't being seen. You need to change your code to this:

public String toString() {
//
return "Grammar : " + grammar + " pts.\nSpelling : " + spelling
        + " pts.\nLength : " + correctLength + " pts.\nContent : "
        + content + " pts." + "\nTotal score" + getScore();
}

Upvotes: 2

Related Questions