The project h20_gpa_calculator_starter contains the GPACalculator class, already implemented to create the application shown below. GPA Calculator Enter your grade: ADD Enter number of units: GPA Reset Exit The user can enter all the grades for any number of courses and calculate the total GPA. The ADD button will add one course after another, and the GPA button calculates the cumulative GPA. The Reset button clears all entries so that the user can start from scratch. The Exit button closes the program. The user enters the grade as a letter, e.g. A, B, C, D, or F. The user enters the units as a number of credit hours, e.g. 1, 2, 3, 4, etc. Step 1– Make the GPA calculations work Your job is to process the grade and units information when the user clicks the ADD or Reset buttons and to compute the GPA when the user clicks the GPA button. There are five (5) places in the given code for you to add your code. Each of these is commented with a brief description of your task followed by the phrase "Your code here (n of 5).". • The variable totalUnits stores the total credits of all classes taken. • The variable gradePoints stores the total grade points earned in those classes taken. For example, if you got a B in a 4 credit hour course, you earn 3.0 x 4 = 12.0 grade points. The variable totalGPA stores the total GPA, which is simply the total grade points earned divided by the total units taken. For example, if you got an A in a 3 credit course, a C in a 5 credit course, and a B in a 4 credit course, you have (4.0 x 3) + (2.0 x 5) + (3.0 x 4) = 34.0 grade points and 3+ 5 + 4 = 12 total units, so the GPA is 34.0/ 12 = 2.833. %3!
Question is in images
Starter code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class GPACalculator extends JFrame
{
private JLabel gradeL, unitsL, emptyL;
private JTextField gradeTF, unitsTF;
private JButton addB, gpaB, resetB, exitB;
private AddButtonHandler abHandler;
private GPAButtonHandler cbHandler;
private ResetButtonHandler rbHandler;
private ExitButtonHandler ebHandler;
private static final int WIDTH = 400;
private static final int HEIGHT = 150;
private static double totalUnits = 0.0; // total units taken
private static double gradePoints = 0.0; // total grade points from those units
private static double totalGPA = 0.0; // total GPA (gradePoints / totalUnits)
//Constructor
public GPACalculator()
{
//Create labels
gradeL = new JLabel("Enter your grade: ", SwingConstants.RIGHT);
unitsL = new JLabel("Enter number of units: ", SwingConstants.RIGHT);
emptyL = new JLabel("",SwingConstants.RIGHT);
//Create text fields
gradeTF = new JTextField(10);
unitsTF = new JTextField(10);
//Create Add Button
addB = new JButton("ADD");
abHandler = new AddButtonHandler();
addB.addActionListener(abHandler);
//Create GPA Button
gpaB = new JButton("GPA");
cbHandler = new GPAButtonHandler();
gpaB.addActionListener(cbHandler);
//Create Reset Button
resetB = new JButton("Reset");
rbHandler = new ResetButtonHandler();
resetB.addActionListener(rbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Set the title of the window
setTitle("GPA Calculator");
//Get the container
Container pane = getContentPane();
//Set the layout
//3 rows, 3 columns
pane.setLayout(new GridLayout(3, 3));
//Place the components in the pane
pane.add(gradeL);
pane.add(gradeTF);
pane.add(addB);
pane.add(unitsL);
pane.add(unitsTF);
pane.add(gpaB);
pane.add(emptyL);
pane.add(resetB);
pane.add(exitB);
//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class AddButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//If there are no entries, display message
if (gradeTF.getText().equals("") && unitsTF.getText().equals(""))
{
noEntryMsg();
}
else
{
//Read the score and the units
char grade = gradeTF.getText().charAt(0);
double units = Double.parseDouble(unitsTF.getText()); // This can throw a NumberFormatException
// Catch it!
double points = 0.0;
// Calculate points according to the entries (grade & units)
// For example:
// When grade is 'a' or 'A', the points should be units * 4.0
// When grade is 'b' or 'B', the points should be units * 3.0
// etc...
// When grade is 'f' or 'F', the points should be 0.0
// If the grade is not A-D or F, set both the points and the units to 0.0
// and show a message dialog box informing the user of the error.
// Your code here (1 of 5)....
// Add units to total units
// Add points to total grade points
// Your code here (2 of 5)....
//Reset text fields
gradeTF.setText("");
unitsTF.setText("");
}
}
}
private class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String msg = "Resetting will delete all entries.\n" +
"Are you sure you want to continue?";
int answer = JOptionPane.showConfirmDialog (null,
msg, "Click Yes of No",JOptionPane.YES_NO_OPTION);
//Display window to try again
if (answer == JOptionPane.YES_OPTION)
{
//Reset text fields
gradeTF.setText("");
unitsTF.setText("");
// Reset total units and total grade points
// Your code here (3 of 5)....
}
}
}
private class GPAButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (gradePoints == 0.0)
{
if (gradeTF.getText().equals("") && unitsTF.getText().equals(""))
{
noEntryMsg();
}
else
{
String msg = "Click the ADD button\n and then click GPA again.";
//Show results on message box
JOptionPane.showMessageDialog(null, msg, "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
// Compute totalGPA by dividing the total grade points by the total units
// Your code here (4 of 5)....
DecimalFormat df = new DecimalFormat("0.000");
String outputStr = "GPA: " + df.format(totalGPA) + "\n";
//Show results on message box
JOptionPane.showMessageDialog(null, outputStr, "Result",
JOptionPane.INFORMATION_MESSAGE);
int answer = JOptionPane.showConfirmDialog (null,
"Try again?", "Click Yes of No",
JOptionPane.YES_NO_OPTION);
//Display window to try again
if (answer == JOptionPane.YES_OPTION)
{
//Reset text fields
gradeTF.setText("");
unitsTF.setText("");
// Reset total units and total grade points
// Your code here (5 of 5)....
}
else if (answer == JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void noEntryMsg()
{
String msg = "Enter grade and number of units.";
//Show results on message box
JOptionPane.showMessageDialog(null, msg, "Result",
JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
GPACalculator gpaCalculator = new GPACalculator();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images