CS 1401 Assignment #6, Solutions

Date Assigned: Monday, February 26, or Tuesday, February 27, 2007.

Due Date: Monday, March 5, or Tuesday, March 6, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to use GUI.

Programming assignment: design and implement a GUI program that asks a student for the student's name and letter grades (A, B, etc.) in the first three CS classes (CS1, CS2, and CS3), and returns the student's GPA in CS. A GPA is an arithmetic average of the numerical grades corresponding to letter grades: A is counted as 4.0, B as 3.0, C as 2.0, D as 1.0, and F as 0.0.

Please take into account that transfer grades (marked as TA, TB, etc.) are not counted. For example, if a student's letter grades had TA, B, and A, then this student's GPA is computed based only on two UTEP grades A ( = 4.0) and B ( = 3.0), and is therefore equal to (3.0 + 4.0) / 2 = 3.5. Feel free to assume that at least one of the grades is not a transfer grade.


Solution:

public class Assignment6{

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;

  public class GPA extends JFrame{
    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private JLabel nameL, cs1L, cs2L, cs3L, gpaL;
    private JTextField nameTF, cs1TF, cs2TF, cs3TF, gpaTF;
    private JButton computeB, exitB;

    private ComputeButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    public GPA(){
      //describe title
      setTitle("GPA in Computer Science");

      //create labels
      nameL = new JLabel("Enter your name: ", SwingConstants.RIGHT);
      cs1L = new JLabel("Enter CS1 letter grade (e.g. TA, A): ", SwingConstants.RIGHT);
      cs2L = new JLabel("Enter CS2 letter grade (e.g. TA, A): ", SwingConstants.RIGHT);
      cs3L = new JLabel("Enter CS3 letter grade (e.g. TA, A): ", SwingConstants.RIGHT);
      gpaL = new JLabel("Your GPA in CS: ", SwingConstants.RIGHT);

      //create text fields
      nameTF = new JTextField(20);
      cs1TF  = new JTextField(2);
      cs2TF  = new JTextField(2);
      cs3TF  = new JTextField(2);
      gpaTF  = new JTextField(10);

      //create compute button
      computeB = new JButton("Compute");
      cbHandler = new ComputeButtonHandler();
      computeB.addActionListener(cbHandler);

      //create exit button
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);

      //create the window
      Container window = getContentPane();

      //set the layout
      window.setLayout(new GridLayout(6,2));

      //place components of GUI into the pane
      window.add(nameL);    window.add(nameTF);
      window.add(cs1L);     window.add(cs1TF);
      window.add(cs2L);     window.add(cs2TF);
      window.add(cs3L);     window.add(cs3TF);
      window.add(gpaL);     window.add(gpaTF);
      window.add(computeB); window.add(exitB);

      //set the size of the window and display it
      setSize(WIDTH, HEIGHT);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class ComputeButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
        char letterGrade1, letterGrade2, letterGrade3;
        int grade1, grade2, grade3;
        int numberOfGrades = 0;
        double sumOfGrades = 0.0;

        letterGrade1 = cs1TF.getText().charAt(0);
        if (letterGrade1 != 'T') {numberOfGrades ++;}
        if (letterGrade1 == 'A')
          {sumOfGrades += 4.0;}
        else if (letterGrade1 == 'B')
          {sumOfGrades += 3.0;}
        else if (letterGrade1 == 'C')
          {sumOfGrades += 2.0;}
        else if (letterGrade1 == 'D')
          {sumOfGrades += 1.0;}

        letterGrade2 = cs2TF.getText().charAt(0);
        if (letterGrade2 != 'T') {numberOfGrades ++;}
        if (letterGrade2 == 'A')
          {sumOfGrades += 4.0;}
        else if (letterGrade2 == 'B')
          {sumOfGrades += 3.0;}
        else if (letterGrade2 == 'C')
          {sumOfGrades += 2.0;}
        else if (letterGrade2 == 'D')
          {sumOfGrades += 1.0;}

        letterGrade3 = cs3TF.getText().charAt(0);
        if (letterGrade3 != 'T') {numberOfGrades ++;}
        if (letterGrade3 == 'A')
          {sumOfGrades += 4.0;}
        else if (letterGrade3 == 'B')
          {sumOfGrades += 3.0;}
        else if (letterGrade3 == 'C')
          {sumOfGrades += 2.0;}
        else if (letterGrade3 == 'D')
          {sumOfGrades += 1.0;}

        gpaTF.setText("" + (sumOfGrades / numberOfGrades));
      }
    }
    private class ExitButtonHandler implements ActionListener{
      public void actionPerformed(ActionEvent e){
        System.exit(0);
      }
    }
    public static void main(String [] args){
      GPA myGpaTool = new GPA();
    }
}

Homework assignment: on a separate sheet of paper, solve Ex. 8, 16, and 18 at the end of Chapter 6.

Deliverables: as instructed by your TA.