// Purpose: The program will gather
the students' grading statistics
//
including min, max, avg, standard deviation, etc.
import java.io.*;
//***************************************************************
// Defination of the class GradeReport(subclass
of DataProcess) *
//***************************************************************
public class GradeReport extends DataProcess
{
// Main method
public static void main(String[]
args) {
// Declaration
of the instance variables
String
aFileName = null;
PrintWriter
out;
GradeReport
aGradeReport = new GradeReport();
// Get
message from method fillArray()
aGradeReport.fillArray();
// fill input from binary file
// into aStudentArr[]
// get
file name from user
System.out.print("Please
enter the name of ");
System.out.println("the
text file:");
aFileName
= Console.readLine();
System.out.println("The
filename is: " + aFileName);
try
{
// Open text file
out = new PrintWriter(new FileOutputStream(aFileName));
// Write report form
aGradeReport.reportForm(out);
out.println();
// Write the Max, Min, avg and sDeviation
aGradeReport.displayResults(out);
// Show the count of the A's, B's, C's, D's, E's, F's
aGradeReport.showCount(out);
// Close file
out.close();
} //
end of try
catch(IOException
e) {
System.err.println("File not opened properly.");
System.err.println(e.toString());
System.exit(1);
}
} // end of main method
// Definition of the method
displayResults
private void displayResults(PrintWriter
out) {
out.println("The
highest score is " + getMax() + ".");
out.println("The
lowest score is " + getMin() + ".");
out.println("The
average score is " + getAverage() + ".");
out.print("The
standard deviation is ");
out.println(getSDeviation()
+ ".");
out.println();
}
// Definition of the method
drawStars
private void drawStars(int
n, PrintWriter out) {
for
(int i = 0; i < n; i++) {
out.print("*");
}
out.println();
}
// Definition of the method
reportForm()
public void reportForm(PrintWriter
out) {
//
Write heading
out.print("**********************************");
out.println("************************");
out.print("*
GRADE REPORT");
out.println("
*");
out.print("**********************************");
out.println("************************");
// Print
the report form before sort
out.println();
out.println("***********
Before Sorting **********");
out.println("
Name" + "\t\t\t" + "Score" + "\t" + "Grade");
reportFormHelp(out);
out.println("*************************************");
// Print
the report form after sort
sortStudent(0,
SIZE-1); // sort aStudentArr[] by name
out.println();
out.println("***********
After Sorting ***********");
out.println("
Name" + "\t\t\t" + "Score" + "\t" + "Grade");
reportFormHelp(out);
out.println("*************************************");
}
// Write the form body
to text file
private void reportFormHelp(PrintWriter
out) {
for
(int i = 0; i < aStudentArr.length; i++) {
out.print((i+1) + "." + aStudentArr[i].getFullName());
out.print("\t\t" + aStudentArr[i].getScore());
out.println("\t" + aStudentArr[i].getGrade());
}
}
// Definition of the method
showCount
private void showCount(PrintWriter
out) {
fillCountArr();
out.print("There
are " + getAGradeCount('A') + " A's: ");
drawStars(getAGradeCount('A'),
out);
out.print("There
are " + getAGradeCount('B') + " B's: ");
drawStars(getAGradeCount('B'),
out);
out.print("There
are " + getAGradeCount('C') + " C's: ");
drawStars(getAGradeCount('C'),
out);
out.print("There
are " + getAGradeCount('D') + " D's: ");
drawStars(getAGradeCount('D'),
out);
out.print("There
are " + getAGradeCount('E') + " E's: ");
drawStars(getAGradeCount('E'),
out);
out.print("There
are " + getAGradeCount('F') + " F's: ");
drawStars(getAGradeCount('F'),
out);
}
}
// ==================== File Creation.java
================
// FileCreation.java
// This program creates a binary file
and writes the data
// consisting of string constants(student
names) and int
// constants(scores) supplied by the
user from keyboard
// to that binary file.
import java.io.*;
import java.util.*;
// Definition of the class FileCreation
public class FileCreation {
// Declaration of the
instance variables
String firstName;
String lastName;
String line;
String aFileName;
int score;
int count;
BufferedReader keyboard;
DataOutputStream out;
StringTokenizer tokenizer;
// Default constructor
public FileCreation()
{
aFileName
= "";
firstName
= "";
lastName
= "";
line
= "";
score
= 0;
count
= 0;
}
// Definition of the createBinaryFile
method
public int createBinaryFile()
{
//get
file name from user
System.out.println("Please
enter the file name:");
aFileName
= Console.readLine();
System.out.println("The
filename is: " + aFileName);
try
{
// Open file
out = new DataOutputStream(new FileOutputStream(aFileName));
do {
process();
count++;
System.out.println("Want to continue (yes|no)?");
line = Console.readLine();
} while(!line.equals("no"));
// Close file
out.close();
}
catch(IOException
e) {
System.err.println("File not opened properly.");
System.err.println(e.toString());
System.exit(1);
}
return count;
} // End of createBinaryFile
method
// Definition of accessor
method
public int getCount()
{
return
count;
}
// Definition of accessor
method
public String getFileName()
{
return
aFileName;
}
// Read a line from keyboard
and write to the binary file
private void process()
{
try
{
System.out.print("Please enter last name, first name,");
System.out.println(" and score with a space in between:");
line = Console.readLine();
tokenizer = new StringTokenizer(line);
lastName = tokenizer.nextToken();
System.out.println("lastName: " + lastName);
firstName = tokenizer.nextToken();
System.out.println("firstName: " + firstName);
score = Integer.parseInt(tokenizer.nextToken());
System.out.println("score: " + score);
out.writeUTF(lastName);
out.writeUTF(firstName);
out.writeInt(score);
}
catch(IOException
e) {
System.out.println(e.getMessage());
System.out.println("Writing failed");
}
}
}
// ======================= FilledArray.java
=================
// FilledArray.java
import java.io.*;
//**********************************************************
// Definition of the class FilledArray
*
//**********************************************************
public class FilledArray {
// Instance variable
declaration
protected int i = 0;
protected int aScore
= 0;
protected String firstname
= null;
protected String lastname
= null;
DataInputStream in;
FileCreation aFileCreation
= new FileCreation();
int SIZE = aFileCreation.createBinaryFile();//get
the count of students.
Student aStudentArr[]
= new Student[SIZE];
// Fill array
public void fillArray()
{
String
aFileName = aFileCreation.getFileName();
try
{
in = new DataInputStream(new FileInputStream(aFileName));
try {
while(true) {
// Get data from binary file
lastname = in.readUTF().trim();
firstname = in.readUTF().trim();
aScore = in.readInt();
// Put data into aStudentArr[] object
aStudentArr[i] = new Student();
aStudentArr[i].setLastName(lastname);
aStudentArr[i].setFirstName(firstname);
aStudentArr[i].setScore(aScore);
i++; // increase the array index.
} // end of while
} // end of inner try.
catch(EOFException e) {
System.out.println("File reading complete.");
} // end of catch 1.
catch(IOException e) {
System.out.println(e.getMessage());
System.out.println("Reading failed");
} // end of catch 2.
// Close file
in.close();
} //
end of outer try.
catch(FileNotFoundException
e) {
System.out.println("File is not found.");
}
// end of outer catch 1.
catch(IOException
e) {
System.err.println("Error closing file.");
System.err.println(e.toString());
System.exit(1);
} //
end of outer catch 2.
} // end method fillArray()
// Definition of the method
sortStudent()
public void sortStudent(int
first, int last) {
//int
first = 0, last = SIZE - 1;
int
i = first, j = last;
String
fence = null;
Student
temp;
fence
= aStudentArr[(first + last) / 2].getFullName();
do
{
while (aStudentArr[i].getFullName().compareTo(fence) < 0)
i++;
while (aStudentArr[j].getFullName().compareTo(fence) > 0)
j--;
if (i <= j) {
temp = aStudentArr[i];
aStudentArr[i] = aStudentArr[j];
aStudentArr[j] = temp;
i++;
j--;
}
} while
(i <= j);
if
(first < j)
sortStudent(first, j);
if
(i < last)
sortStudent(i, last);
}
}
// ====================== DataProcess.java
====================
// DataProcess.java
import java.io.*;
import java.text.*;
//***********************************************************
// Definition of class DataProcess(subclass
of FilledArray)
*
//***********************************************************
public class DataProcess extends FilledArray
{
// Instance variable
delaration
protected int countArr[]
= {0, 0, 0, 0, 0, 0};;
protected double avg
= 0.0;
DecimalFormat precision2
= new DecimalFormat("#0.00");
// Definition of the method
getMin()
public int getMin() {
int
min = aStudentArr[0].getScore();
for(int
i = 0; i < aStudentArr.length; i++) {
if (aStudentArr[i].getScore() < min)
min = aStudentArr[i].getScore();
}
return
min;
}
// Definition of the method
getMax()
public int getMax() {
int
max = aStudentArr[0].getScore();
for(int
i = 0; i < aStudentArr.length; i++) {
if(aStudentArr[i].getScore() > max)
max = aStudentArr[i].getScore();
}
return
max;
}
// Definition of the method
getAverage()
public double getAverage()
{
String
average = null;
double
sum = 0.0;
for(int
i = 0; i < aStudentArr.length; i++) {
sum = sum + aStudentArr[i].getScore();
}
average
= precision2.format(sum / aStudentArr.length);
avg
= Double.valueOf(average).doubleValue();
return
avg ;
}
// Definition of the method
getSDeviation()
public double getSDeviation()
{
String
sDeviation = null;
double
sum = 0.0;
for(int
i = 0; i < aStudentArr.length; i++){
sum = sum + (aStudentArr[i].getScore() - avg)
* (aStudentArr[i].getScore() - avg);
}
sDeviation
= precision2.format
(Math.sqrt(sum / aStudentArr.length));
return(Double.valueOf(sDeviation).doubleValue());
}
// Definition of the method
fillCountArr()
public void fillCountArr()
{
for(int
i = 0; i < aStudentArr.length; i++) {
switch ( aStudentArr[i].getGrade() ) {
case 'A':
countArr[0]++; break;
case 'B':
countArr[1]++; break;
case 'C':
countArr[2]++; break;
case 'D':
countArr[3]++; break;
case 'E':
countArr[4]++; break;
case 'F':
countArr[5]++; break;
default:
System.out.println("Invalid grade"); break;
} // end of switch.
} //
end of for loop.
} // end of fillCountArr()
method.
// Definition of the method
getAGradeCount
public int getAGradeCount(char
grade) {
int
count = 0;
switch
( grade ) {
case 'A':
count = countArr[0]; break;
case 'B':
count = countArr[1]; break;
case 'C':
count = countArr[2]; break;
case 'D':
count = countArr[3]; break;
case 'E':
count = countArr[4]; break;
case 'F':
count = countArr[5]; break;
default:
System.out.println("Invalid grade"); break;
}
// end of switch.
return
count;
}
}
// ==================== Student.java
===================
// Student.java
// Student class
import java.io.*;
//***************************************************************
// Definition of the Student class
*
//***************************************************************
public class Student {
// Declaration of the
instance variables
protected String lastName
= null;
protected String firstName
= null;
protected int score =
0;
// Definition of accessor
method getLastName()
public String getLastName()
{
return
lastName;
}
// Definition of accessor
method getFirstName()
public String getFirstName()
{
return
firstName;
}
// Definition of accessor
method getFullName()
public String getFullName()
{
return
lastName + ", " + firstName;
}
// Definition of accessor
method getGrade()
public char getGrade()
{
char
aGrade = '\0';
if (score
>= 90) {
aGrade = 'A';
} else
if ((score >= 80) && (score < 90)) {
aGrade = 'B';
} else
if ((score >= 70) && (score < 80)) {
aGrade = 'C';
} else
if ((score >= 60) && (score < 70)) {
aGrade = 'D';
} else
if ((score >= 50) && (score < 60)) {
aGrade = 'E';
} else
if (score < 50) {
aGrade = 'F';
}
return
aGrade;
}
// Definition of accessor
method getScore()
public int getScore()
{
return
score;
}
// Definition of the mutator
method
public void setFirstName(String
first) {
firstName
= first;
}
// Definition of the mutator
method
public void setLastName(String
last) {
lastName
= last;
}
// Definition of the mutator
method
public void setScore(int
aScore) {
score
= aScore;
}
}