/** * GPACalc * by Jay Tomlin, 1/19/97 * http://ezinfo.ucs.indiana.edu/~ltomlin/GPA.html * A configurable GPA calculator applet (^_^) */ import java.applet.*; import java.awt.*; public class GPACalc extends Applet implements Runnable { // // Declare the class variables // Thread calculationThread; // a background thread for updating the GPA \\ Panel Priors, Calc, Controls, Master, ConfigPanel, Config, ConfigButtons; GridLayout grid, configGrid; GridBagLayout gridbag; CardLayout card; // For the Calc panel: \\ TextField Hours1, Hours2, Hours3, Hours4, Hours5, Hours6, Hours7; TextField Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7; TextField NumGrade1, NumGrade2, NumGrade3, NumGrade4, NumGrade5, NumGrade6, NumGrade7; // For the Priors panel: \\ TextField PriorGPA, PriorHours; // For the Controls panel: \\ TextField SemesterHours, SemesterGPA, CumulativeHours, CumulativeGPA; Button Clear, anotherSemester, Configure; // For the Config panel: \\ Panel instructionsPanel; GridLayout instGrid; TextArea instructions; TextField APlusValue, AValue, AMinusValue, BPlusValue, BValue, BMinusValue, CPlusValue, CValue, CMinusValue, DPlusValue, DValue, DMinusValue, FValue; Button Return, RestoreDefaults; double aplus, a, aminus, bplus, b, bminus, cplus, c, cminus, dplus, d, dminus, f; double points = 0; public void init() { // get the GridBag started gridbag = new GridBagLayout(); // // Create the component Panels and add their widgets: // // Priors, a panel where the user enters prior GPA and credit hours: \\ Priors = new Panel(); Priors.setLayout(gridbag); PriorHours = new TextField(3); PriorGPA = new TextField(4); addComponent(Priors, new Label("Prior Credit Hours:"), 1, 0, 1, 1, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE); addComponent(Priors, PriorHours, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE); addComponent(Priors, new Label("Prior GPA:"), 1, 1, 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE); addComponent(Priors, PriorGPA, 2, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE); PriorHours.setText("0"); PriorGPA.setText("0.00"); // Calc, a Panel where the user enters current-semester grades \\ Calc = new Panel(); grid = new GridLayout(9, 3, 5, 5); // a 9x3 grid for entering course & grade info \\ Calc.setLayout(grid); Hours1 = new TextField(1); Hours2 = new TextField(1); Hours3 = new TextField(1); Hours4 = new TextField(1); Hours5 = new TextField(1); Hours6 = new TextField(1); Hours7 = new TextField(1); Grade1 = new TextField(2); Grade2 = new TextField(2); Grade3 = new TextField(2); Grade4 = new TextField(2); Grade5 = new TextField(2); Grade6 = new TextField(2); Grade7 = new TextField(2); NumGrade1 = new TextField(4); NumGrade1.setEditable(false); NumGrade2 = new TextField(4); NumGrade2.setEditable(false); NumGrade3 = new TextField(4); NumGrade3.setEditable(false); NumGrade4 = new TextField(4); NumGrade4.setEditable(false); NumGrade5 = new TextField(4); NumGrade5.setEditable(false); NumGrade6 = new TextField(4); NumGrade6.setEditable(false); NumGrade7 = new TextField(4); NumGrade7.setEditable(false); Calc.add(new Label("Hours")); Calc.add(new Label("Grade")); Calc.add(new Label("Points")); Calc.add(Hours1); Calc.add(Grade1); Calc.add(NumGrade1); Calc.add(Hours2); Calc.add(Grade2); Calc.add(NumGrade2); Calc.add(Hours3); Calc.add(Grade3); Calc.add(NumGrade3); Calc.add(Hours4); Calc.add(Grade4); Calc.add(NumGrade4); Calc.add(Hours5); Calc.add(Grade5); Calc.add(NumGrade5); Calc.add(Hours6); Calc.add(Grade6); Calc.add(NumGrade6); Calc.add(Hours7); Calc.add(Grade7); Calc.add(NumGrade7); // Controls, a panel for the bottom row of buttons and fields \\ Controls = new Panel(); Controls.setLayout(gridbag); SemesterHours = new TextField(3); SemesterGPA = new TextField(8); CumulativeHours = new TextField(3); CumulativeGPA = new TextField(8); Clear = new Button("Clear"); anotherSemester = new Button("Do Another Semester"); Configure = new Button("Configure..."); addComponent(Controls, new Label("Total Hours"), 1, 0, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.NONE); addComponent(Controls, new Label("GPA"), 2, 0, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.NONE); addComponent(Controls, new Label("This Semester: "), 0, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE); addComponent(Controls, new Label("Cumulative: "), 0, 2, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE); addComponent(Controls, SemesterHours, 1, 1, 1, 1); addComponent(Controls, SemesterGPA, 2, 1, 1, 1); addComponent(Controls, CumulativeHours, 1, 2, 1, 1); addComponent(Controls, CumulativeGPA, 2, 2, 1, 1); addComponent(Controls, SemesterHours, 1, 1, 1, 1); addComponent(Controls, Clear, 2, 3, 1, 1); addComponent(Controls, anotherSemester, 0, 3, 1, 1); addComponent(Controls, Configure, 1, 3, 1, 1); // Master, a Panel (Card) to hold everything so far Master = new Panel(); Master.setLayout(gridbag); addComponent(Master, Priors, 0, 0, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.NONE); addComponent(Master, Calc, 0, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.NONE); addComponent(Master, Controls, 0, 2, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.NONE); // ConfigPanel, a Panel (Card) to hold the instructions and configurations ConfigPanel = new Panel(); instructions = new TextArea(10, 35); instructions.setEditable(false); instructions.setText("GPACalc\nby Jay Tomlin\nhttp://ezinfo.ucs.indiana.edu/~ltomlin\n\n"); instructions.appendText("Enter the number equivalents for each \nletter grade listed below. "); instructions.appendText("To restore\nthe original equivalents, click the\n\"Restore Defaults\" button.\n"); instructionsPanel = new Panel(); instGrid = new GridLayout(1,1); // necessary so the text area resizes instructionsPanel.setLayout(instGrid); instructionsPanel.add(instructions); ConfigButtons = new Panel(); // we'll just let the default FlowLayout take this one Return = new Button("Return"); RestoreDefaults = new Button("Restore Defaults"); ConfigButtons.add(RestoreDefaults); ConfigButtons.add(Return); // Config, a panel where the user sets the point value for each letter grade \\ Config = new Panel(); configGrid = new GridLayout(7, 4); // a 7x4 grid for configuring grade values \\ Config.setLayout(configGrid); APlusValue = new TextField(3); AValue = new TextField(3); AMinusValue = new TextField(3); BPlusValue = new TextField(3); BValue = new TextField(3); BMinusValue = new TextField(3); CPlusValue = new TextField(3); CValue = new TextField(3); CMinusValue = new TextField(3); DPlusValue = new TextField(3); DValue = new TextField(3); DMinusValue = new TextField(3); FValue = new TextField(3); RestoreDefaults(); // defined below Config.add(new Label("A+:")); Config.add(APlusValue); // four components \\ Config.add(new Label(" C: ")); Config.add(CValue); // per row \\ Config.add(new Label("A:")); Config.add(AValue); Config.add(new Label(" C-:")); Config.add(CMinusValue); Config.add(new Label("A-:")); Config.add(AMinusValue); Config.add(new Label(" D+:")); Config.add(DPlusValue); Config.add(new Label("B+:")); Config.add(BPlusValue); Config.add(new Label(" D:")); Config.add(DValue); Config.add(new Label("B:")); Config.add(BValue); Config.add(new Label(" D-:")); Config.add(DMinusValue); Config.add(new Label("B-:")); Config.add(BMinusValue); Config.add(new Label(" F: ")); Config.add(FValue); Config.add(new Label("C+:")); Config.add(CPlusValue); // assemble the elements of the ConfigPanel in a GridBagLayout: \\ ConfigPanel.setLayout(gridbag); addComponent(ConfigPanel, instructionsPanel, 0, 0, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH); addComponent(ConfigPanel, Config, 0, 1, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.NONE); addComponent(ConfigPanel, ConfigButtons, 0, 2, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.NONE); // // Finally, add all Panels to the Applet and show them // card = new CardLayout(); this.setLayout(card); this.add("First", Master); this.add("Second", ConfigPanel); card.show(this, "First"); this.show(); } // ends init() \\ public void start() { if (calculationThread == null) { calculationThread = new Thread(this); calculationThread.start(); // starts the run() method } } // ends start() \\ public void run() { while (calculationThread != null) { try {Thread.sleep(3000);} // sleep for 3 seconds at a time to reduce flicker catch (InterruptedException e) {} // Get the number equivalences for each letter grade \\ // from the fields on the Config panel: \\ try { Double aplus_val = new Double(APlusValue.getText()); aplus = aplus_val.doubleValue(); } catch (NumberFormatException e) { aplus = 0;} try { Double a_val = new Double(AValue.getText()); a = a_val.doubleValue(); } catch (NumberFormatException e) { a = 0;} try { Double aminus_val = new Double(AMinusValue.getText()); aminus = aminus_val.doubleValue(); } catch (NumberFormatException e) { aminus = 0;} try { Double bplus_val = new Double(BPlusValue.getText()); bplus = bplus_val.doubleValue(); } catch (NumberFormatException e) { bplus = 0;} try { Double b_val = new Double(BValue.getText()); b = b_val.doubleValue(); } catch (NumberFormatException e) { b = 0;} try { Double bminus_val = new Double(BMinusValue.getText()); bminus = bminus_val.doubleValue(); } catch (NumberFormatException e) { bminus = 0;} try { Double cplus_val = new Double(CPlusValue.getText()); cplus = cplus_val.doubleValue(); } catch (NumberFormatException e) { cplus = 0;} try { Double c_val = new Double(CValue.getText()); c = c_val.doubleValue(); } catch (NumberFormatException e) { c = 0;} try { Double cminus_val = new Double(CMinusValue.getText()); cminus = cminus_val.doubleValue(); } catch (NumberFormatException e) { cminus = 0;} try { Double dplus_val = new Double(DPlusValue.getText()); dplus = dplus_val.doubleValue(); } catch (NumberFormatException e) { dplus = 0;} try { Double d_val = new Double(DValue.getText()); d = d_val.doubleValue(); } catch (NumberFormatException e) { d = 0;} try { Double dminus_val = new Double(DMinusValue.getText()); dminus = dminus_val.doubleValue(); } catch (NumberFormatException e) { dminus = 0;} try { Double f_val = new Double(FValue.getText()); f = f_val.doubleValue(); } catch (NumberFormatException e) { f = 0;} // Now we have primitive doubles (not Doubles) in // aplus, a, aminus, etc... // multiply number values times Hours fields and put // the result in NumGrade fields: TextField[] HoursArray = {Hours1, Hours2, Hours3, Hours4, Hours5, Hours6, Hours7}; TextField[] NumGradeArray = {NumGrade1, NumGrade2, NumGrade3, NumGrade4, NumGrade5, NumGrade6, NumGrade7}; TextField[] GradeArray = {Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7}; for (int i=0; i < 7; i++) { Double h = new Double(0); try {h = new Double(HoursArray[i].getText());} catch (NumberFormatException e) {h = new Double(0);} int hours = h.intValue(); String grade = GradeArray[i].getText(); if (grade.equals("A+")) { points = aplus * hours;} else if(grade.equals("A")) { points = a * hours;} else if(grade.equals("A-")) { points = aminus * hours;} else if(grade.equals("B+")) { points = bplus * hours;} else if(grade.equals("B")) { points = b * hours;} else if(grade.equals("B-")) { points = bminus * hours;} else if(grade.equals("C+")) { points = cplus * hours;} else if(grade.equals("C")) { points = c * hours;} else if(grade.equals("C-")) { points = cminus * hours;} else if(grade.equals("D+")) { points = dplus * hours;} else if(grade.equals("D")) { points = d * hours;} else if(grade.equals("D-")) { points = dminus * hours;} else if(grade.equals("F")) { points = f * hours;} else {points = 0;} Double Points = new Double(points); NumGradeArray[i].setText(Points.toString()); } //ends for loop double sum_hours = 0; double sum_points = 0; Double sem_hrs = new Double(0); Double hrs = new Double(0); Double pnts = new Double(0); for(int i=0; i < 7; i++) { try { hrs = new Double(HoursArray[i].getText());} catch (NumberFormatException e) {hrs = new Double(0);} sum_hours += hrs.doubleValue(); try { pnts = new Double(NumGradeArray[i].getText()); } catch (NumberFormatException e) { pnts = new Double(0); } sum_points += pnts.doubleValue(); } // ends for loop Double Total_Semester_Hours = new Double(sum_hours); SemesterHours.setText(Total_Semester_Hours.toString()); double sem_gpa = 0; if (sum_hours != 0) {sem_gpa = sum_points / sum_hours;} else {sem_gpa = 0.0;} Double Sem_GPA = new Double(sem_gpa); SemesterGPA.setText(Sem_GPA.toString()); // now calculate cumulatives double prior_hours, prior_gpa, prior_points, cum_hours, cum_gpa, cum_points; Double Prior_Hours, Prior_GPA, Cum_Hours, Cum_GPA; try { Prior_Hours = new Double(PriorHours.getText()); } catch (NumberFormatException e) {Prior_Hours = new Double(0);} prior_hours = Prior_Hours.doubleValue(); try { Prior_GPA = new Double(PriorGPA.getText()); } catch (NumberFormatException e) {Prior_GPA = new Double(0);} prior_gpa = Prior_GPA.doubleValue(); prior_points = prior_hours * prior_gpa; cum_hours = prior_hours + sum_hours; cum_points = prior_points + sum_points; if (cum_hours != 0) {cum_gpa = cum_points / cum_hours;} else {cum_gpa = 0.0;} Cum_Hours = new Double(cum_hours); Cum_GPA = new Double(cum_gpa); CumulativeHours.setText(Cum_Hours.toString()); CumulativeGPA.setText(Cum_GPA.toString()); } // ends while thread == null } // ends run() \\ void addComponent(Container cont, Component comp, int grid_x, int grid_y, int grid_w, int grid_h) { GridBagConstraints c = new GridBagConstraints(); c.gridx=grid_x; c.gridy=grid_y; c.gridwidth=grid_w; c.gridheight=grid_h; gridbag.setConstraints(comp, c); cont.add(comp); } //ends addComponent(cont, comp, x, y, w, h) void addComponent(Container cont, Component comp, int grid_x, int grid_y, int grid_w, int grid_h, int anchor, int fill) { GridBagConstraints c = new GridBagConstraints(); c.gridx=grid_x; c.gridy=grid_y; c.gridwidth=grid_w; c.gridheight=grid_h; c.anchor = anchor; c.fill = fill; gridbag.setConstraints(comp, c); cont.add(comp); } //ends addComponent(cont, comp, x, y, w, h, anchor, fill) // // Handle user clicks. // public boolean handleEvent(Event e) { switch(e.id) { case Event.ACTION_EVENT: if(e.target == Configure) { card.show(this, "Second"); return true;} else if(e.target == Return) {card.show(this, "First"); return true;} else if(e.target == RestoreDefaults) {RestoreDefaults(); return true;} else if(e.target == Clear) {ClearFields(); return true;} else if(e.target == anotherSemester) {DoAnotherSemester(); return true;} else {return false;} } //ends switch return false; // this way the super.handleEvent(e) is called } // ends handleEvent // // And now the function calls: // void RestoreDefaults() { APlusValue.setText("4.0"); AValue.setText("4.0"); AMinusValue.setText("3.7"); BPlusValue.setText("3.3"); BValue.setText("3.0"); BMinusValue.setText("2.7"); CPlusValue.setText("2.3"); CValue.setText("2.0"); CMinusValue.setText("1.7"); DPlusValue.setText("1.3"); DValue.setText("1.0"); DMinusValue.setText("0.7"); FValue.setText("0.0"); } // ends restoreDefaults() void ClearFields() { Hours1.setText(""); Grade1.setText(""); Hours2.setText(""); Grade2.setText(""); Hours3.setText(""); Grade3.setText(""); Hours4.setText(""); Grade4.setText(""); Hours5.setText(""); Grade5.setText(""); Hours6.setText(""); Grade6.setText(""); Hours7.setText(""); Grade7.setText(""); } // ends ClearFields() void DoAnotherSemester() { PriorGPA.setText(CumulativeGPA.getText()); PriorHours.setText(CumulativeHours.getText()); ClearFields(); } // ends DoAnotherSemester() } // ends class GPACalc