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. Step 2 – Add exception handling You may notice that when you accidentally enter a non-number in the units field, it crashes your program. Instead of crashing add exception handling (using a try-catch block) to catch the problem. The error message tells you which statement causes the exception. Then you can wrap all the code related to that task (updating the points and units running totals) in a try-catch block. Use a JOptionPane.showMessageDialog(…) to report the error.
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. Step 2 – Add exception handling You may notice that when you accidentally enter a non-number in the units field, it crashes your program. Instead of crashing add exception handling (using a try-catch block) to catch the problem. The error message tells you which statement causes the exception. Then you can wrap all the code related to that task (updating the points and units running totals) in a try-catch block. Use a JOptionPane.showMessageDialog(…) to report the error.
![GPACalculator X
Compile
Undo
Cut
Сopy
Paste
Find...
Close
Source Code
//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)....
totalGPA = gradePoints / totalUnits;
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)....
totalUnits
0;
gradePoints
0;
saved](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F07c38dc6-4a9f-400d-9784-ec1cee64c4c5%2F4e1f4eed-2569-4af9-af71-75e34a0cc49f%2Fkfeivwi_processed.png&w=3840&q=75)
![GPACalculator X
Compile
Undo
Cut
Соpy
Paste
Find...
Close
Source Code
|// Your code here (2 of 5)....
totalUnits += units;
gradePoints += points;
|//Reset text fields
gradeTF.setText("");
unitsTF.setText("");
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "You entered:
JOptionPane.INFORMATION_MESSAGE);
+ unitsTF.getText(),"Please enter valid\nnumber of units",
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?";
JOptionPane.showConfirmDialog (null,
msg, "Click Yes of No",J0ptionPane. YES_NO_OPTION);
int answer
|7/Display window to try again
if (answer
{
JOptionPane.YES_OPTION)
==
|//Reset text fields
gradeTF.setText("");
unitsTF.setText("");
II I|
|// Reset total units and total grade points
|// Your code here (3 of 5)..
saved](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F07c38dc6-4a9f-400d-9784-ec1cee64c4c5%2F4e1f4eed-2569-4af9-af71-75e34a0cc49f%2Fagv1g3c_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)