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

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

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

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

//place components of GUI into the pane

//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){

}
}
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.