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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text: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
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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education