简体   繁体   中英

How do i do math with object instance variables once i have created the object in java

I have constructed an object in java with integer instance variables, how do find the quotient of two of these variables after i have created the object.

The objects i have created are like this:

class Student(int score, int marks)

where score is the score he received in the test and marks is the max score in the test, how can i find the percentage he got, after i have created multiple students like:

Student student1 = new Student(60, 90);

where i want the program to print:

"student1 got "+((score/marks)*100)+"%"

Thanks guys!

Here's what I'd recommend:

class Student {

    private int grade; 
    private int maxGrade;

    public Student(int grade, int maxGrade) {
        this.grade = grade;
        this.maxGrade = maxGrade;
    }

    public double getPercentage() {
        return 100.0 * grade/maxGrade;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM