HomeworkDB

**Solutions for the Homework Database Case Study**
code format="javaScript" private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { clearData; // do this before typing a new record }
 * 1 Add a [New Record] Button**

public void clearData {   // Put empty Strings in all the data boxes

dateGivenBox.setText(""); dateDueBox.setText(""); titleBox.setText(""); gradeBox.setText(""); subjectBox.setText(""); detailsArea.setText(""); } code

code format="javaScript" private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { loadRecord; }
 * 2 Load one specific record by typing the record number**

public void loadRecord {   // Get the number from the recordNum box // Seek to that location in the file // Read all the FIELDS and //  display them in the boxes

try {       RandomAccessFile info = new RandomAccessFile("hw.dat","rw");

long rec = Long.parseLong(recordNum.getText);

info.seek(rec*1060 + 0); long dateGiven = info.readLong; String given = new Date(dateGiven).toLocaleString; int comma = given.indexOf(",")+6; given = given.substring(0,comma); dateGivenBox.setText(given);

info.seek(rec*1060 + 8); long dateDue = info.readLong; String due = new Date(dateDue).toLocaleString; comma = due.indexOf(",")+6; due = due.substring(0,comma); dateDueBox.setText(due);     //REFACTOR

info.seek(rec*1060 + 16); String subject = info.readUTF; subjectBox.setText(subject);

info.seek(rec*1060 + 36); String title = info.readUTF; titleBox.setText(title);

info.seek(rec*1060+100); String details = info.readUTF; detailsArea.setText(details);

info.close; }   catch(IOException ex) {       System.out.println(ex.toString); } } code

code format="javascript" private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { nextRecord; }
 * 3 A [Next] button that loads the next record after #2 Load (above)**

private void nextRecord {   // Get the record number out of the box // add 1 // put it back in the box // loadRecord long rec = Long.parseLong(recordNum.getText); rec = rec+1; recordNum.setText(rec+""); loadRecord; } code


 * 4 Too difficult to input a date - just use #5 and type the subject**

code format="javascript" private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { showOneSubject; }
 * 5 Display only the Math (or any other subject) assignments**

public void showOneSubject {       loadAllData;   // in case the array is not up-to-date

String subject = input("What subject?");

dataList.removeAll; for(int c=0; c < countData; c = c+1) {          if(allData[c][2].equalsIgnoreCase(subject)) {             String all = allData[c][0]+ " ::: " + allData[c][1]+" ::: " + "[" + allData[c][5] + "]" +" ::: "+allData[c][2] + " ::: "+allData[c][3]+ " ::: "+allData[c][4]; dataList.add(all); }       }

} code

code format="javascript" private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { loadTomorrow; showAllData; }
 * 6 Display a list of the assignments that are due tomorrow.**

public void loadTomorrow {       // need to read directly from the file // so we can compare tomorrow to the // long values stored in the file

Date today = new Date; System.out.println("--"); System.out.println(today.getTime);

long millis = today.getTime; long millis2 = millis + 1000*60*60*24; // tomorrow

try{ RandomAccessFile info = new RandomAccessFile("hw.dat","rw");

long size = info.length; long records = -1; if(size % 1060 == 0) { records = size / 1060; } else { records = size / 1060 + 1; }

countData=0;

for(int c=0; c millis && dateDue < millis2 ) {                info.seek(c * 1060 + 16); String subject = info.readUTF; info.seek(c * 1060 + 36); String title = info.readUTF; info.seek(c * 1060 + 100); String details = info.readUTF; info.seek(c * 1060 + 1056); int g = info.readInt;

String given = new Date(dateGiven).toLocaleString; String due = new Date(dateDue).toLocaleString; String grade = g + "";

// look for the comma int comma = given.indexOf(",")+6; given = given.substring(0,comma);

comma = due.indexOf(",")+6; due = due.substring(0,comma);

dueDates[countData] = dateDue; allData[countData][0] = given; allData[countData][1] = due; allData[countData][2] = subject; allData[countData][3] = title; allData[countData][4] = details; allData[countData][5] = grade;

countData = countData+1; }           }            info.close; }       catch(IOException ex) { System.out.println(ex.toString); }

} code


 * 7 Display all assignments that are due in the future**

This is identical to **loadTomorrow (above)**, except we need to change the **if..** command:


 * --> if (dateDue > millis )**

Easiest do do this directly in the file. code format="javascript" private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { countScience; }
 * 8 COUNT the number of Science assignments that were assigned**

public void countScience {      int count = 0; try{ RandomAccessFile info = new RandomAccessFile("hw.dat","rw");

long size = info.length; long records = -1; if(size % 1060 == 0) { records = size / 1060; } else { records = size / 1060 + 1; }

countData=0;

for(int c=0; c<records; c=c+1) {                info.seek(c * 1060 + 16); String subject = info.readUTF; if (subject.equals("Physics")                    || subject.equals("Biology")                     || subject.equals("Chemistry")                     || subject.equals("Science")                 ) {                   count = count + 1; }           }            info.close; output("There were " + count + " science assignments"); }       catch(IOException ex) { System.out.println(ex.toString); } } code

code format="javascript" public void sortSubjects {       for(int pos=0; pos < countData; pos = pos+1) {          int best = findFirst(pos);
 * 9 [HL] SORT all the assignments by subject and display all**

allData[99] = allData[pos]; allData[pos] = allData[best]; allData[best] = allData[99]; }       showAllData; }

public int findFirst(int pos) {       int bestRow = pos; String subject = allData[pos][2];

for(int r=pos+1; r < countData; r=r+1) {           String check = allData[r][2]; if(check.compareTo(subject) < 0) {               bestRow = r;                subject = check; }       }        return bestRow; }

code
 * 10 [HL] SORT the assignments by Due Date and display them all**

code format="javascript" public void sortDueDates {       for(int x=0; x<countData-1; x++) {          int smallRow = findSmallest(x); swapRows(x,smallRow); }       showAllData; }

public int findSmallest(int start) {       int row = start; long smallest = dueDates[start]; for(int c=start; c < countData; c=c+1) {          long d = dueDates[c]; if(d<smallest) {              smallest = d;               row = c;           } }       return row; } code
 * 11 [HL] SORT only the future assignments by Due Date**

One way to do this is to sort the due dates in REVERSE order, from latest down to earliest. Then let the user scroll from the top of the list until the find today.

code format="javascript" public void markComplete {   // assume a record has already been loaded // concatenate XXX to the front of the Title // but first check that it doesn't already // have XXX at the beginning
 * 12 Mark an assignment as COMPLETE by changing the Title to have xxx at the beginning.**

String title = titleBox.getText; if(!title.substring(0,3).equals("xxx")) {       title = "xxx-" + title; }   titleBox.setText(title); saveRecord; } code

code format="javascript"
 * 13 Search for all the UNFINISHED assignments that were due before today.**

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { loadOverdue; showAllData; }

public void loadOverdue {       // need to read directly from the file // so we can compare tomorrow to the // long values stored in the file

Date today = new Date;

long millis = today.getTime; System.out.println(millis); try{ RandomAccessFile info = new RandomAccessFile("hw.dat","rw");

long size = info.length; long records = -1; if(size % 1060 == 0) { records = size / 1060; } else { records = size / 1060 + 1; }

countData=0;

for(int c=0; c<records; c=c+1) {             info.seek(c * 1060); long dateGiven = info.readLong; info.seek(c * 1060 + 8); long dateDue = info.readLong; System.out.println(dateDue); info.seek(c * 1060 + 16); String subject = info.readUTF; info.seek(c * 1060 + 36); String title = info.readUTF;

if (dateDue < millis && !title.substring(0,3).equals("xxx")) {

info.seek(c * 1060 + 100); String details = info.readUTF; info.seek(c * 1060 + 1056); int g = info.readInt;

String given = new Date(dateGiven).toLocaleString; String due = new Date(dateDue).toLocaleString; String grade = g + "";

// look for the comma int comma = given.indexOf(",")+6; given = given.substring(0,comma);

comma = due.indexOf(",")+6; due = due.substring(0,comma);

dueDates[countData] = dateDue; allData[countData][0] = given; allData[countData][1] = due; allData[countData][2] = subject; allData[countData][3] = title; allData[countData][4] = details; allData[countData][5] = grade;

countData = countData+1; }           }            info.close; }       catch(IOException ex) { System.out.println(ex.toString); }

}

code

Then add a box on the Assignment form where the grade can be typed and saved.
 * 14 Add a GRADE Field by using the last 4 Bytes of each RECORD to store an 32-bit int value.**

code format="javascript" private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) { // This should only average grades for a single subject. averageOneSubject; }
 * 15 Average the grades for one subject.**

public void averageOneSubject {      String find = input("Subject to find");

int count = 0; int total = 0; try{ RandomAccessFile info = new RandomAccessFile("hw.dat","rw");

long size = info.length; long records = -1; if(size % 1060 == 0) { records = size / 1060; } else { records = size / 1060 + 1; }

countData=0;

for(int c=0; c 0) { output("Average grade for " + find + " = " + ( (total+0.0)/count) );} else { output("No assignments found for " + find); } // need to add 0.0 to force decimal division } code