Question

/** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...

/**
 * File: GradeCalculator . java
 * Description: Instances of this class are used to calculate
 *  a course average and a letter grade. In order to calculate
 *  the average and the letter grade, a GradeCalculator must store
 *  two essential pieces of data: the number of grades and the sum
 *  of the grades.  Therefore these are declared as object properties
 *  (instance variables).
 *  Each time calcAverage (grade) is called, a new grade is added to
 *  the running total, and the number of grades is incremented.
 */
/**
 *  1) Change the program so that it outputs +/- letter grades (you can decide
 *     the thresholds)
 *  2) Output, in addition to the average and letter grade, the number of grades
 *     entered so far. This requires a new method in the GradeCalculator class:
 *     public int getCount()
 *  3) Modify the addGrade() method in the GradeCalculator class so that it will
 *     reject grades that are out of range... and do not update the grade count if
 *     there is a problem with the grade range.
 *     a) notify the user that the grade has been rejected, but keep the program
 *        running
 *  4) Check user input for errors. The program should handle the entry of characters,
 *     and other non-numeric symbols without causing a crash and display error/informational 
 *     messages in a "new" text box in the panel.  
 */
/**
 *
 *
 */
public class GradeCalculator {
  private int gradeCount = 0; // GradeCalculator 's internal state
  private double gradeTotal = 0.0;
  /**
   *
   * calcAdd() is given a grade, which is added to
   * the running total. It then increments the grade count.
   * @param double grade
   * @return boolean
   */
  // Hint: change code to reject grades out of the range 0 to 100 by returning false, do
  // not update gradeCount if there is an error
  public boolean addGrade (double grade) {
    gradeTotal += grade;
    ++ gradeCount ;
    return true;
  } // calcAdd 
  /**
   * calcAverage() is given a grade, which is added to
   * the running total. It then increments the grade count
   * and returns the running average.
   * @return double average
   */
  public double calcAvg () {
    return gradeTotal / gradeCount ;
  } // calcAvg 
  //  HINT: Add a new public method that returns the gradeCount 
  //  public int getCount() -- fill in rest of code
  /**
   * calcLetterGrade() returns the letter grade for this object.
   * Algorithm: The course average is first computed from the stored
   * gradeTotal and gradeCount and then converted into a
   * letter grade.
   * @return a String representing "A" through "F"
   */
  public String calcLetterGrade () {
    // HINT: Change if/else logic for +/- letter grades
    double avg = gradeTotal / gradeCount ; // Get the average
    if (avg >= 90.0) {
      return "A";
    } else if (avg >= 80.0) {
      return "B";
    } else if (avg >= 70.0) {
      return "C";
    } else if (avg >= 60.0) {
      return "D";
    } else if (avg >= 0) {
      return "F";
    }
    return "Error";
   } // calcLetterGrade ()
} // GradeCalculator 

  /**
   * File: GradeCalcMain . java 
   * Description: This program creates a GradeCalcPanel and
   * adds it to the Frame's content pane and sets its size.
   */
  /**
   * 
   *
   */
  // See GradeCalculator . java for instructions

import javax .swing.*;

public class GradeCalcMain extends JFrame { 
  public GradeCalcMain () {
    getContentPane ().add(new GradeCalcPanel ());
    // register 'Exit upon closing' as a default close operation
    setDefaultCloseOperation ( EXIT_ON_CLOSE );
  } // end GradeCalcFrame () constructor
  public static void main(String args []){
    // change GUI so that it looks like Windows GUI, don't worry about this now
    try {
      UIManager . setLookAndFeel ("com.sun. java .swing. plaf .windows. WindowsLookAndFeel ");
    } catch (Exception e) {}
    GradeCalcMain   aframe = new GradeCalcMain ();
    aframe . setSize (450,75);
    aframe . setVisible (true);
  } // main()
} // GradeCalcMain class 

/**
 * File: GradeCalcPanel . java -
 * This class provides a user interface to
 * the GradeCalc class, which calculates a student's average
 * and letter grade for grades input into a JTextField .
 * The interface consists of input and output JTextFields and
 * and button to calculate the course average and letter grades. 
 * Also, add a text box to display user error/informational messages,
 * similar to resultField. 
 */
/**
 *
 * 
 * 
 */
// See GradeCalculator . java for instructions

import javax .swing.*;
import java . awt .*;
import java . awt .event.*;
import java .text.*;

public class GradeCalcPanel extends JPanel implements ActionListener {
  private JLabel prompt; // GUI components
  private JTextField   inputField ;
  private JLabel   resultLabel ;
  private JTextField   resultField ;
  private JButton button;
  private GradeCalculator calculator;  // The Calculator object

  public GradeCalcPanel () {
    calculator = new GradeCalculator (); // Create a calculator instance
    // setLayout ( new GridLayout (1,5,10,10));
    prompt = new JLabel ("Grade:");
    resultLabel = new JLabel ("Average:");
    inputField = new JTextField (10);
    resultField = new JTextField (20);
    resultField . setEditable (false);
    button = new JButton ("Enter");
    button. addActionListener (this);
    add(prompt);
    add( inputField );
    add(button);
    add( resultLabel );
    add( resultField );
    inputField . setText ("");
  } // end GradeCalcPanel ()

  /**
   * actionPerformed () handles clicks on the button. 
   * It takes the data from the input JTextFields , and sends them to
   * the GradeCalculater class to calculate a running average and
   * computes the letter grade, which are displayed in TextFields .
   * @param e -- the ActionEvent the generated this system call
   */
  public void actionPerformed ( ActionEvent e) {
    double grade, ave;
    DecimalFormat   df = new DecimalFormat ("0.00");
    String inputString = inputField . getText ();
    // HINT: use try/catch blocks to catch bad input to parseDouble ()
    grade = Double. parseDouble ( inputString );
    inputField . setText ("");
    // HINT: reject a bad grade in some way (the modified addGrade will return false
    // there is a problem with the grade
    calculator. addGrade (grade);
    // HINT: output grade count along with average and letter grade
    ave = calculator. calcAvg ();
    String average = "" + df .format(ave);
    String letterGrade = calculator. calcLetterGrade ();
    resultField . setText (average + " " + letterGrade );
  } // end actionPeformed ()
} // end GradeCalcPanel class
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#################### GradeCalcMain.class ##############
/**
* File: GradeCalcMain . java
* Description: This program creates a GradeCalcPanel and
* adds it to the Frame's content pane and sets its size.
*/
/**
*
*
*/
// See GradeCalculator . java for instructions

import javax.swing.*;

public class GradeCalcMain extends JFrame {
   public GradeCalcMain() {
       getContentPane().add(new GradeCalcPanel());
       // register 'Exit upon closing' as a default close operation
       setDefaultCloseOperation(EXIT_ON_CLOSE);
   } // end GradeCalcFrame () constructor

   public static void main(String args[]) {
       // change GUI so that it looks like Windows GUI, don't worry about this
       // now
       try {
           UIManager.setLookAndFeel("com.sun. java .swing. plaf .windows. WindowsLookAndFeel ");
       }
       catch (Exception e) {
       }
       GradeCalcMain aframe = new GradeCalcMain();
       aframe.setSize(450, 75);
       aframe.setVisible(true);
   } // main()
} // GradeCalcMain class

###################### GradeCalcPanel.java #################
/**
* File: GradeCalcPanel . java -
* This class provides a user interface to
* the GradeCalc class, which calculates a student's average
* and letter grade for grades input into a JTextField .
* The interface consists of input and output JTextFields and
* and button to calculate the course average and letter grades.
* Also, add a text box to display user error/informational messages,
* similar to resultField.
*/
/**
*
*
*
*/
// See GradeCalculator . java for instructions

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class GradeCalcPanel extends JPanel implements ActionListener {
   private JLabel prompt; // GUI components
   private JTextField inputField;
   private JLabel resultLabel;
   private JTextField resultField;
   private JButton button;
   private GradeCalculator calculator; // The Calculator object

   public GradeCalcPanel() {
       calculator = new GradeCalculator(); // Create a calculator instance
       // setLayout ( new GridLayout (1,5,10,10));
       prompt = new JLabel("Grade:");
       resultLabel = new JLabel("Average:");
       inputField = new JTextField(10);
       resultField = new JTextField(20);
       resultField.setEditable(false);
       button = new JButton("Enter");
       button.addActionListener(this);
       add(prompt);
       add(inputField);
       add(button);
       add(resultLabel);
       add(resultField);
       inputField.setText("");
   } // end GradeCalcPanel ()

   /**
   * actionPerformed () handles clicks on the button. It takes the data from
   * the input JTextFields , and sends them to the GradeCalculater class to
   * calculate a running average and computes the letter grade, which are
   * displayed in TextFields .
   * @param e -- the ActionEvent the generated this system call
   */
   public void actionPerformed(ActionEvent e) {
       double grade, ave;
       DecimalFormat df = new DecimalFormat("0.00");
       String inputString = inputField.getText();
       // HINT: use try/catch blocks to catch bad input to parseDouble ()
       try {
           grade = Double.parseDouble(inputString);
           inputField.setText("");
           // HINT: reject a bad grade in some way (the modified addGrade will
           // return false
           // there is a problem with the grade
           if (calculator.addGrade(grade)) {
               // HINT: output grade count along with average and letter grade
               ave = calculator.calcAvg();
               String average = "" + df.format(ave);
               String letterGrade = calculator.calcLetterGrade();
               int count = calculator.getCount();
               resultField.setText(average + " " + letterGrade + " count: " + count);
           }
           else {
               JOptionPane.showMessageDialog(null, "Grade is out of range", "InfoBox: error", JOptionPane.ERROR_MESSAGE);
           }
       }
       catch (Exception ex) {
           JOptionPane.showMessageDialog(null, "Invalid grade", "InfoBox: error", JOptionPane.ERROR_MESSAGE);
       }

   } // end actionPeformed ()
} // end GradeCalcPanel class

################### GradeCalculator.java ##################

/**
* File: GradeCalculator . java
* Description: Instances of this class are used to calculate
* a course average and a letter grade. In order to calculate
* the average and the letter grade, a GradeCalculator must store
* two essential pieces of data: the number of grades and the sum
* of the grades. Therefore these are declared as object properties
* (instance variables).
* Each time calcAverage (grade) is called, a new grade is added to
* the running total, and the number of grades is incremented.
*/
/**
* 1) Change the program so that it outputs +/- letter grades (you can decide
* the thresholds)
* 2) Output, in addition to the average and letter grade, the number of grades
* entered so far. This requires a new method in the GradeCalculator class:
* public int getCount()
* 3) Modify the addGrade() method in the GradeCalculator class so that it will
* reject grades that are out of range... and do not update the grade count if
* there is a problem with the grade range.
* a) notify the user that the grade has been rejected, but keep the program
* running
* 4) Check user input for errors. The program should handle the entry of characters,
* and other non-numeric symbols without causing a crash and display error/informational
* messages in a "new" text box in the panel.
*/
/**
*
*
*/
public class GradeCalculator {
   private int gradeCount = 0; // GradeCalculator 's internal state
   private double gradeTotal = 0.0;

   /**
   *
   * calcAdd() is given a grade, which is added to the running total. It then
   * increments the grade count.
   * @param double grade
   * @return boolean
   */
   // Hint: change code to reject grades out of the range 0 to 100 by returning
   // false, do
   // not update gradeCount if there is an error
   public boolean addGrade(double grade) {
       if (grade >= 0 && grade <= 100) {
           gradeTotal += grade;
           ++gradeCount;
           return true;
       }
       return false;
   } // calcAdd

   /**
   * calcAverage() is given a grade, which is added to the running total. It
   * then increments the grade count and returns the running average.
   * @return double average
   */
   public double calcAvg() {
       return gradeTotal / gradeCount;
   } // calcAvg
       // HINT: Add a new public method that returns the gradeCount
       // public int getCount() -- fill in rest of code

   /**
   * calcLetterGrade() returns the letter grade for this object. Algorithm:
   * The course average is first computed from the stored gradeTotal and
   * gradeCount and then converted into a letter grade.
   * @return a String representing "A" through "F"
   */
   public String calcLetterGrade() {
       // HINT: Change if/else logic for +/- letter grades
       double avg = gradeTotal / gradeCount; // Get the average
       if (avg >= 97.0) {
           return "A+";
       }
       else if (avg >= 93.0 && avg < 97.0) {
           return "A";
       }
       else if (avg >= 90.0 && avg < 93.0) {
           return "A-";
       }
       else if (avg >= 87.0 && avg < 90.0) {
           return "B+";
       }
       else if (avg >= 83.0 && avg < 87.0) {
           return "B";
       }
       else if (avg >= 80.0 && avg < 83.0) {
           return "B-";
       }
       else if (avg >= 77.0 && avg < 80.0) {
           return "C+";
       }
       else if (avg >= 73.0 && avg < 77.0) {
           return "C";
       }
       else if (avg >= 70.0 && avg < 73.0) {
           return "C-";
       }
       else if (avg >= 67.0 && avg < 70.0) {
           return "D+";
       }
       else if (avg >= 63.0 && avg < 67.0) {
           return "D";
       }
       else if (avg >= 50.0 && avg < 63.0) {
           return "D-";
       }
       else if (avg < 50) {
           return "F";
       }
       return "Error";
   } // calcLetterGrade ()

   public int getCount() {
       return gradeCount;
   }
} // GradeCalculator

Add a comment
Know the answer?
Add Answer to:
/** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT