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);
}
}
-------------------------------------------------
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
-------------------------------------------------
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.
getPercentage()
which gets the percentage of all marks using 100.0 * getAllSubjectsTotal() / 600.0;
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.printMarksSummary()
method to print the percentage using getPercentage()
and grade using getGrade()
.Student
which takes the name
and marks of all subjects as parameters.