Assignment - Saving Grade Book Entries Add a method to the GradeBook that saves
ID: 643675 • Letter: A
Question
Assignment - Saving Grade Book Entries
Add a method to the GradeBook that saves its entries.
Put each entry on its own line, with a separator between values.
Hint: Class Integer's static parseInt method can turn a string into it's int value.
Also write a method that can read them back in from the file.
Don't worry about saving the details of the person class with the grade book entry.
import java.util.ArrayList;
import java.util.Scanner;
public class GradeBook {
private String course;
private ArrayList<Person> students = new ArrayList<Person>();
private ArrayList<GradeBookEntry> entries = new ArrayList<GradeBookEntry>();
public GradeBook(String course){
this.course = course;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public void addStudent( Person student ){
students.add(student);
}
public void addEntry(){
System.out.println("Grade which student: ");
for (int i = 0; i < students.size(); i++) {
System.out.println(i + " " + students.get(i).getName());
}
Scanner reader = new Scanner(System.in);
int studentIndex = reader.nextInt();
reader.nextLine();
System.out.println("Enter the assessment name: ");
String assessmentName = reader.nextLine();
System.out.println("Homework (1) or exam (2): ");
int entryType = reader.nextInt();
reader.nextLine();
GradeBookEntry newEntry;
// TODO: create either a Homework or Exam entry
// TODO: add getData method to the Homework and Exam
newEntry.getData();
newEntry.setStudent(students.get(studentIndex));
entries.add(newEntry);
}
public void listGrades(){
for( int i=0; i<entries.size(); i++ ){
GradeBookEntry entry = entries.get(i);
entry.printEntry();
// This could also be condensed to one line:
// entries.get(i).printEntry();
}
}
public void displaySummary(){
// TODO: show a distribution of grades in this class.
// See the barchart example in Java HTP 7.4
// Initialize the chart
String[] distribution = new String[5];
distribution[0] = "F: ";
distribution[1] = "D: ";
distribution[2] = "C: ";
distribution[3] = "B: ";
distribution[4] = "A: ";
for( GradeBookEntry entry : entries ){
if( entry.getLetterGrade().equals("F")){
distribution[0] += "*";
}
if( entry.getLetterGrade().equals("D")){
distribution[1] += "*";
}
if( entry.getLetterGrade().equals("C")){
distribution[2] += "*";
}
if( entry.getLetterGrade().equals("B")){
distribution[3] += "*";
}
if( entry.getLetterGrade().equals("A")){
distribution[4] += "*";
}
}
for( int i=0; i<distribution.length; i++ ){
System.out.println(distribution[i]);
}
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class GradeBook
{
private String course;
private ArrayList<Person> students = new ArrayList<Person>();
private ArrayList<GradeBookEntry> entries = new ArrayList<GradeBookEntry>();
private ArrayList<Double> home=new ArrayList<Double>();
private ArrayList<Double> exam=new ArrayList<Double>();
public GradeBook(String course)
{
this.course = course;
}
public String getCourse()
{
return course;
}
public void setCourse(String course)
{
this.course = course;
}
public void addStudent( Person student )
{
students.add(student);
}
public void addEntry()
{
System.out.println("Grade which student: ");
for (int i = 0; i < students.size(); i++)
{
System.out.println(i + " " + students.get(i).getName());
}
Scanner reader = new Scanner(System.in);
int studentIndex = reader.nextInt();
//reader.nextLine();
System.out.println("Enter the assessment name: ");
String assessmentName = reader.nextLine();
System.out.println("Homework (1) or exam (2): ");
int entryType = reader.nextInt();
//reader.nextLine();
String ch="y";
int i=0;
double exam;
GradeBookEntry newEntry=new GradeBookEntry(studentIndex);
// TODO: create either a Homework or Exam entry
if(entryType==1)
{
do
{
System.out.println("Enter Homework "+i+" marks:");
home.add(reader.nextDouble());
System.out.println("Want to enter more home work marks. Press 'Y' or 'y' to continue.");
ch=reader.next();
}while(ch.equalsIgnoreCase("y"));
//if the user wants to quit the loop, the user presses a character other than y.
//then after quitting, the home work marks are added to the Person
students.get(studentIndex).setHomeWorkMarks(ArrayList<Double> home);
}
else if(entryType==2)
{
do
{
System.out.println("Enter exam "+i+" marks:");
home.add(reader.nextDouble());
System.out.println("Want to enter more exam marks. Press 'Y' or 'y' to continue.");
ch=reader.next();
}while(ch.equalsIgnoreCase("y"));
//if the user wants to quit the loop, the user presses a character other than y.
//then after quitting, the exam marks are added to the Person
students.get(studentIndex).setExamMarks(ArrayList<Double> exam);
}
students.get(studnetIndes).getData();
newEntry.setStudent(students.get(studentIndex));
entries.add(newEntry);
}
public void listGrades()
{
for( int i=0; i<entries.size(); i++ )
{
GradeBookEntry entry = entries.get(i);
entry.printEntry();
// This could also be condensed to one line:
// entries.get(i).printEntry();
}
}
public void displaySummary()
{
// TODO: show a distribution of grades in this class.
// See the barchart example in Java HTP 7.4
// Initialize the chart
String[] distribution = new String[5];
distribution[0] = "F: ";
distribution[1] = "D: ";
distribution[2] = "C: ";
distribution[3] = "B: ";
distribution[4] = "A: ";
for( GradeBookEntry entry : entries )
{
if( entry.getLetterGrade().equals("F"))
{
distribution[0] += "*";
}
if( entry.getLetterGrade().equals("D"))
{
distribution[1] += "*";
}
if( entry.getLetterGrade().equals("C"))
{
distribution[2] += "*";
}
if( entry.getLetterGrade().equals("B"))
{
distribution[3] += "*";
}
if( entry.getLetterGrade().equals("A"))
{
distribution[4] += "*";
}
}
for( int i=0; i<distribution.length; i++ )
{
System.out.println(distribution[i]);
}
}
void readDataFromFile() throws Exception
{
File f=new File("FileName.txt");
Scanner s=new Scanner(f);
String line;
if(f.exist())
{
while(s.hasNextLine())
{
line=s.nextLine();
System.out.println(line);
}
}
else
{
System.out.println("Sorry! File is not found.");
}
}
}
This method should be present in GradeBookEntry.
//Assuming, ArrayList of person class is also present in GradeBookEntry.
//So, depending on the object created in the addEntry method, the getDataMethod is called.
//Assuming int index is present in GradeBookEntry which is initialised by the object.
void getDataMethod()
{
String s=""+person.get(index).getName();
s+=","+person.get(index).getId();
s+=","+person.get(index).getHomeWorkMarks();
s+=","+person.get(index).getExamMarks();
s+=","+person.get(index).getTotal();
s+=" ";
System.out.println(s);
}
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class GradeBook
{
private String course;
private ArrayList<Person> students = new ArrayList<Person>();
private ArrayList<GradeBookEntry> entries = new ArrayList<GradeBookEntry>();
private ArrayList<Double> home=new ArrayList<Double>();
private ArrayList<Double> exam=new ArrayList<Double>();
public GradeBook(String course)
{
this.course = course;
}
public String getCourse()
{
return course;
}
public void setCourse(String course)
{
this.course = course;
}
public void addStudent( Person student )
{
students.add(student);
}
public void addEntry()
{
System.out.println("Grade which student: ");
for (int i = 0; i < students.size(); i++)
{
System.out.println(i + " " + students.get(i).getName());
}
Scanner reader = new Scanner(System.in);
int studentIndex = reader.nextInt();
//reader.nextLine();
System.out.println("Enter the assessment name: ");
String assessmentName = reader.nextLine();
System.out.println("Homework (1) or exam (2): ");
int entryType = reader.nextInt();
//reader.nextLine();
String ch="y";
int i=0;
double exam;
GradeBookEntry newEntry=new GradeBookEntry(studentIndex);
// TODO: create either a Homework or Exam entry
if(entryType==1)
{
do
{
System.out.println("Enter Homework "+i+" marks:");
home.add(reader.nextDouble());
System.out.println("Want to enter more home work marks. Press 'Y' or 'y' to continue.");
ch=reader.next();
}while(ch.equalsIgnoreCase("y"));
//if the user wants to quit the loop, the user presses a character other than y.
//then after quitting, the home work marks are added to the Person
students.get(studentIndex).setHomeWorkMarks(ArrayList<Double> home);
}
else if(entryType==2)
{
do
{
System.out.println("Enter exam "+i+" marks:");
home.add(reader.nextDouble());
System.out.println("Want to enter more exam marks. Press 'Y' or 'y' to continue.");
ch=reader.next();
}while(ch.equalsIgnoreCase("y"));
//if the user wants to quit the loop, the user presses a character other than y.
//then after quitting, the exam marks are added to the Person
students.get(studentIndex).setExamMarks(ArrayList<Double> exam);
}
students.get(studnetIndes).getData();
newEntry.setStudent(students.get(studentIndex));
entries.add(newEntry);
}
public void listGrades()
{
for( int i=0; i<entries.size(); i++ )
{
GradeBookEntry entry = entries.get(i);
entry.printEntry();
// This could also be condensed to one line:
// entries.get(i).printEntry();
}
}
public void displaySummary()
{
// TODO: show a distribution of grades in this class.
// See the barchart example in Java HTP 7.4
// Initialize the chart
String[] distribution = new String[5];
distribution[0] = "F: ";
distribution[1] = "D: ";
distribution[2] = "C: ";
distribution[3] = "B: ";
distribution[4] = "A: ";
for( GradeBookEntry entry : entries )
{
if( entry.getLetterGrade().equals("F"))
{
distribution[0] += "*";
}
if( entry.getLetterGrade().equals("D"))
{
distribution[1] += "*";
}
if( entry.getLetterGrade().equals("C"))
{
distribution[2] += "*";
}
if( entry.getLetterGrade().equals("B"))
{
distribution[3] += "*";
}
if( entry.getLetterGrade().equals("A"))
{
distribution[4] += "*";
}
}
for( int i=0; i<distribution.length; i++ )
{
System.out.println(distribution[i]);
}
}
void readDataFromFile() throws Exception
{
File f=new File("FileName.txt");
Scanner s=new Scanner(f);
String line;
if(f.exist())
{
while(s.hasNextLine())
{
line=s.nextLine();
System.out.println(line);
}
}
else
{
System.out.println("Sorry! File is not found.");
}
}
}
This method should be present in GradeBookEntry.
//Assuming, ArrayList of person class is also present in GradeBookEntry.
//So, depending on the object created in the addEntry method, the getDataMethod is called.
//Assuming int index is present in GradeBookEntry which is initialised by the object.
void getDataMethod()
{
String s=""+person.get(index).getName();
s+=","+person.get(index).getId();
s+=","+person.get(index).getHomeWorkMarks();
s+=","+person.get(index).getExamMarks();
s+=","+person.get(index).getTotal();
s+=" ";
System.out.println(s);
}
Note:
Just assumed the GradeBookEntry class, there are lot of confusions present in the code.
In general, the marks are assigned in the persons class, if any total or average calculations are done in GradBookEntry class by calling each persons data.
Then a Test class is used to test the entries.
So far some assumptions are made and code is given. Hope this could help you out to certain point.