Menu
Topics Index
...
`


Classes >
Siva Nookala - 12 Apr 2016
As discussed in Behavior Of Java Classes Using Methods we can create methods in a class. This program shows how we can create multiple methods in a class and how we can call one method from another method.

Student class with multiple methods
class StudentClassWithMultipleMethods
{
    public static void main(String arg[])
    {
        Student srini = new Student();
        srini.name = "Srinivas";
        srini.english = 87;
        srini.telugu = 84;
        srini.hindi = 76;
        srini.maths = 100;
        srini.science = 92;
        srini.social = 88;
        
        System.out.println("-------------------------------------------------");
        System.out.println("Detailed Marks");
        System.out.println("-------------------------------------------------");
        srini.printMarksDetails();
        System.out.println("-------------------------------------------------");
        System.out.println();
        System.out.println("-------------------------------------------------");
        System.out.println("Summary Marks");
        System.out.println("-------------------------------------------------");
        srini.printMarksSummary();
        System.out.println("-------------------------------------------------");    
    }
}

class Student
{
    String name;
    // Marks in various subjects
    int english;
    int telugu;
    int hindi;
    int maths;
    int science;
    int social;

    int getLanguagesTotal()
    {
        return english + telugu + hindi;
    }

    int getNonLanguagesTotal()
    {
        return maths + science + social;
    }

    int getAllSubjectsTotal()
    {
        return getLanguagesTotal() + getNonLanguagesTotal();
    }

    double getPCMPercentage()
    {
        return 100.0 * (maths + science) / 200.0;
    }

    void printMarksSummary()
    {
        System.out.println("Languages: " + getLanguagesTotal());
        System.out.println("Non languages: " + getNonLanguagesTotal());
        System.out.println("PCM Percentage: " + getPCMPercentage());
        System.out.println("Total: " + getAllSubjectsTotal());
    }

    void printMarksDetails()
    {
        System.out.println("English : " + english);
        System.out.println("Telugu : " + telugu);
        System.out.println("Hindi : " + hindi);
        System.out.println("Maths : " + maths);
        System.out.println("Science : " + science);
        System.out.println("Social : " + social);
    }
}
OUTPUT

-------------------------------------------------
Detailed Marks
-------------------------------------------------
English : 87
Telugu : 84
Hindi : 76
Maths : 100
Science : 92
Social : 88
-------------------------------------------------

-------------------------------------------------
Summary Marks
-------------------------------------------------
Languages: 247
Non languages: 280
PCM Percentage: 96.0
Total: 527
-------------------------------------------------

DESCRIPTION

Here we have create a Student class with member variables name, english, telugu, hindi, maths, science and social. The method getLanguagesTotal() returns the sum of marks in all languages i.e. sum of marks in english, hindi and telugu. Similarly getNonLanguagesTotal() returns the total of maths, science and social. When getAllSubjectsTotal() is called, we could either sum all the subjects like english + hindi + telugu + maths + science + social or simply sum the result of the methods getLanguagesTotal() and getNonLanguagesTotal(). We have used the second approach here. We have also implemented the print methods to print the details of the subjects.

THINGS TO TRY
  • Add one more method getPercentage() which gets the percentage of all marks using 100.0 * getAllSubjectsTotal() / 600.0;
  • Add String getGrade() method, which gives the grade like 'Distinction' for percentage greater than 70, 'First Class' for percentage greater than 60, 'Second Class' for percentage greater than 35 and 'Fail' if percentage less than 35.
  • Change the printMarksSummary() method to print the percentage using getPercentage() and grade using getGrade().
  • Create a constructor for Student which takes the name and marks of all subjects as parameters.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App