question archive Average and Grade Calculation Develop a smart application as Student Grade Calculator(SGC)

Average and Grade Calculation Develop a smart application as Student Grade Calculator(SGC)

Subject:Computer SciencePrice:2.87 Bought7

Average and Grade Calculation

Develop a smart application as Student Grade Calculator(SGC).

Create class Student with following private attribute :

int id, String name, marks(integer array), float average and char 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 class StudentMain and write the main method.

In this class, write method

public static Student getStudentDetails() - this method should get the input from the user for a student, create 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

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:
public class Student  {
    String name;
    int id;
    float[] marks;
    char grade;
    float avgMark;

    public Student(String name, int id, int subjects) {
       setName(name);
       setId(id);
       setMarks(subjects);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setMarks(int numOfSubjects) {
        this.marks = new float[numOfSubjects];
    }


    public void calculateAvg() {
        for (float mark:
             marks) {
            avgMark += mark;
        }
        avgMark /= marks.length;
    }

    public void calculateGrade() {
        if (avgMark <= 100 && avgMark >= 80 ) {
            this.grade = 'O';
        }
        else if (avgMark >= 50 && avgMark <= 79) {
            this.grade = 'A';
        }
        else {
            this.grade = 'F';
        }
    }

    public float getAvgMark() {
        return avgMark;
    }

    public char getGrade() {
        return grade;
    }
}

 

 

 

import java.util.Scanner;

public class StudentMain {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
    Student student = getDetails();
        for (int i = 0; i < student.marks.length ; i++) {
            System.out.println("Enter marks for subject " + (i + 1)+":");
            student.marks[i] = sc.nextFloat();
            while (student.marks[i] < 0 || student.marks[i] > 100) {
                System.out.println("Invalid marks ");
                System.out.println("Enter marks for subject " + (i + 1) + " again");
                student.marks[i] = sc.nextFloat();
            }
        }

            student.calculateAvg();
            student.calculateGrade();
            System.out.println("ID "+student.getId());
            System.out.println("Name "+student.getName());
            System.out.printf("Average %.2f",student.getAvgMark());
            System.out.println();
            System.out.println("Grade "+student.getGrade());

    }

    public static Student getDetails() {
        String name;
        int id, numOfSubjects;

        System.out.println("Enter the id ");
        id = sc.nextInt();
        sc.nextLine();
        System.out.println("Enter the name ");
        name = sc.nextLine();
        System.out.println("Enter the number of Subjects ");
        numOfSubjects = sc.nextInt();
        while (numOfSubjects < 0 ) {
            System.out.println("Invalid number of subjects ");
            System.out.println("Enter the number of Subjects again ");
            numOfSubjects = sc.nextInt();
        }

        return new Student(name,id,numOfSubjects);
    }
}

Step-by-step explanation

Enter the id 

123

Enter the name 

Tom

Enter the number of Subjects 

3

Enter marks for subject 1:

95

Enter marks for subject 2:

80

Enter marks for subject 3:

75

ID 123

Name Tom

Average 83.33

Grade O

put them in same package and run StudentMain class