Write a Java GUI program that will serve as a GPA calculator
Write a Java GUI program that will serve as a GPA calculator (using the 4.0 scale and only A, B, C, D, or F letter grades) that can accept one semester of five (5) courses. The program will allow the user to enter the letter grade for each of their courses. The interface should calculate the GPA for that semester. Create a Semester class that can contain information about a student’s semester. Information in the class should include student name, course names (CSCI 1302, MATH 1161, etc.), credit hours, and final letter grades. Each of the non-name items should be stored in separate collections. There should be methods to get total credit hours, calculate total quality points, and to calculate GPA as well as appropriate getters/setters for any data properties. The GUI program should create a semester instance with your current course names, subjects, numbers, and credit hours. These values will be used to populate the running GUI and can be created manually (aka hard-coded) in the program. The final grades will be set after the user enters them and the Calculate button is clicked. The interface should contain one set of each of these controls for every course in the semester: Course Name – Label or equivalent, value is pre-filled from Semester instance Credit Hours – TextField, Label or equivalent, value is pre-filled from Semester instance Letter Grade – TextField, ComboBox or equivalent, value is set by user The values in the Semester instance should be used to pre-populate values for this JavaFX GUI program. The program should allow a user to set letter grades for each of the classes and click the Calculate button to calculate semester GPA. The Calculate button should work by populating the final grades for the Semester instance, then using the GPA calculation method in the Semester class to calculate the semester GPA. The GUI program should then display the calculated GPA from the Semester instance visually. The Reset button should reset grades for the courses and the change the displayed GPA calculation to 0.
Detailed Requirements and Notes: This program should: o Assume that there are always 5 courses for being taken a semester. 2 o Pre-populate the class name and credit hours based on values in the created Semester instance. o Have a “calculate” button that, when clicked, calculates and displays the GPA to 2 decimals. The calculated value should be visible to the user in the interface (not printed to the console). The calculate operation should set values for the final letter grades within the created Semester instance based on user input. The GPA calculation should be calculated in the semester instance. o Have a “reset” button to reset all letter grades to their default state and set any calculated GPAs to 0.00. Research how GPA is calculated with quality points, letter grades, and credit hours to fit the 4.0 scale and use that calculation for this program. There are no visual requirements for this project. Design your interface however you would like with whatever controls you feel are best suited. However, you should: o Effectively use panes and spacing/padding/alignment options to present a clear and understandable interface; think of usability when designing. o Consider error prevention/detection o Test all cases (especially blank/null values) and handle them appropriately. Use an online GPA calculator to check the results of your calculations. Sketching the object will help you figure out which types of panes to use. UI Components can be stored and retrieved from ArrayLists. This may make creating and accessing these components easier.
Solution
GUI.java
package gpacalc;
import java.awt.Container;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JTextField;
public class GUI extends JFrame implements ActionListener {
JFrame mainFrame;
 JButton calculateBtn, resetBtn;
 JTextField[] creditHourTF;
 JTextField gpaTF;
 JLabel[] courseNameLabels;
 JLabel topLabel,gpaLabel,emptyLabel;
 JComboBox[] gradeSelect;
   
 Container con,buttons,gpaCon;
   
 public GUI(Semester s){
 mainFrame = new JFrame(\"GPA Calculator\");
 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
 calculateBtn = new JButton(\"Calculate\");
 resetBtn = new JButton(\"Reset\");
 topLabel = new JLabel(\"Hello\");
   
 con = new Container();
 con.setLayout(new GridLayout(Semester.NO_OF_COURSES, 3));
   
 creditHourTF = new JTextField[Semester.NO_OF_COURSES];
 courseNameLabels = new JLabel[Semester.NO_OF_COURSES];
 gradeSelect = new JComboBox[Semester.NO_OF_COURSES];
   
 String grades[] = {\"F\",\"D\",\"C\",\"B\",\"A\"};
 for(int i=0;i < Semester.NO_OF_COURSES;i++){
 courseNameLabels[i] = new JLabel(s.courses[i].getName());
 creditHourTF[i] = new JTextField();
 gradeSelect[i] = new JComboBox(grades);
   
 con.add(courseNameLabels[i]);
 con.add(creditHourTF[i]);
 con.add(gradeSelect[i]);
   
 }
   
 buttons = new Container();
 buttons.setLayout(new GridLayout(1,2));
 buttons.add(resetBtn);
 buttons.add(calculateBtn);
   
 gpaLabel = new JLabel(\"GPA\");
 gpaTF = new JTextField();
 emptyLabel = new JLabel();
 gpaCon = new Container();
 gpaCon.setLayout(new GridLayout(1,3));
 gpaCon.add(emptyLabel);
 gpaCon.add(gpaLabel);
 gpaCon.add(gpaTF);
   
 mainFrame.setSize(600, 400);
 mainFrame.setLayout(new GridLayout(4,1));
 mainFrame.add(topLabel);
 mainFrame.add(con);
 mainFrame.add(gpaCon);
 mainFrame.add(buttons);
 mainFrame.setVisible(true);
   
 resetBtn.addActionListener(this);
 calculateBtn.addActionListener(this);
 }
@Override
 public void actionPerformed(ActionEvent e) {
if(e.getSource() == resetBtn){
 for(int i=0;i < 5;i++){
 creditHourTF[i].setText(\"0.0\");
 gradeSelect[i].setSelectedIndex(0);
 gpaTF.setText(\"0.0\");
 }
 }
   
 if(e.getSource() == calculateBtn){
 double sum = 0,unit_sum =0;
 for(int i=0;i < 5;i++){
 System.out.println(creditHourTF[i].getText() + \" \" + gradeSelect[i].getSelectedIndex());
 sum += Double.parseDouble(creditHourTF[i].getText()) * gradeSelect[i].getSelectedIndex();
 unit_sum += Double.parseDouble(creditHourTF[i].getText());
 }
 System.out.println(sum);
 System.out.println(unit_sum);
 sum = sum / unit_sum;
 gpaTF.setText(Double.toString(sum));
 }
 }
   
 }
------------------------------------------------------
Semester.java
package gpacalc;
import java.io.BufferedReader;
 import java.io.InputStreamReader;
public class Semester {
public static final int NO_OF_COURSES = 5;
 public Course[] courses;
   
 public Semester(){
 courses = new Course[NO_OF_COURSES];
 for(int i=0;i < NO_OF_COURSES;i++){
 courses[i] = new Course();
 }
   
 System.out.println(\"Enter Names of 5 subjects: \");
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String[] names = new String[NO_OF_COURSES];
   
 try{
 for(int i=0;i < NO_OF_COURSES;i++){
 names[i] = br.readLine();
 }
 }
 catch(Exception e){
 System.out.println(\"Error\");
 }
   
 for(int i=0;i < NO_OF_COURSES;i++){
 courses[i].setName(names[i]);
 }
 }
   
 }
------------------------------------------------------
Course.java
package gpacalc;
public class Course {
private String name;
 private String grade;
 private double credit_hours;
   
 public Course(){
 name = \"\";
 grade = \"A\";
 credit_hours = 0.0;
 }
   
 public String getName(){
 return name;
 }
   
 public void setName(String newName){
 this.name = newName;
 }
   
 public String getGrade(){
 return grade;
 }
   
 public void setGrade(String newGrade){
 this.grade = newGrade;
 }
   
 public double getCreditHours(){
 return credit_hours;
 }
   
 public void setCreditHours(double newCreditHours){
 this.credit_hours = newCreditHours;
 }
 }
------------------------------------------------------
GPAcalc.java
package gpacalc;
public class GPAcalc {
public static void main(String[] args) {
   
 Semester s = new Semester();
 GUI g = new GUI(s);
 }
}
------------------------------------------------------




