Can anyone help completing the code for these three classes in Java: Please fill
ID: 3798285 • Letter: C
Question
Can anyone help completing the code for these three classes in Java:
Please fill in code where the question marks(???) are. The pseudocode is below. Thanks!
________________________________________________________________________________________
4.4 GradebookWriter A class which writes the gradebook information to gradebook.txt before the program exits. This class inherits from java.io. PrintWriter. This class is very simple. Read the comments and implement the pseudocode.
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.
*/
???
__________________________________________________________________________________________________
4.5 Roster Stores the student information in an ArrayList<Student> list. Read the comments and implement the pseudocode
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;
}
}
______________________________________________________________________________________________
4.8 Student The Student class stores the information for one student. Read the comments and implement the pseudocode.
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
*/
???
}
/**
* 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()
*/
???
}
Explanation / Answer
Hi,
Please see below the classes. Please comment for any queries/feedbacks.
Thanks,
Anita
//----------------------------------------
//Searcher.java
//----------------------------------------
package p03;
import java.util.ArrayList;
public class Searcher {
/**
* search method to search the given lastName in a studentList
*
* @param studentList
* @param lastName
* @return
*/
public static int search (ArrayList<Student> studentList , String lastName){
int index = -1;
for(int i=0;i<studentList.size();i++){
Student st = studentList.get(i);
if(lastName.equalsIgnoreCase(st.getLastName())){
index = i;
}
}
return index;
}
}
//----------------------------------------
//Sorter.java
//----------------------------------------
package p03;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Sorter {
/**
*
* sort method - to sort the given studentList
*
* @param studentList
*/
public static void sort(ArrayList<Student> studentList ) {
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2){
return s1.getLastName().compareTo(s2.getLastName());
}
}); //
}
}
//----------------------------------------
//Roster.java
//----------------------------------------
package p03;
import java.util.ArrayList;
/**
* The Roster class encapsulates an ArrayList<Student> which stores the information for each student in the gradebook.
*/
public class Roster {
// Declare mStudentList
ArrayList studentList;
/**
* Roster()
*
* PSEUDOCODE:
* Create mStudentList.
*/
public Roster(){
studentList = new ArrayList<Student>() ;
}
/**
* addStudent()
*
* PSEUDOCODE:
* Add (append) pStudent to mStudentList.
*/
public void addStudent(Student student){
this.getStudentList().add(student);
}
/**
* 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 Student getStudent(String pLastName){
int index = Searcher.search(getStudentList(), pLastName);
if(index == -1){
return null;
}
else{
return this.getStudentList().get(index);
}
}
/**
* getStudentList()
* Accessor method for mStudentList.
*/
public ArrayList<Student> getStudentList() {
return this.studentList;
}
/**
* setStudentList()
* Mutator method for mStudentList.
*/
public void setStudentList(ArrayList<Student> pStudentList) {
this.studentList = pStudentList;
}
/**
* sortRoster()
* Called to sort the roster by last name.
*
* PSEUDOCODE:
* Call Sorter.sort() passing the list of students
*/
public void sortRoster(){
Sorter.sort(this.studentList);
}
/**
* 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.java
//----------------------------------------
package p03;
import java.util.ArrayList;
/**
* The Student class stores the grade information for one Student.
*/
public class Student implements Comparable<Student> {
// Declare the instance variables.
private String pFirstName;
private String pLastName;
private ArrayList mExamList;
private ArrayList mHomeworkList;
/**
* Student()
*
* PSEUDOCODE:
* Save pFirstName and pLastName.
* Create mExamList
* Create mHomeworkList
*/
public Student(String pFirstName, String pLastName){
this.pFirstName =pFirstName;
this.pLastName = pLastName;
this.mExamList = new ArrayList();
this.mHomeworkList = new ArrayList();
}
/**
* addExam()
*
* PSEUDOCODE:
* Call add(pScore) on getExamList() to add a new exam score to the list of exam scores.
*/
public void addExam(int pScore){
this.getExamList().add(pScore);
}
/**
* addHomework()
*
* PSEUDOCODE:
* Call add(pScore) on getHomeworkList() to add a new homework score to the list of homework scores.
*/
public void addHomework(int pScore){
this.getHomeworkList().add(pScore);
}
/**
* 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.
*/
public int compareTo(Student pStudent) {
return this.pLastName.compareTo(pStudent.getLastName()) ;
}
/**
* 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 this.pFirstName;
}
/**
* 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 this.pLastName;
}
/**
* 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) {
this.pFirstName = 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) {
this.pLastName = 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
*/
public String toString(){
String retString = "";
retString = retString + "lastname : "+this.pFirstName;
retString = retString + "firstname : "+this.pLastName;
for(int i=0 ;i < this.mHomeworkList.size();i++){
retString = retString + "hw"+i+" : "+this.getHomework(i);
}
for(int i =0 ;i < this.mExamList.size();i++){
retString = retString + "exam"+i+" : "+this.getExam(i);
}
return retString;
}
}
//----------------------------------------
//GradebookWriter.java
//----------------------------------------
package p03;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* GradebookWriter inherits from PrintWriter and writes the gradebook info to the file name passed to the ctor.
*/
public class GradebookWriter extends PrintWriter {
private Roster pRoster;
/**
* GradebookWriter()
* Call the super class ctor that takes a String.
* @throws FileNotFoundException
*/
public GradebookWriter(Roster roster) throws FileNotFoundException{
super("GradeInfo.txt");
this.pRoster =roster;
}
/**
* 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()
*/
public void writeGradebook(){
for(Student student : pRoster.getStudentList()){
System.out.println(student);
super.println(student.toString()); //Calling print
super.flush(); //flush will write the contents
}
}
}
//----------------------------------------
//Tester.java
//----------------------------------------
package p03;
import java.io.FileNotFoundException;
public class Tester {
public static void main(String [] args) throws FileNotFoundException{
//creating Student objects
Student s1= new Student("Geo", "Lan");
Student s2= new Student("Kevin", "Peter");
s1.addExam(50);
s1.addExam(30);
s1.addHomework(39);
s1.addHomework(40);
s2.addExam(20);
s2.addExam(60);
s2.addHomework(49);
s2.addHomework(20);
Roster roster = new Roster(); //Craeting roster
roster.addStudent(s1);
roster.addStudent(s2);
GradebookWriter gradeWriter = new GradebookWriter(roster);
gradeWriter.writeGradebook(); //Writing
}
}
GradeInfo.txt
lastname : Geofirstname : Lanhw0 : 39hw1 : 40exam0 : 50exam1 : 30
lastname : Kevinfirstname : Peterhw0 : 49hw1 : 20exam0 : 20exam1 : 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.