TechSpot

Hello been awhile since i last posted...

By Jacal
Feb 8, 2008
Post New Reply
  1. hey wasup hope yall been doing well, with school i have been very busy so havnt been online much. Anyway i am i need of some help in regards to one of my class assignments. i am getting alot of errors in my program and am wondering if yall could help me in fixing them. its three of my buttons that i have problem with. Previous, Next and Done i cant get them to work the way i would like them to. Previous button should go back one space in the array and show the previous array data, next button should move forward the array and let me input data, and the done button should should the data collected. the code i currently have is working to a point but not the way i expect that is if i comment out the previous and done button listeners and class for the listeners.

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

    public class GradeCalculator extends JFrame {

    private JTextField gradeA = new JTextField(3);
    private JTextField gradeB = new JTextField(3);
    private JTextField gradeC = new JTextField(3);
    private JTextField gradeD = new JTextField(3);
    private JTextField gradeE = new JTextField(3);
    private JTextField studentsName = new JTextField(15);
    private JTextField assignmentOne = new JTextField(5);
    private JTextField assignmentTwo = new JTextField(5);
    private JTextField assignmentThree = new JTextField(5);
    private JTextField examOne = new JTextField(5);
    private JTextField examTwo = new JTextField(5);
    private JTextArea textOutput;
    private JPanel buttonJPanel;
    private JPanel buttonJPanelTwo;

    public GradeCalculator(){

    JMenu fileMenu = new JMenu("File");

    JMenuItem aboutItem = new JMenuItem( "About" );
    fileMenu.add( aboutItem );
    aboutItem.addActionListener(

    new ActionListener()
    {
    public void actionPerformed( ActionEvent event )
    {
    JOptionPane.showMessageDialog( GradeCalculator.this,
    "Grade Calculator Version 1.00. \nMade by Carlson Smith.\nSchool: Pre-University\nSubject: Cape Computer Science.",
    "About", JOptionPane.PLAIN_MESSAGE );
    }
    }
    );


    JMenuItem exitItem = new JMenuItem( "Exit" );
    fileMenu.add( exitItem );
    exitItem.addActionListener(

    new ActionListener()
    {
    public void actionPerformed( ActionEvent event )
    {
    System.exit( 0 );
    }
    }
    );

    JMenuBar bar = new JMenuBar();
    setJMenuBar(bar);
    bar.add(fileMenu);

    JTabbedPane tabbedPane = new JTabbedPane();

    buttonJPanel = new JPanel();
    buttonJPanel.setLayout(new GridLayout(1,2));
    JButton ok = new JButton("OK");
    ok.addActionListener(new OkBtnListener());
    JButton edit = new JButton("EDIT");
    edit.addActionListener(new EditBtnListener());

    buttonJPanelTwo = new JPanel();
    buttonJPanelTwo.setLayout(new GridLayout(1,3));
    JButton previous = new JButton("PREVIOUS");
    //previous.addActionListener(new PreviousBtnListener());
    JButton done = new JButton("DONE");
    //done.addActionListener(new DoneBtnListener());
    JButton next = new JButton("NEXT");
    next.addActionListener(new NextBtnListener());

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new FlowLayout());

    contentPane.add(new JLabel("Grade A")/**,BorderLayout.NORTH*/);
    contentPane.add(gradeA/**,BorderLayout.NORTH*/);
    contentPane.add(new JLabel("Grade B")/**,BorderLayout.WEST*/);
    contentPane.add(gradeB/**,BorderLayout.WEST*/);
    contentPane.add(new JLabel("Grade C")/**,BorderLayout.WEST*/);
    contentPane.add(gradeC/**,BorderLayout.WEST*/);
    contentPane.add(new JLabel("Grade D")/**,BorderLayout.WEST*/);
    contentPane.add(gradeD/**,BorderLayout.WEST*/);
    contentPane.add(new JLabel("Grade E")/**,BorderLayout.WEST*/);
    contentPane.add(gradeE/**,BorderLayout.WEST*/);
    tabbedPane.addTab("Grade Entry Tab", null, contentPane, "Grade Tab");

    //contentPane.add(ok,BorderLayout.WEST);
    //contentPane.add(edit,BorderLayout.WEST);
    buttonJPanel.add(ok);
    buttonJPanel.add(edit);
    contentPane.add(buttonJPanel, BorderLayout.WEST);

    JPanel content = new JPanel();
    content.setLayout(new FlowLayout());

    content.add(new JLabel("Student's Name")/**,BorderLayout.EAST*/);
    content.add(studentsName/**,BorderLayout.EAST*/);
    content.add(new JLabel("Assignment 1 score:")/**,BorderLayout.EAST*/);
    content.add(assignmentOne/**,BorderLayout.EAST*/);
    content.add(new JLabel("Assignment 2 score:")/**,BorderLayout.EAST*/);
    content.add(assignmentTwo/**,BorderLayout.EAST*/);
    content.add(new JLabel("Assignment 3 score:")/**,BorderLayout.EAST*/);
    content.add(assignmentThree/**,BorderLayout.EAST*/);
    content.add(new JLabel("Exam 1 score:")/**,BorderLayout.EAST*/);
    content.add(examOne/**,BorderLayout.EAST*/);
    content.add(new JLabel("Exam 2 score:")/**,BorderLayout.EAST*/);
    content.add(examTwo/**,BorderLayout.EAST*/);
    //content.add(previous,BorderLayout.EAST);
    //content.add(done,BorderLayout.EAST);
    //content.add(next,BorderLayout.EAST);
    buttonJPanelTwo.add(previous);
    buttonJPanelTwo.add(done);
    buttonJPanelTwo.add(next);
    content.add(buttonJPanelTwo, BorderLayout.EAST);

    Box box = Box.createVerticalBox();
    String output = "test";
    String outputTwo = output;
    textOutput = new JTextArea(outputTwo,10,30);
    box.add(new JScrollPane(textOutput));
    textOutput.setEditable(false);
    content.add(box,BorderLayout.SOUTH);

    tabbedPane.addTab("Calculator Tab", null, content, "Calculation & Output Tab");

    setContentPane(tabbedPane);
    pack();
    setTitle("Grade Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    }

    class OkBtnListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    String gradeAA = gradeA.getText();
    double gradA = Double.parseDouble(gradeAA);
    gradeA.setText(" "+gradA);

    String gradeBB = gradeB.getText();
    double gradB = Double.parseDouble(gradeBB);
    gradeB.setText(" "+gradB);

    String gradeCC = gradeC.getText();
    double gradC = Double.parseDouble(gradeCC);
    gradeC.setText(" "+gradC);

    String gradeDD = gradeD.getText();
    double gradD = Double.parseDouble(gradeDD);
    gradeD.setText(" "+gradD);

    String gradeEE = gradeE.getText();
    double gradE = Double.parseDouble(gradeEE);
    gradeE.setText(" "+gradE);

    gradeA.setEditable(false);
    gradeB.setEditable(false);
    gradeC.setEditable(false);
    gradeD.setEditable(false);
    gradeE.setEditable(false);
    }
    }

    class EditBtnListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    gradeA.setEditable(true);
    gradeB.setEditable(true);
    gradeC.setEditable(true);
    gradeD.setEditable(true);
    gradeE.setEditable(true);
    }
    }
    /**
    class PreviousBtnListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    int i;

    if (i <= 0){
    i = 0;
    }else if(i > 0){
    i = i-1;
    }
    }
    }

    /**class DoneBtnListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    output = "Results \n";
    output+= studentName+" Assignment Average = "+averageO+" Exam Average = "+averageT+" Overall average = ";
    output+= tAverage+" Letter Grade = "+letterGrade;
    }
    }
    */
    class NextBtnListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    String [] studentName = new String [40];

    double [] assignOne = new double [40];
    double [] assignTwo = new double [40];
    double [] assignThree = new double [40];
    double [] gradeOne = new double [40];
    double [] gradeTwo = new double [40];
    double [] averageO = new double [40];
    double [] averageT = new double [40];
    double [] tAverage = new double [40];

    char [] letterGrade = new char [40];

    double max = 0;
    double min = 100;

    int i = 0;

    while(i < studentName.length){

    studentName = studentsName.getText();
    studentsName.setText(studentName);

    String aOne = assignmentOne.getText();
    assignOne = Double.parseDouble(aOne);
    //assignmentOne.setText(assignOne);
    String aTwo = assignmentTwo.getText();
    assignTwo = Double.parseDouble(aTwo);
    //assignmentTwo.setText(assignTwo);
    String aThree = assignmentThree.getText();
    assignThree = Double.parseDouble(aThree);
    //assignmentThree.setText(assignThree);

    String gOne = examOne.getText();
    gradeOne = Double.parseDouble(gOne);
    //examOne.setText(gradeOne);
    String gTwo = examTwo.getText();
    gradeTwo = Double.parseDouble(gTwo);
    //examTwo.setText(gradeTwo);

    averageO = (assignOne+assignTwo+assignThree[i])/3;
    averageT[i] = (gradeOne[i]+gradeTwo[i])/2;
    tAverage[i] = (averageO[i]+averageT[i])/2;

    if (tAverage[i] >= Double.parseDouble(gradeA.getText())){
    letterGrade[i] = 'A';
    }else if(tAverage[i] >= Double.parseDouble(gradeB.getText())){
    letterGrade[i] = 'B';
    }else if(tAverage[i] >= Double.parseDouble(gradeC.getText())){
    letterGrade[i] = 'C';
    }else if(tAverage[i] >= Double.parseDouble(gradeD.getText())){
    letterGrade[i] = 'D';
    }else if(tAverage[i] <= Double.parseDouble(gradeE.getText())){
    letterGrade[i] = 'E';
    }

    i++;
    }
    }
    }

    public static void main(String[]args){

    GradeCalculator calWindow = new GradeCalculator();
    calWindow.setVisible(true);
    }
    }[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]


Add New Comment

TechSpot Members
Login or sign up for free,
it takes about 30 seconds.
You may also...


Get complete access to the TechSpot community. Join thousands of technology enthusiasts that contribute and share knowledge in our forum. Get a private inbox, upload your own photo gallery and more.