Develop a smart application as Student Grade Calculator(SGC).
Create a class Student with following private attribute :
int id
,String name
,marks
(integer array),float average
andchar grade
. Include appropriate getters and setters methods and constructor.
public void calculateAvg()
- This method should calculate average and set average mark for the current student.public void findGrade()- This method should set the grade based on the average calculated. If the average is between 80 and 100 then, then return grade as 'O', else 'A' .If the student gets less than 50 in any of the subjects then return grade as 'F'. Using appropriate setter method set the grade to the student.
(Note : number of subject should be greater than zero, if not display as 'Invalid number of subject' and get number of subject again, Assume mark for a subject should be in the range 0 - 100. If not display a message "Invalid Mark" and get the mark again)
Write a class StudentMain and write the main method.
In this class, write a method
public static Student getStudentDetails()
- this method should get the input from the user for a student, create a student object with those details and return that object.In main create student’s object by invoking the getStudentDetails method. Also calculate average and grade for that student object using appropriate methods.
SGC app should get the input and display the output as specified in the snapshot:
Sample Input 1: Enter the id: 123 Enter the name: Tom Enter the no of subjects: 3 Enter mark for subject 1: 95 Enter mark for subject 2: 80 Enter mark for subject 3: 75 Sample Output 1: Id:123 Name:Tom Average:83.33 Grade:O Sample Input 2: Enter the id: 123 Enter the name: Tom Enter the no of subjects: 0 Invalid number of subject Enter the no of subjects: 3 Enter mark for subject 1: 75 Enter mark for subject 2: 49 Enter mark for subject 3: 90 Sample Output 2: Id:123 Name:Tom Average:71.33 Grade:F
public class Student{
private int id;
private String name;
private Integer[] marks;
private float average;
private char grade;
public Student(int id, String name, Integer[] marks){
this.id=id;
this.name=name;
this.marks=marks;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public Integer[] setMarks(Integer[] marks){
return this.marks = getMarks();
}
public Integer[] getMarks(){
return marks;
}
public void setAverage(float average){
this.average=average;
}
public float getAverage(){
return average;
}
public void setGrade(char grade){
this.grade=grade;
}
public char getGrade(){
return grade;
}
public void calculateAvg(){
StudentMain sm = new StudentMain ();
int sum=0;
//int no=0;
for(int i=0;i<marks.length;i++){
sum+=marks[i];
}
float avg=sum/sm.n;
//System.out.println(sum);
setAverage(avg);
}
public void findGrade(){
float avg=getAverage();
if (avg>+80 && avg<+100){
setGrade('O');
}
else if (avg>=50 && avg<80){
setGrade('A');
}
else {
setGrade('F');
}
}
}
import java.util.*;
import java.io.*;
public class StudentMain{
static Scanner sc = new Scanner(System.in);
static int n;
static List<Integer> al = new ArrayList<Integer>();
public static Student getStudentDetails(){
System.out.println("Enter the id:");
int id=sc.nextInt();
System.out.println("Enter name:");
String name=sc.next();
System.out.println("Enter the no of subjects");
n = sc.nextInt();
if (n<=0){
System.out.println("Invalid number of subjects");
System.out.println("Enter the number of subjects");
n=sc.nextInt();
}
//List<Integer> al = new ArrayList<Integer>();
for (int i=0; i<n;i++){
int t=i+1;
System.out.println("Enter mark for subject" +t);
int m =sc.nextInt();
//List<Integer> al = new ArrayList<Integer>();
//System.out.println(m);
al.add(m);
}
Integer[] marks = new Integer[al.size()];
for (int i =0; i<al.size(); i++){
marks[i] = al.get(i);
}
for (Integer x : marks){
//return marks;
}
//int [] marks = null;
Student s = new Student(id,name,marks);
return s;
}
public static void main (String[] args) {
Student s2 = getStudentDetails();
System.out.println("Id:" + s2.getId());
System.out.println("Name:" + s2.getName());
s2.calculateAvg();
System.out.println("Average:"+s2.getAverage());
s2.findGrade();
System.out.println("Grade:"+s2.getGrade());
}
}