-4

Average and Grade Calculation

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

char grade

Include appropriate getters and setters methods and constructor.

The getter method names should be getId, getName, getMarks, getAverage and getGrade and setter method names should be setId, setName, setMarks, setAverage and setGrade.

Write a public 3 argument constructor in the order – id, name and marks.

Write the below methods in Student class :

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

import java.util.*;

public class Student {
private int id;
private String name;
private int marks[];
private float average;
private char grade;

//Student(int id, String name, int marks[]){}
Student(){}

public void setId(int id) {
    this.id =id;
}
public void setName(String name) {
    this.name=name;
}
public void setMarks(int marks[]) {
    this.marks =marks;
}
public void setAverage(float average) {
    this.average=average;
}
public void setGrade(char grade) {
    System.out.println("grade");
    this.grade=grade;
}
public void calculateAvg() {
    float average = (Arrays.stream(marks).sum())/(marks.length);
    Student student = new Student();
    student.setAverage(average);
}
public void findGrade() {
    Student sm = new Student();
    if(average>80.0 && average <= 100.0) {
        sm.setGrade('O');
    }
    else if(average<50) {
        sm.setGrade('F');
    }
    else
        sm.setGrade('A');
}
public int getId() {
    return id;  
}
public String getName() {
    return name;
}
public int[] getMarks() {
    return marks;
}
public float getAverage() {
    System.out.println("average");
    return average;
}
public char getGrade() {
    System.out.println("null");
    return grade;
}

}

import java.util.Scanner;

public class StudentMain {

public static void main(String[] args) {
    StudentMain object = new StudentMain();
    object.getStudentDetails();

}
public static Student getStudentDetails() { 
    int i; int m[] = new int[100];
    Scanner sc = new Scanner(System.in);
    Student student = new Student();
    System.out.println("Enter the id:");
    int id= sc.nextInt();
    student.setId(id);sc.nextLine();
    System.out.println("Enter the name:");
    student.setName(sc.nextLine());
    do {
    System.out.println("Enter the no of subjects:");
    i = sc.nextInt();
    if(i==0) {
        System.out.println("Invalid number of subject");
        }
    }while(i==0);
    for(int j = 1;j<=i;j++) {
        System.out.println("Enter mark for subject "+j+":");
        m[j-1]=sc.nextInt();
    }
    student.setMarks(m);
    
    System.out.println("Id:"+student.getId());
    System.out.println("Name:"+student.getName());
    System.out.println("Average:"+student.getAverage());
    System.out.println("Grade:"+student.getGrade());
    return student;
}

}

After running the code, I figured out that setMarks(m) is not passing the array to the method present in Student class, and output is
Id: name : Avg:0.0 Grade: The Avg and Grade is not giving expected output.enter code here

  • 3
    When you write a method but don't use it when why do you expect it to be used anyway? – Tom Mar 17 '21 at 11:56
  • 2
    Please see [mcve]. You dont need to give us your whole assignment and ALL your code. Focus on the essential problem you need help with, and try to come up with code that only shows that problem. Doing so most likely allows you to solve the problem yourself already ... if not, you can take that to other people. Keep in mind that you want others to spend their time for you for free, so you please invest YOUR time to make that as simple and straight forward as possible. Me, honestly, I stopped reading after 3 lines, and I didnt even bother looking at your code. – GhostCat Mar 17 '21 at 11:57
  • 2
    And yes, the problem is most likely that your code is NOT calling the `findGrade()` method. Or the one to calculate averages. – GhostCat Mar 17 '21 at 11:59
  • No brother, I am not trying to spend other people time for free. I have been working on this problem for 2 days and still not able to figure out the mistake am I doing.. – User3323334 Mar 17 '21 at 12:08

2 Answers2

1

You must setAverage(). Since it is calculateAvg(int m[]) which does the work,

        
        student.setMarks(m);
        student.setAverage(student.calculateAvg(m)); // add this line

        System.out.println("Id:"+student.getId());
        System.out.println("Name:"+student.getName());
        System.out.println("Average:"+student.getAverage());
        System.out.println("Grade:"+student.getGrade());

To improve, your work states to have a public void calculateAvg(), for that, no need to pass marks[], but directly modify the internal average

// firstly remove parameter and update internal average
    public void calculateAvg() {
        this.average = ((float) Arrays.stream(marks).sum()) / (marks.length);
        // OR as spotted by @Lino
        // this.average = (float) Arrays.stream(marks).asDoubleStream().average().orElse(0.0);
    }

// secondly, in your main, just call calculateAvg()
        student.setMarks(m);
        student.calculateAvg();

        System.out.println("Id:"+student.getId());
        System.out.println("Name:"+student.getName());
        System.out.println("Average:"+student.getAverage());
        System.out.println("Grade:"+student.getGrade());
        return student;
g.momo
  • 536
  • 2
  • 7
  • `(Arrays.stream(marks).asLongStream().sum()) / (marks.length)` could also be written as `Arrays.stream(marks).average().orElse(0.0)`, see [`IntStream.average()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#average--) – Lino Mar 17 '21 at 12:26
  • Also note that your current method is using integer division which discards the values after the decimal point, which may be undesired – Lino Mar 17 '21 at 12:34
0

You said you "figured out that setMarks(m) is not passing the array to the method present in Student class". What makes you think this is the case?

Your getAverage() method returns the value of the field average. Where does this field get set?

You should debug your class by single-stepping through it in a debugger. If you haven't learned how to use a debugger yet, just put some temporary System.out.println traces. But it would be very worthwhile learning to use a debugger.

k314159
  • 5,051
  • 10
  • 32