sohai 0 Newbie Poster

Now, I've just done a calculation program. What I need now is to build a Semester class that will contain information about the list of subjects taken in a semester, the corresponding grades as well as the grade average for that semester.

How should i build the class to store in information needed??

This is my code without the Semester class :

//import the packages to use the classes in them into the program

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

/**
 *	A public class
 */
public class GPATest extends JFrame{

	//declaration of the private variables used in the program

	//Create the Menu Bar that contains the JMenu "file"
	private JMenuBar jmb = new JMenuBar();
	//Create the menu that contains the items "about and exit"
	private JMenu fileMenu = new JMenu("File");
	//Create the menu items abouT, and exiT
	private JMenuItem abouT, exiT;
	//Create the North Panel
	private JPanel njp = new JPanel();
	/**
	 *Create a label which ask the user to enter the suited way for
	 *calculating the grade point average and the semester average
	 */
	private JLabel jl = new JLabel("The Average form : ");
	//Create Radio Buttons
	private JRadioButton jrb4 = new JRadioButton(" 4");
	//Create a Button Group
	private ButtonGroup bg = new ButtonGroup();

	//Create the West Panels used to arrange the contents of the Graphical user interafce
	private JPanel wjp = new JPanel();
	private JPanel wwjp= new JPanel();
	private JPanel cwjp= new JPanel();
	private JPanel ewjp= new JPanel();
	//Create labels for subject, houres and averages to be entered by the user
	private JLabel sjl = new JLabel(" Subject ");
	private JLabel hjl = new JLabel(" Hours ");
	private JLabel ajl = new JLabel(" Average ");
	//Create text fields
	private JTextField sjtf[] = new JTextField[8];
	private JTextField hjtf[] = new JTextField[8];
	//Create a Combobox for averages
	private JComboBox jcb[] = new JComboBox[8];
	//An array of averages represented in letters as used in university
	private String grades[] = {"A","A-","B+","B","B-","C+","C","C-","D+","D","E"};

	//Create the Center Panel
	private JPanel cjp = new JPanel();
	//label for asking the user to enter the previous GPA
	private JLabel agpajl = new JLabel("Enter the previous GPA");
	//label for asking the user to enter the credit hours
	private JLabel hgpajl = new JLabel("Enter your Total Credit Hours");
	//label for referring the user current semester GPA
	private JLabel cgpajl = new JLabel("Your Current GPA");
	//label referring to the new Cumulative Grade Point Average
	private JLabel ngpajl = new JLabel("Your new CGPA");
	//Create the text fields
		//for previous grade point average
	private JTextField agpajtf = new JTextField("0.0", 4);
		//for credit hours
	private JTextField hgpajtf = new JTextField("0", 4);
		//for new grade point average
	private JTextField cgpajtf = new JTextField(4);
		//for semister average
	private JTextField ngpajtf = new JTextField(4);

	//Create the South Panel
	private JPanel sjp = new JPanel();
	//Create a button used to calculate the grade point average
	private JButton calculateButton= new JButton("Calculate");

	//numeric arrays of averages
	public static final double avg4[] = {4.00, 3.67, 3.33, 3.00, 2.67, 2.33, 2.00, 1.67, 1.33, 1.00, 0.00};

	//Constructor of GPA
	public GPATest(){
		super("JAVA™ Grade Point Average (JGPA)");
		setSize(500,300); //set the size of the screen
		setResizable(true); //the resize feature is enabled

		//Create Logical Relationship between JRadioButtons
		bg.add(jrb4);

		setJMenuBar(jmb); //for setting the Menu Bar
		jmb.add(fileMenu);//for adding the File Menu to the Menu Bar
		//allowing the file menu to be selected by pressing Alt+F
		fileMenu.setMnemonic('F');
		//add About and Exit items to file menu
		fileMenu.add(abouT = new JMenuItem("About"));
		fileMenu.add(exiT  = new JMenuItem("Exit"));
		//File | Exit action performed
		exiT.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				System.exit(0); //terminate the program
			}
		});
		//File | About action performed
		abouT.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				JOptionPane.showMessageDialog(null, new About(),"About" ,JOptionPane.PLAIN_MESSAGE);
			}
		});
		//for adding a Listener to windowClosing
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(0);
			}
		});

		//get the graphical user interface components display area
		Container cp = getContentPane();
		//set a Layout for the North Panel
		njp.setLayout(new FlowLayout(FlowLayout.LEFT));
		//add the label and radio buttons to the North Panel
		njp.add(jl);
		njp.add(jrb4);
		//add the North Panel to container cp
		cp.add("North", njp);

		//West Panel with defult layout
		wjp.setLayout(new BorderLayout());
		/**
		 * There are three panels in the West Panel
		 * 1- (West Region) wwjp pannel with 9 rows and 1 column
		 */
		wwjp.setLayout(new GridLayout(9, 1, 1, 1));
		//add "Subject" label to wwjp pannel
		wwjp.add(sjl);
		//array of text fields for optionally typing subjects' names
		for(int i = 0; i < sjtf.length; i++){ //loop 7 times
			//text field & add it to (West) wwjp panel
			wwjp.add(sjtf[i] = new JTextField("Subject", 6));
		}
		//add wwjp panel to the West Panel in west region
		wjp.add("West", wwjp);

		// 2- (Center Region) cwjp panel with 9 rows and 1 column
		cwjp.setLayout(new GridLayout(9, 1, 1, 1));
		//add "Hours" label to cwjp panel
		cwjp.add(hjl);
		//array of JTextFields for typing houres for each subject
    	for(int i = 0; i < hjtf.length; i++){ //loop 7 times
			//text field & add it to cwjp panel
			cwjp.add(hjtf[i] = new JTextField("0", 5));
		}
		//add cwjp pannel to wjp panel in the middle OR center region
		wjp.add("Center", cwjp);

		// 3- (East Region) ewjp panel with 9 rows and 1 column
		ewjp.setLayout(new GridLayout(9, 1, 1, 1));
		//add "Average" label to ewjp panel
		ewjp.add(ajl);
		//array of JComboBox for selecting the average for each subject
    	for(int i = 0; i < jcb.length; i++){ //loop 7 times
			//combo box & add it to ewjp panel
			ewjp.add(jcb[i] = new JComboBox(grades));
		}
		//add ewjp panel to wjp panel in east region
		wjp.add("East", ewjp);
		//add wjp to container cp
		cp.add("West", wjp);

		//The Center Panel in the right of the Frame
		cjp.setLayout(new FlowLayout(FlowLayout.RIGHT));
		/**
		 * Add previous GPA label, previous GPA TextField,
		 * Studied Hours label, Studied Hours TextField,
		 * new GPA label,  new GPA TextField,
		 * semester average label and semester average TextField
		 * to cjp pannel
		 */
    	cjp.add(agpajl);
		cjp.add(agpajtf);
		cjp.add(hgpajl);
		cjp.add(hgpajtf);
		cjp.add(cgpajl);
		cgpajtf.setEnabled(false); //the user can't write in this JTextField
		cjp.add(cgpajtf);
		cjp.add(ngpajl);
		ngpajtf.setEnabled(false); //the user can't write in this JTextField
		cjp.add(ngpajtf);
		//add cjp pannel to container cp
		cp.add("Center", cjp);

		//The South Panel to the right bottom of the Frame
		sjp.setLayout(new FlowLayout(FlowLayout.RIGHT));
		//add calculateButton to the South Panel
		sjp.add(calculateButton);
		//add sjp panel to the bottom of container cp
		cp.add("South", sjp);

		////////////////////////////////////////////////////
		///////////////////Calculating//////////////////////
		calculateButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){

				//declaration of variables
				DecimalFormat formatter = new DecimalFormat("0.00");
				int hours;
				int sumOfHours = 0;   //default
				double noGrade = 0;   //default
				double gpa = 0;       //default
				int earnedHours = 0;  //default
				double a = 0;         //default

				//print error messages for wrong input in previous gpa
				if(!jrb4.isSelected()){
					JOptionPane.showMessageDialog(null,"Please select your GPA!","Warning",JOptionPane.WARNING_MESSAGE);
				}
				else if(Double.parseDouble(agpajtf.getText())>4.00){
					JOptionPane.showMessageDialog(null,"Your previous GPA can't exceed 4.00!","Error",JOptionPane.WARNING_MESSAGE);
				}
				else if(Double.parseDouble(agpajtf.getText())<0.00){
					JOptionPane.showMessageDialog(null,"Your previous GPA can't less than 0.00!","Error",JOptionPane.WARNING_MESSAGE);
				}
				else{

				//loop 7 times
				for(int i = 0; i < 8; i++){
					//get hours from hjtf text field
					hours = Integer.parseInt(hjtf[i].getText());
					if (hours <= 0) continue;
					//get the grade value from the selected item & convert the value to String
					noGrade = getGrade(jcb[i].getSelectedItem().toString());
					//summation of houres * grade
					gpa += hours * noGrade;
					//calculating the summation of houres in the last semester
					sumOfHours += hours;
				}
				//semester average
				cgpajtf.setText("" + formatter.format(gpa / sumOfHours));
				gpa += Double.parseDouble(agpajtf.getText()) * Integer.parseInt(hgpajtf.getText());
				//new GPA
				ngpajtf.setText("" + formatter.format(gpa / (sumOfHours + Integer.parseInt(hgpajtf.getText()))));
				}
			}
			private double getGrade(String grade){
				//selecting the approperiate average value according to the selected radio button
				for(int j = 0; j < 9; j++)
				if(grades[j].equals(grade))
				if(jrb4.isSelected()) return avg4[j];
				throw new RuntimeException("Varify Vector, or letter Grade, This Error should not be thrown at all.");
			}
		});
		setVisible(true);
	}
	//Main Method
	public static void main(String[] args){
		GPATest gpa = new GPATest();
		//Centering the window
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//to get the size
		gpa.setLocation((screenSize.width - gpa.getWidth()) / 2, (screenSize.height - gpa.getHeight()) / 2);
	}
}
/**
 *	A class for creating about panel
 */
class About extends JPanel{
	public About(){
		JLabel jl = new JLabel(new ImageIcon("java.gif"));
	  	this.add(jl);
		JLabel label = new JLabel("SOZAI© PRODUCTION");
		this.add(label);
	}
}