??? /** * The Roster class encapsulates an ArrayList<Student> which stores the *
ID: 640333 • Letter: #
Question
???
/**
* The Roster class encapsulates an ArrayList<Student> which stores the
* information for each student in the gradebook.
*/
public class Roster {
// Declare mStudentList
private ArrayList<Student> mStudentList;
/**
* Roster()
*
* PSEUDOCODE: Create mStudentList.
*/
public Roster() {
mStudentList = new ArrayList<>();
}
/**
* addStudent()
*
* PSEUDOCODE: Add (append) pStudent to mStudentList.
*/
public void addStudent(Student pStudent) {
mStudentList.add(pStudent);
}
/**
* getStudent() Searches mStudentList for a Student with pLastName.
*
* PSEUDOCODE: index = Call Searcher.search(getStudentList(), pLastName) If
* index == -1 Then Return null Else return the Student object in
* mStudentList at index
*/
public getStudent(String pLastName) {
???
}
/**
* getStudentList() Accessor method for mStudentList.
*/
public ArrayList<Student> getStudentList() {
return mStudentList;
}
/**
* setStudentList() Mutator method for mStudentList.
*/
public void setStudentList(ArrayList<Student> pStudentList) {
mStudentList = pStudentList;
}
/**
* sortRoster() Called to sort the roster by last name.
*
* PSEUDOCODE: Call Sorter.sort() passing the list of students
*/
???
Explanation / Answer
main.java
import javax.swing.*;
import java.io.FileNotFoundException;
public class Main {
// The Roster of students that is read from "gradebook.txt".
private Roster mRoster;
// A reference to the View object.
private View mView;
public static void main(String[] pArgs) {
new Main().run();
}
//Exits the program when the exit button is clicked
public void exit() {
try {
GradebookWriter gradeBookWriter = new GradebookWriter("gradebook.txt");
gradeBookWriter.writeGradebook(getRoster());
System.exit(0);
} catch (FileNotFoundException pException) {
getView().messageBox("Could not open gradebook.txt for writing. Exiting without saving.");
System.exit(-1);
}
}
public Roster getRoster() {
return mRoster;
}
public void setRoster(Roster pRoster) {
mRoster = pRoster;
}
public View getView() {
return mView;
}
public void setView(View pView) {
mView = pView;
}
//run() is the main routine.
private void run() {
JFrame.setDefaultLookAndFeelDecorated(false);
setView(new View(this));
try {
GradebookReader pGradeBook = new GradebookReader("gradebook.txt");
setRoster(pGradeBook.readGradebook());
mRoster = getRoster();
} catch (FileNotFoundException pException) {
getView().messageBox("Could not open gradebook.txt for reading. Exiting.");
System.exit(-1);
}
}
//Search is called when the search button is clicked
public Student search(String pLastName) {
return getRoster().getStudent(pLastName);
}
}
GradebookReader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GradebookReader {
//Instance variable
private Scanner mIn;
//Attempts to open the gradebook file for reading.
public GradebookReader(String pFname) throws FileNotFoundException {
mIn = new Scanner(new File(pFname));
}
//Reads the exam scores for a Student.
private void readExam(Student pStudent) {
for (int n = 0; n < CourseConstants.NUM_EXAMS; ++n) {
pStudent.addExam(mIn.nextInt());
}
}
//Called to read the gradebook information.
public Roster readGradebook() {
Roster roster = readRoster();
roster.sortRoster();
return roster;
}
//Reads the homework scores for a Student.
private void readHomework(Student pStudent) {
for (int n = 0; n < CourseConstants.NUM_HOMEWORKS; ++n) {
pStudent.addHomework(mIn.nextInt());
}
}
//Reads the student information from the input file adding Student objecs to the roster.
private Roster readRoster() {
Roster roster = new Roster();
while (mIn.hasNext()) {
String lastName = mIn.next();
String firstName = mIn.next();
Student student = new Student(firstName, lastName);
readHomework(student);
readExam(student);
roster.addStudent(student);
}
return roster;
}
}
GradebookWriter.java
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class GradebookWriter extends PrintWriter {
//GradeWriter constructor
public GradebookWriter(String pFname) throws FileNotFoundException {
super(pFname);
}
//Writes the gradebook info to the file, which was opened in the ctor.
public void writeGradebook(Roster pRoster) {
for (Student student : pRoster.getStudentList()) {
println(student);
}
close();
}
}
gradebook.txt
Simpson Lisa 25 25 25 25 100 100
Flintstone Fred 15 17 22 18 80 60
Jetson George 20 21 22 23 70 83
Explosion Nathan 5 4 3 2 1 0
Muntz Nelson 20 15 10 5 60 70
Terwilliger Robert 23 21 19 17 80 90
Flanders Ned 12 14 17 23 85 95
Bouvier Selma 16 16 16 16 16 16
Spuckler Cletus 1 2 3 4 5 6
Wiggum Clancy 6 5 4 3 2 1
Skinner Seymour 19 23 21 24 78 83
Roster.java
import java.util.ArrayList;
public class Roster {
// Declare mStudentList
private ArrayList<Student> mStudentList;
public Roster() {
mStudentList = new ArrayList<>();
setStudentList(mStudentList);
}
//Add student to the list
public void addStudent(Student pStudent) {
mStudentList.add(pStudent);
}
//Get a student by calling searcher
public Student getStudent(String pLastName) {
int index = Searcher.search(getStudentList(), pLastName);
if (index == -1) {
return null;
} else
return mStudentList.get(index);
}
public ArrayList<Student> getStudentList() {
return mStudentList;
}
protected void setStudentList(ArrayList<Student> pStudentList) {
mStudentList = pStudentList;
}
//Sort the roster
public void sortRoster() {
//Sorter.sort(mStudentList.subList(1, mStudentList.size()));
Sorter.sort(mStudentList);
}
//Roster to String
@Override
public String toString() {
String result = "";
for (Student student : getStudentList()) result += student + " ";
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.