import java.util.*;
/** This class tests the symbol table implementation of
Assignment 2, CS 146, Fall 2002.
*/
public class A2
{
// for generating random input data
Random r = new Random(1);
// holds the map between percentage grades and letter grades
String[] gradeArray = new String[100];
/**
@return a randomly chosen id number from 1000 through 9999
*/
private void init() {
int i;
for (i=0; i<60; i++)
gradeArray[i]="F";
for (i=60; i<62; i++)
gradeArray[i]="D-";
for (i=62; i<68; i++)
gradeArray[i]="D";
for (i=68; i<70; i++)
gradeArray[i]="D+";
for (i=70; i<72; i++)
gradeArray[i]="C-";
for (i=72; i<78; i++)
gradeArray[i]="C";
for (i=78; i<80; i++)
gradeArray[i]="C+";
for (i=80; i<82; i++)
gradeArray[i]="B-";
for (i=82; i<88; i++)
gradeArray[i]="B";
for (i=88; i<90; i++)
gradeArray[i]="B+";
for (i=90; i<92; i++)
gradeArray[i]="A-";
for (i=92; i<100; i++)
gradeArray[i]="A"; }
/**
@return a random 4-digit id number that doesn't begin with 0
*/
private int getRandomId() {
return 1000 + r.nextInt(9000); }
/**
@return a randomly generated letter grade, selected from a normal
distribution with median equal to the lowest C+ grade and
standard deviation equal to 80% of a letter grade
*/
// Note that grades higher than 99 or lower than 0 are rounded to
// these values. These grades should be rare, especially the
// latter ones.
private String getRandomGrade() {
int numberGrade = (int) Math.round(78.0+8.0*r.nextGaussian());
if (numberGrade >= 100)
numberGrade = 99;
if (numberGrade <= 0)
numberGrade = 0;
return gradeArray[numberGrade]; }
/**
Creates a student record for a given student id number and
name. Prints an acknowledgement if successful and an error
message if not
@param i the id number
@param n the name
@return the student record if the id number is legal, and null
otherwise
*/
// The method simply calls the StudentRecord constructor and determines
// whether the constructor has thrown an exception (due to a bad id number)
private StudentRecord tryCreatingStudentRecord(int i, String n) {
try {
StudentRecord sr= new StudentRecord(i,n);
System.out.println(
"student record "+sr.getIdNumber()+" created ");
return sr; }
catch (NumberFormatException e) {
System.out.print("student record not created -- ");
System.out.print(" bad id number ");
System.out.println(i);
return null; } }
private StudentRecord createSemirandomStudentRecord(char c) {
StudentRecord sr = new StudentRecord(getRandomId(),c+"an");
sr.addCourse("CS",200,992,5,getRandomGrade());
sr.addCourse("CS",201,994,5,getRandomGrade());
return sr; }
/**
Adds a student record to a student file, if the record and
file are not empty, and there is no other record with the
same student id number in the file. Prints an acknowledgement
in case of success and an error message in case of error
@param sr the student record
@param sf the student file
*/
// The addStudentRecord method will check for the first and
// last type of error and return false if it arises.
// So this method need only check for that value, and for the case
// of an empty student file.
// In real life, error message might be sent to System.err, but it is
// easier to grade if not.
private void tryAddingStudentRecord(StudentRecord sr,
StudentFile sf) {
if (sf==null)
System.out.println(
"student file does not exist -- record not added");
else {
boolean added = sf.addStudentRecord(sr);
if (added)
System.out.println(
"student record "+sr.getIdNumber()+" added ");
else {
System.out.print("duplicate or empty student record ");
System.out.println(" not added"); } } }
/**
Adds a student record to a student file, if the record and
file are not empty, and there is no other record with the
same student id number in the file. Prints an acknowledgement
in case of success and an error message in case of error
@param sr the student record
@param sf the student file
*/
// The addStudentRecord method will check for the first and
// last type of error and return false if it arises.
// So this method need only check for that value, and for the case
// of an empty student file.
// In real life, error message might be sent to System.err, but it is
// easier to grade if not.
private String tryGettingStudentName(int key,
StudentFile sf) {
if (sf==null)
return "";
else {
StudentRecord record = sf.getStudentRecord(key);
if (record == null)
return "";
else
return record.getName(); } }
/**
prints the contents of a collection
@param c the collection
*/
// From Weiss, p. 515
static void printCollection(Collection c) {
Iterator itr = c.iterator();
while (itr.hasNext())
System.out.println(itr.next()); }
/**
the test cases
*/
public void test() {
StudentFile sf=null;
StudentRecord sr = tryCreatingStudentRecord(12345, "Jones, Kim");
sr.traverse();
StudentRecord srx = tryCreatingStudentRecord(54321, null);
srx.traverse();
if (sr!=null)
tryAddingStudentRecord(sr,sf);
sf= new StudentFile();
sf.traverse();
tryAddingStudentRecord(null,sf);
if (sr!=null) {
tryAddingStudentRecord(sr,sf);
sr.addCourse("CS", 101, 982, 3, "A");
sr.addCourse("CS", 102, 984, 3, "A");
sr.addCourse("CS", 103, 992, 3, "C-");
sr.addCourse("CS", 103, 992, 3, "C-");
sr.addCourse("CS", -99, 994, 3, "F");
sr.addCourse("CS", 100, 994, -6, "C");
sr.addCourse("CS", 100, -14, 3, "C");
sr.addCourse("CS", 103, 994, 3, "B+"); }
StudentRecord sr0 = tryCreatingStudentRecord(12345, "Repeat, Pete");
if (sr0!=null)
tryAddingStudentRecord(sr0,sf);
System.out.println();
StudentRecord sr1 = tryCreatingStudentRecord(-999, "Low, Too");
if (sr1!=null)
tryAddingStudentRecord(sr1,sf);
System.out.println();
sr.traverse();
System.out.println(sr.computeGPA());
System.out.println();
StudentRecord sr2 =
tryCreatingStudentRecord(getRandomId(),"Random, Randy");
if (sr2!=null) {
tryAddingStudentRecord(sr2,sf);
for (int j=1; j<=10; j++)
sr2.addCourse("Math", 100+j, 902+10*j, 3 + j/5, getRandomGrade());
sr2.traverse();
System.out.println(sr2.computeGPA()); }
System.out.println();
StudentRecord sr3 = tryCreatingStudentRecord(43210, "Jones, Kim");
if (sr3!=null) {
tryAddingStudentRecord(sr3,sf);
sr3.addCourse("CS", 201, 912, 3, "A");
sr3.addCourse("CS", 202, 914, 3, "A-");
sr3.addCourse("CS", 203, 922, 3, "B+");
sr3.addCourse("CS", 204, 924, 3, "B");
sr3.addCourse("CS", 205, 932, 3, "B-");
sr3.addCourse("CS", 206, 934, 3, "C+");
sr3.addCourse("CS", 207, 942, 3, "C");
sr3.addCourse("CS", 208, 944, 3, "C-");
sr3.addCourse("CS", 209, 952, 3, "D+");
sr3.addCourse("CS", 210, 954, 3, "D");
sr3.addCourse("CS", 211, 962, 3, "D-");
sr3.addCourse("CS", 212, 964, 3, "F");
sr3.traverse();
System.out.println(sr3.computeGPA());
System.out.println(); }
StudentRecord sr4 = tryCreatingStudentRecord(00000, "Zero, Zoe");
if (sr4!=null) {
tryAddingStudentRecord(sr4,sf);
sr4.addCourse(null, 101, 982, 3, null);
sr4.addCourse("Zero", 300, 001, 5, "U");
sr4.addCourse("Zero", 300, 002, 5, "AA");
sr4.addCourse("Zero", 300, 003, 5, "C++");
sr4.addCourse("Zero", 300, 004, 5, "+");
sr4.traverse();
System.out.println(sr4.computeGPA());
System.out.println(); }
System.out.println();
System.out.println(sr.findGrade("CS", 102));
System.out.println(sr2.findGrade("CS", 102));
System.out.println(sr.findGrade("CS", 234));
System.out.println(sr2.findGrade("Math", 109));
System.out.println(sr.findGrade("CS", 103));
System.out.println();
sf.traverse();
System.out.println();
sf.traverseLevels();
for (char c='A'; c<='Z'; c++) {
tryAddingStudentRecord(createSemirandomStudentRecord(c),sf); }
System.out.println(sf.containsStudent(1234));
System.out.println(sf.containsStudent(1310));
System.out.println(sf.containsStudent(9099));
System.out.println(sf.containsStudent(12345));
System.out.println(tryGettingStudentName(1234,sf));
System.out.println(tryGettingStudentName(1310,sf));
System.out.println(tryGettingStudentName(9099,sf));
System.out.println(tryGettingStudentName(12345,sf));
System.out.println();
printCollection(sf.sortByName());
System.out.println();
System.out.println();
printCollection(sf.sortByGPA());
System.out.println();
sf.traverse();
System.out.println();
sf.traverseLevels(); }
/**
Creates and tests an A2 object
@param args is ignored
*/
public static void main(String[] args) {
A2 a2 = new A2(); // default constructor is ok here
a2.init();
a2.test(); }
};