GPACalculator

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


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();
}
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Unreferenced Objects
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
  • SEE MORE 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