GradeBook.java: /** * GradeBook stores the grades of students and retrieves data
ID: 3534162 • Letter: G
Question
GradeBook.java:/**
* GradeBook stores the grades of students and retrieves data
* by scanning a file
*
*/ import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeBook { private List<Student> roster;
/**
* Construtor creates a gradebook from a file of given data
* @param fileName the name of the file holding the data
*/
//Creates gradeBook, gets data from fileName
public GradeBook(String fileName) {
roster = readFromFile(fileName);
}
/**
* Store student's data
* @param fileName
* @return students
*/ //Creates scanner inFile
public List<Student> readFromFile( String fileName ) {
List<Student> students = new ArrayList<Student>();
Scanner inFile;
try {
inFile = new Scanner(new File( fileName ));
//error program cannot find file
} catch (FileNotFoundException e) {
System.err.println("File (" + fileName + ") not found");
return students;
}
try {
//where data is stored
while (inFile.hasNext()) {
String studentData = inFile.nextLine();
//create scanner stud
Scanner stud = new Scanner(studentData);
String name = stud.next();
String ID = stud.next();
int q0 = stud.nextInt();
int q1 = stud.nextInt();
int q2 = stud.nextInt();
int q3 = stud.nextInt();
int q4 = stud.nextInt();
int fin = stud.nextInt();
String gender = stud.next();
stud.close();
boolean isMale = gender.equals("true");
Student s = new Student(name, ID, q0, q1, q2, q3, q4, fin, isMale);
students.add(s);
}
//error within file
} catch (Exception e) {
System.err.println("Error in data file format; returning roster 'to date'");
}
inFile.close();
return students;
}
public int classSize(int students){
int classSize;
for (classSize=0;classSize< students;classSize++)
return classSize;
}
public String printRoster(String roster){
return roster;
}
}
GradeBookMain.java:
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; ? public class GradeBookMain { public static void main(String[] args) { GradeBook gb = new GradeBook("CS007.txt"); System. out.println(gb.classSize()); System. out.println(gb.printRoster()); } } GradeBook.java:
/**
* GradeBook stores the grades of students and retrieves data
* by scanning a file
*
*/ import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeBook { private List<Student> roster;
/**
* Construtor creates a gradebook from a file of given data
* @param fileName the name of the file holding the data
*/
//Creates gradeBook, gets data from fileName
public GradeBook(String fileName) {
roster = readFromFile(fileName);
}
/**
* Store student's data
* @param fileName
* @return students
*/ //Creates scanner inFile
public List<Student> readFromFile( String fileName ) {
List<Student> students = new ArrayList<Student>();
Scanner inFile;
try {
inFile = new Scanner(new File( fileName ));
//error program cannot find file
} catch (FileNotFoundException e) {
System.err.println("File (" + fileName + ") not found");
return students;
}
try {
//where data is stored
while (inFile.hasNext()) {
String studentData = inFile.nextLine();
//create scanner stud
Scanner stud = new Scanner(studentData);
String name = stud.next();
String ID = stud.next();
int q0 = stud.nextInt();
int q1 = stud.nextInt();
int q2 = stud.nextInt();
int q3 = stud.nextInt();
int q4 = stud.nextInt();
int fin = stud.nextInt();
String gender = stud.next();
stud.close();
boolean isMale = gender.equals("true");
Student s = new Student(name, ID, q0, q1, q2, q3, q4, fin, isMale);
students.add(s);
}
//error within file
} catch (Exception e) {
System.err.println("Error in data file format; returning roster 'to date'");
}
inFile.close();
return students;
}
public int classSize(int students){
int classSize;
for (classSize=0;classSize< students;classSize++)
return classSize;
}
public String printRoster(String roster){
return roster;
}
}
GradeBookMain.java:
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; ? public class GradeBookMain { public static void main(String[] args) { GradeBook gb = new GradeBook("CS007.txt"); System. out.println(gb.classSize()); System. out.println(gb.printRoster()); } }
Explanation / Answer
These are the classes
------------------------------------------------
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeBook {
private List<Student> roster;
String dir="C:/Users/timacs/Desktop/eclipse_workspace/temp/";
/**
* Construtor creates a gradebook from a file of given data
* @param fileName the name of the file holding the data
*/
//Creates gradeBook, gets data from fileName
public GradeBook(String fileName) {
roster = readFromFile(fileName);
}
/**
* Store student's data
* @param fileName
* @return students
*/
//Creates scanner inFile
public List<Student> readFromFile( String fileName ) {
List<Student> students = new ArrayList<Student>();
try {
FileInputStream fstream = new FileInputStream(dir+fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine="";
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(" ");
String name =tokens[0];
String ID = tokens[1];
int q0 = Integer.parseInt(tokens[2].trim());
int q1 = Integer.parseInt(tokens[3].trim());
int q2 = Integer.parseInt(tokens[4].trim());
int q3 = Integer.parseInt(tokens[5].trim());
int q4 = Integer.parseInt(tokens[6].trim());
int fin = Integer.parseInt(tokens[7].trim());
String gender = tokens[8].trim();
// stud.close();
boolean isMale = gender.equals("true");
Student s = new Student(name, ID, q0, q1, q2, q3, q4, fin, isMale);
students.add(s);
}
//error within file
} catch (Exception e) {
//System.err.println("Error in data file format; returning roster 'to date'");
e.printStackTrace();
}
//inFile.close();
return students;
}
public int classSize(){
return roster.size();
}
public void printRoster(){
Student s=new Student();
for(int i=0;i<roster.size();i++)
{
s=roster.get(i);
s.ToString();
System.out.println("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.