entered in the array
import java.util.Scanner;
public class Student {
private String name;
private int[] grades;
private float finalGrade;
public Student(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void addGrade(int nbr) {
Scanner in = new Scanner(System.in);
grades = new int[nbr];
finalGrade = 0;
for (int i = 0; i < nbr; i++) {
System.out.print("Enter a grade: ");
grades[i] = in.nextInt();
finalGrade += grades[i];
}
finalGrade /= grades.length;
}
@Override
public String toString() {
return "Name of the student: " + name + " Final grade: " + finalGrade;
}
}
public class Test {
public static void main(String[] args) {
Student student = new Student("Mark");
student.setName("Tom");
student.addGrade(3);
System.out.println(student);
}
}
Comments
Leave a comment