Skip to main content

Practical-13

Write a Java Program to create a Student class and generate result of student (Total, Per, Grade).

  1. Introduction

The Student class is an important part of the Java language. It helps to create and manage a student’s data, such as their name, their grade, and the amount of time they have remaining in the course. It also allows for the generation of a student’s final result, which is the combination of their total marks, their percentage obtained and the corresponding grade. This program also has the capability to be modified to suit the user’s requirements, such as changing the class structure for a particular student or increasing a student’s time limit.

Code

// class student
class Student {

private String name;
private int totalMarks;
private float per;
private char grade;

// constructor
public Student(String name, int totalMarks) {
this.name = name;
this.totalMarks = totalMarks;
}

// setting percentage
public void setPer() {
per = totalMarks / 500;
}

// setting grade
public void setGrade() {
if (per >= 0.9) {
grade = 'A';
} else if (per >= 0.8) {
grade = 'B';
} else if (per >= 0.7) {
grade = 'C';
} else if (per >= 0.6) {
grade = 'D';
} else {
grade = 'E';
}
}

// generating result
public void generateResult() {
setPer();
System.out.println("Total Marks:"+ totalMarks);
System.out.println("Name of student:" + name);
System.out.println("Percentage:" + per);
System.out.println("Grade:" + grade);
}

}

Output

Total Marks:450
Name of student: John
Percentage: 0.9
Grade: A
Explanation

The Student class is a java class that creates a student object with a name, total marks and a grade. The constructor creates the objects with these values and the generateResult() method calculates the percentage and grade of a student based on the total marks and prints them out. This method also calls setPer() and setGrade() to calculate and set the percentage and grade.