Create a class called Student that contains A field to stor
Create a class called Student that contains: - A field to store the name - A field to store their average numerical grade - A field to store their letter grade - A constructor that sets the information in all the fields - Appropriate accessor methods Write a main class that asks the user the number of students in the class (n). Then prompt for the information for all students and create a Student object for each one. Use an ArrayList to store all the Student objects created. Your ArrayList must be of type student. After all the data is entered, write a loop that iterates through your array and displays a neatly formatted gradebook, with the averages rounded to 1 decimal place: Example Output: Name | Average | Grade Stew Dent | 78.3 | C Joe Schmoe | 91.1 | A . . . ...up to n students
Assume 20 for the maximum length of the name.
Solution
Student.java
public class Student
{
//Declaring instance variables
private String name;
private double average;
private char grade;
//Creating Parameterized Constructor
public Student(String name, double average, char grade) {
super();
this.name = name;
this.average = average;
this.grade = grade;
}
//Settersa nd getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
}
_____________________________
Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Declaring variables
int no_of_students;
String name;
double average;
char grade;
Student st = null;
//Scanner class object is used to read the values entered by the user
Scanner sc = new Scanner(System.in);
//Getting the no of students in the class
System.out.print(\"Enter number of students in the class :\");
no_of_students = sc.nextInt();
//Creating an Array List which holds Student class objects
ArrayList<Student> al = new ArrayList<Student>(no_of_students);
/* This for loop will get the information from the
* user and populate them into ArrayList
*/
for (int i = 0; i < no_of_students; i++) {
sc.nextLine();
//Getting the Name entered by the user
System.out.print(\"Enter Student Name :\");
name = sc.nextLine();
//Getting the Student average entered by the suer
System.out.print(\"Enter Student Average :\");
average = sc.nextDouble();
//Getting the grade entered by the user
System.out.print(\"Enter Grade :\");
grade = sc.next(\".\").charAt(0);
//Creating the Student class object by passing the arguments
st = new Student(name, average, grade);
//Adding the student class object to the array list
al.add(st);
}
//Displaying the Student Details
System.out.println(\"Name\\t\\t\\tAverage\\tGrade\");
for (Student s : al) {
System.out.print(s.getName() + \"\\t\\t\" + s.getAverage() + \"\\t\\t\"+ s.getGrade() + \"\ \");
}
}
}
____________________________
Output:
Enter number of students in the class :3
Enter Student Name :Stew Dent
Enter Student Average :78.3
Enter Grade :C
Enter Student Name :Joe Schmoe
Enter Student Average :91.1
Enter Grade :A
Enter Student Name :Kane Williams
Enter Student Average :93.3
Enter Grade :A
Name Average Grade
Stew Dent 78.3 C
Joe Schmoe 91.1 A
Kane Williams 93.3 A


