Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Student student1 = new Student(60, 90);

where i want the program to print:

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

Thanks guys!

>Solution :

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;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading