'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!
Solution 1:[1]
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;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | duffymo |
