//// Course Constants //// package p03; public class CourseConstants { public st
ID: 668546 • Letter: #
Question
//// Course Constants ////
package p03;
public class CourseConstants {
public static final int NUM_EXAMS = 2;
public static final int NUM_HOMEWORKS = 4;
}
/// GRADEBOOK READER ////
package p03;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* GradebookRead reads the gradebook info from the file name passed to the ctor.
*/
public class GradebookReader {
private Scanner mIn;
/**
* Attempts to open the gradebook file for reading. If successful, mIn will be used to read from the file. If the file
* cannot be opened, a FileNotFoundException will be thrown.
*/
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. Calls readRoster() to read the student records and then sorts the roster
* by last name.
*/
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;
}
}
//// GRADEBOOK WRITER //////
package p03;
???
/**
* GradebookWriter inherits from PrintWriter and writes the gradebook info to the file name passed to the ctor.
*/
public class GradebookWriter extends PrintWriter {
/**
* GradebookWriter()
* Call the super class ctor that takes a String.
*/
???
/**
* writeGradebook()
* Writes the gradebook info to the file, which was opened in the ctor.
*
* PSEUDOCODE:
* EnhancedFor each student in pRoster.getStudentList() Do
* Call println(student)
* End For
* Call close()
*/
???
}
////// ROSTER //////
package p03;
???
/**
* The Roster class encapsulates an ArrayList<Student> which stores the information for each student in the gradebook.
*/
public class Roster {
// Declare mStudentList
???
/**
* Roster()
*
* PSEUDOCODE:
* Create mStudentList.
*/
???
/**
* addStudent()
*
* PSEUDOCODE:
* Add (append) pStudent to mStudentList.
*/
???
/**
* 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
*/
???
/**
* 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
*/
???
/**
* Returns a String representation of this Roster. Handy for debugging.
*/
@Override
public String toString() {
String result = "";
for (Student student : getStudentList()) result += student + " ";
return result;
}
}
/////// STUDENT /////
package p03;
???
/**
* The Student class stores the grade information for one Student.
*/
public class Student implements Comparable<Student> {
// Declare the instance variables.
???
/**
* Student()
*
* PSEUDOCODE:
* Save pFirstName and pLastName.
* Create mExamList
* Create mHomeworkList
*/
???
/**
* addExam()
*
* PSEUDOCODE:
* Call add(pScore) on getExamList() to add a new exam score to the list of exam scores.
*/
???
/**
* addHomework()
*
* PSEUDOCODE:
* Call add(pScore) on getHomeworkList() to add a new homework score to the list of homework scores.
*/
???
/**
* compareTo()
*
* PSEUDOCODE:
* Return: -1 if the last name of this Student is < the last name of pStudent
* Return: 0 if the last name of this Student is = the last name of pStudent
* Return: 1 if the last name of this Student is > the last name of pStudent
* Hint: the last names are Strings.
*/
???
/**
* getExam()
*
* Accessor method to retreive an exam score from the list of exams.
*/
public int getExam(int pNum) {
return getExamList().get(pNum);
}
/**
* getExamList()
*
* Accessor method for mExamList.
*/
protected ArrayList<Integer> getExamList() {
return mExamList;
}
/**
* getFirstName()
*
* Accessor method for mFirstName.
*/
public String getFirstName() {
return mFirstName;
}
/**
* getHomework()
*
* Accessor method to retrieve a homework score from the list of homeworks.
*/
public int getHomework(int pNum) {
return getHomeworkList().get(pNum);
}
/**
* getHomeworkList()
*
* Accessor method for mHomeworkList.
*/
protected ArrayList<Integer> getHomeworkList() {
return mHomeworkList;
}
/**
* getLastname()
*
* Accessor method for mLastName.
*/
public String getLastName() {
return mLastName;
}
/**
* setExam()
*
* Mutator method to store an exam score into the list of exam scores.
*/
public void setExam(int pNum, int pScore) {
getExamList().set(pNum, pScore);
}
/**
* setExamList()
*
* Mutator method for mExamList.
*/
protected void setExamList(ArrayList<Integer> pExamList) {
mExamList = pExamList;
}
/**
* setFirstName()
*
* Mutator method for mFirstName.
*/
public void setFirstName(String pFirstName) {
mFirstName = pFirstName;
}
/**
* setHomework()
*
* Mutator method to store a homework score into the list of homework scores.
*/
public void setHomework(int pNum, int pScore) {
getHomeworkList().set(pNum, pScore);
}
/**
* setHomeworkList()
*
* Mutator method for mHomeworkList.
*/
protected void setHomeworkList(ArrayList<Integer> pHomeworkList) {
mHomeworkList = pHomeworkList;
}
/**
* setLastname()
*
* Mutator method for mLastName.
*/
public void setLastName(String pLastName) {
mLastName = pLastName;
}
/**
* toString()
*
* Returns a String representation of this Student. The format of the returned string shall be such that the Student
* information can be printed to the output file, i.e:
*
* lastname firstname hw1 hw2 hw3 hw4 exam1 exam2
*/
???
}
/////// VIEW ///////
package p03;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* The View class implements the GUI.
*/
public class View extends JFrame implements ActionListener {
public static final int FRAME_WIDTH = 500;
public static final int FRAME_HEIGHT = 250;
// Declare instance variables
???
/**
* View()
*
* The View constructor creates the GUI interface and makes the frame visible at the end.
*/
public View(Main pMain) {
// Save a reference to the Main object pMain in mMain.
???
// PSEUDOCODE:
// Create a JPanel named panelSearch which uses the FlowLayout.
// Add a JLabel "Student Name: " to panelSearch
// Create mSearchText and make the field 25 cols wide
// Add mSearchText to the panel
// Create mSearchButton
// Make this View the action listener for the button
// Add the button to the panel
???
// PSEUDOCODE:
// Create a JPanel named panelHomework which uses the FlowLayout.
// Add a JLabel "Homework: " to the panel
// Create mHomeworkText which is an array of CourseConstants.NUM_HOMEWORKS JTextFields
// For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
// Create textfield mHomeworkText[i] displaying 5 cols
// Add mHomeworkText[i] to the panel
// End For
???
// Create the exam panel which contains the "Exam: " label and the two exam text fields. The pseudocode is omitted
// because this code is very similar to the code that creates the panelHomework panel.
???
// PSEUDOCODE:
// Create a JPanel named panelButtons using FlowLayout.
// Create the Clear button mClearButton.
// Make this View the action listener for mClearButton.
// Add the Clear button to the panel.
// Repeat the three above statements for the Save button.
// Repeat the three above statements for the Exit button.
???
// PSEUDOCODE:
// Create a JPanel named panelMain using a vertical BoxLayout.
// Add panelSearch to panelMain.
// Add panelHomework to panelMain.
// Add panelExam to panelMain.
// Add panelButtons to panelMain.
???
// Initialize the remainder of the frame, add the main panel to the frame, and make the frame visible.
setTitle("Gradebookulator");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panelMain);
setVisible(true);
}
/**
* actionPerformed()
*
* Called when one of the JButtons is clicked. Detects which button was clicked and handles it.
*
* PSEUDOCOODE:
* If the source of the event was the Search button Then
* lastName = retrieve the text from the mSearchText text field.
* If lastName is the empty string Then
* Call messageBox() to display "Please enter the student's last name."
* Else
* student = Call mMain.search(lastName)
* If student is null Then
* Call messageBox() to display "Student not found. Try again."
* Else
* Call displayStudent(student)
* End if
* End If
* Else if the source of the event was the Save button Then
* If mStudent is not null Then Call saveStudent(mStudent)
* Else if the source of the event was the Clear button Then
* Call clear()
* Else if the source of the event was the Exit button Then
* If mStudent is not null Then Call saveStudent(mStudent)
* Call mMain.exit() to terminate the application
* End If
*/
???
/**
* clear()
*
* Called when the Clear button is clicked. Clears all of the text fields by setting the contents to the empty string.
* After clear() returns, no student information is being edited or displayed.
*
* PSEUDOCODE:
* Set the mSearchText text field to ""
* Set each of the homework text fields to ""
* Set each of the exam text fields to ""
* Set the mStudent reference to null
*/
???
/**
* displayStudent()
*
* Displays the homework and exam scores for a student in the mHomeworkText and mExamText text fields.
*
* PSEUDOCODE:
* For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
* int hw = pStudent.getHomework(i)
* String hwstr = convert hw to a String (Hint: Integer.toString())
* mHomeworkText[i].setText(hwstr)
* End For
* Write another for loop similar to the one above to place the exams scores into the text fields
*/
???
/**
* messageBox()
*
* Displays a message box containing some text.
*/
public void messageBox(String pMessage) {
JOptionPane.showMessageDialog(this, pMessage, "Message", JOptionPane.PLAIN_MESSAGE);
}
/**
* saveStudent()
*
* Retrieves the homework and exam scores for pStudent from the text fields and writes the results to the Student record
* in the Roster.
*
* PSEUDOCODE:
* For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
* String hwstr = mHomeworkText[i].getText()
* int hw = convert hwstr to an int (Hint: Integer.parseInt())
* Call pStudent.setHomework(i, hw)
* End For
* Write another for loop similar to the one above to save the exam scores
*/
???
}
/////// MAIN //////
package p03;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
/**
* The Main class containing the main() and run() methods.
*/
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;
/**
* This is where execution starts. Instantiate a Main object and then call run().
*/
public static void main(String[] pArgs) {
???
}
/**
* exit() is called when the Exit button in the View is clicked.
*
* PSEUDOCODE:
* try
* Instantiate a GradebookWriter object opening "gradebook.txt" for writing
* Call writeGradebook(getRoster()) on the object
* Call System.exit(0) to terminate the application
* catch FileNotFoundException
* Call messageBox() on the View to display ""Could not open gradebook.txt for writing. Exiting without saving."
* Call System.exit(-1) to terminate the application with an error code of -1
*/
???
/**
* Accessor method for mRoster.
*/
public Roster getRoster() {
return mRoster;
}
/**
* Accessor method for mView.
*/
public View getView() {
return mView;
}
/**
* run() is the main routine.
*
* PSEUDOCODE:
* Call JFrame.setDefaultLookAndFeelDecorated(true or false depending on your preference)
* Call setView(instantiate new View object passing this to the ctor)
* try
* Instantiate a GradebookReader object to open "gradebook.txt" for reading
* Call readGradebook() on the object
* Call setRoster() to save the Roster returned from readGradebook()
* catch
* Call messageBox() on the View to display "Could not open gradebook.txt for reading. Exiting."
* Call System.exit(-1) to terminate the application with an error code
*/
???
/**
* search() is called when the Search button is clicked on the View. The input parameter is the non-empty last name
* of the Student to locate. Call getStudent(pLastName) on the Roster object to get a reference to the Student with
* that last name. If the student is not located, getStudent() returns null.
*/
???
/**
* Mutator method for mRoster.
*/
public void setRoster(Roster pRoster) {
mRoster = pRoster;
}
/**
* Mutator method for mView.
*/
public void setView(View pView) {
mView = pView;
}
}
Explanation / Answer
package p03;
public class CourseConstants {
public static final int NUM_EXAMS = 2;
public static final int NUM_HOMEWORKS = 4;
}
/// GRADEBOOK READER ////
package p03;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* GradebookRead reads the gradebook info from the file name passed to the ctor.
*/
public class GradebookReader {
private Scanner mIn;
/**
* Attempts to open the gradebook file for reading. If successful, mIn will be used to read from the file. If the file
* cannot be opened, a FileNotFoundException will be thrown.
*/
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. Calls readRoster() to read the student records and then sorts the roster
* by last name.
*/
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;
}
}
//// GRADEBOOK WRITER //////
package p03;
???
/**
* GradebookWriter inherits from PrintWriter and writes the gradebook info to the file name passed to the ctor.
*/
public class GradebookWriter extends PrintWriter {
/**
* GradebookWriter()
* Call the super class ctor that takes a String.
*/
???
/**
* writeGradebook()
* Writes the gradebook info to the file, which was opened in the ctor.
*
* PSEUDOCODE:
* EnhancedFor each student in pRoster.getStudentList() Do
* Call println(student)
* End For
* Call close()
*/
???
}
////// ROSTER //////
package p03;
???
/**
* The Roster class encapsulates an ArrayList<Student> which stores the information for each student in the gradebook.
*/
public class Roster {
// Declare mStudentList
???
/**
* Roster()
*
* PSEUDOCODE:
* Create mStudentList.
*/
???
/**
* addStudent()
*
* PSEUDOCODE:
* Add (append) pStudent to mStudentList.
*/
???
/**
* 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
*/
???
/**
* 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
*/
???
/**
* Returns a String representation of this Roster. Handy for debugging.
*/
@Override
public String toString() {
String result = "";
for (Student student : getStudentList()) result += student + " ";
return result;
}
}
/////// STUDENT /////
package p03;
???
/**
* The Student class stores the grade information for one Student.
*/
public class Student implements Comparable<Student> {
// Declare the instance variables.
???
/**
* Student()
*
* PSEUDOCODE:
* Save pFirstName and pLastName.
* Create mExamList
* Create mHomeworkList
*/
???
/**
* addExam()
*
* PSEUDOCODE:
* Call add(pScore) on getExamList() to add a new exam score to the list of exam scores.
*/
???
/**
* addHomework()
*
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.