Create a class to maintain a GradeBook . The class should store information on u
ID: 3684863 • Letter: C
Question
Create a class to maintain a GradeBook. The class should store information on up to 20 students. The information for each student should be encapsulated in a Student class, which should include the student's last name and up to 10 grades for the student. Note that less than 10 grades may sometimes be stored. Your GradeBook class should contain an array of Student objects and at least support operations for the client to add a student object to the end of the book (i.e., the book does not have to be in alphabetical order), and to list all student objects (name and associated grades) it contains. To add a student to the end of the book, the client should not have to specify a position, (ie. your GradeBook should keep track of the last position and add to the end automatically). Your Student class should include at least the set functions to allow setting of the last name and setting up to 10 grades (one per function call), and to return the name and grades (one per function call) for that student. It should not accept test scores less than 0 or greater than 100 (i.e., no extra credit).You should write a main program that creates a grade book and presents a menu to the user that allows them to select either Add (A), or List (L), or Quit (Q). Add should create a Student object, allow the user to enter a student record (name and grades), and add it to the end of the Gradebook list. List should list all student records currently in the grade book. You should be able to add and list repeatedly, until you select Q to quit.
Thanks for your answer.
Explanation / Answer
Hello there , please find code below and the output too.
package com.dipal;
import java.util.ArrayList;
/**
* GradeBook containing Student grade information.
* @author welcome
*/
public class GradeBook {
ArrayList<Student> studentList=new ArrayList<Student>(20);
/**
* It adds a new student to Student list.
* @param student
* @return
*/
public boolean addStudent(Student student){
return studentList.add(student);
}
/**
* It list out all the students.
*/
public void listAllStudents(){
for(Student student: studentList){
System.out.println(student.toString());
}
}
}
=============
package com.dipal;
import java.util.ArrayList;
/**
* Student class containing name and grades.
*
* @author welcome
*/
public class Student {
String name;
ArrayList<Integer> grades = new ArrayList<Integer>(10);
/**
* Sets the name of the students.
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* Adds grade of the student.
*
* @param grade
*/
public void addGrade(int grade) {
grades.add(grade);
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", grades=" + grades + '}';
}
}
===========
package com.dipal;
import java.util.Scanner;
/**
* This is the main application for making a gradebook.
*
* @author dipal
*/
public class GradeApp {
public static void main(String arg[]) {
Scanner scanner = new Scanner(System.in);
GradeBook gradeBook = new GradeBook();
System.out.println("Select an option:");
//Continue until quite
while (true) {
System.out.println("Add (A), or List (L), or Quit (Q)");
String option = scanner.next();
if (option.equals("A")) {
System.out.println("Please enter the Student name :");
Student student = new Student();
student.setName(scanner.next());
System.out.println("How many grades does the student have:");
int no = scanner.nextInt();
for (int i = 0; i < no; i++) {
System.out.println("grade" + (i + 1) + ":");
while (true) {
int grade = scanner.nextInt();
if (grade > 0 && grade < 100) {
student.addGrade(grade);
break;
} else {
System.out.println("Enter grade again .Grade should be less than 100 and greater than 0.");
}
}
}
gradeBook.addStudent(student); //Add student to gradebook.
} else if (option.equals("L")) {
gradeBook.listAllStudents(); //List all Students in a gradebook.
} else if (option.equals("Q")) {
System.exit(0); //quite.
}
}
}
}
Output:
run:
Select an option:
Add (A), or List (L), or Quit (Q)
A
Please enter the Student name :
John
How many grades does the student have:
3
grade1:
90
grade2:
0
Enter grade again .Grade should be less than 100 and greater than 0.
89
grade3:
99
Add (A), or List (L), or Quit (Q)
A
Please enter the Student name :
Morgan
How many grades does the student have:
10
grade1:
9
grade2:
79
grade3:
89
grade4:
88
grade5:
69
grade6:
599
Enter grade again .Grade should be less than 100 and greater than 0.
59
grade7:
98
grade8:
89
grade9:
80
grade10:
85
Add (A), or List (L), or Quit (Q)
L
Student{name=John, grades=[90, 89, 99]}
Student{name=Morgan, grades=[9, 79, 89, 88, 69, 59, 98, 89, 80, 85]}
Add (A), or List (L), or Quit (Q)
Q
BUILD SUCCESSFUL (total time: 50 seconds)
Let me know if you have queries. Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.