Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying averages, problem

Status
Not open for further replies.

Flloyd

Programmer
Jan 20, 2003
36
CA
Hi i've written a program which stores digits in a vector called marks. I have been trying to make a button which when clicked takes the digits from the vector marks and displays the average number of those marks. The average will need to be displayed in a text box called txtdisplay.

If anyone could help i would be extremely grateful.
 
This is strange I post the solution to this question by a person with another user name (which I assume is you). However when I went back to look for the question so I could referer you to it, it has been deleted or something. That thread is no longer in this forum. That in it self is strange. Lucky I have the code changes I made and posted still, however I do not remember all the great things I said :) ... (it was very long post hehe) I will just post the code, and it has comments where I made modifications, and where there was no code but there is now has no comments. So to keep it short here is the code, if you have questions just ask. It compliles and runs, and all the buttons now function per your orignal discription from your first post. :)

Rodney

Code:
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class ProjectTester extends Applet implements ActionListener
{
    private TextField txtName, txtMark, txtExam, txtDisplayName, txtDisplayMark, txtDisplayExam, txtDisplayAverage, txtGrade;

    private Button btnAddDetails, btnPass, btnFail, btnDistinction, btnAverage, btnDisplayAll, btnReset, btnExit;

    private Vector Names, Marks, Exams;

    private int allMarks=0;

    public void init()
    {
        Names = new Vector(1);
        Marks = new Vector(1);
        Exams = new Vector(1);

        setLayout(new GridLayout (0,1));
        setBackground(Color.green);

        Label lblName, lblMark, lblExam, lblDisplayName, lblDisplayMark, lblDisplayExam, lblDisplayAverage, lblPassMark;

        lblName    = new Label("Enter Name");
        lblMark    = new Label("Enter Course Work Mark");
        lblExam    = new Label("Enter Exam Mark");
        lblDisplayName = new Label("Student Names:");
        lblDisplayMark = new Label("Course Work Marks:");
        lblDisplayExam = new Label("Exam Marks:");
        lblDisplayAverage = new Label("Average Course Mark");
        lblPassMark = new Label("Marks Displayed");

        txtName    = new TextField("", 10);
        txtMark    = new TextField("", 4);
        txtExam    = new TextField("", 4);
        txtDisplayName = new TextField("", 250);
        txtDisplayMark = new TextField("", 250);
        txtDisplayExam = new TextField("", 250);
        txtDisplayAverage = new TextField("", 5);
        txtGrade = new TextField("", 5);

        btnAddDetails = new Button("Add Details");
        btnPass    = new Button("Show Pass Marks");
        btnFail    = new Button("Show Fail Marks");
        btnDistinction = new Button("Show Distinction Marks");
        btnAverage = new Button("Show Average Marks");
        btnDisplayAll = new Button("Show All");
        btnReset = new Button("Reset");
        btnExit    = new Button("Exit");

        btnAddDetails.addActionListener(this);
        btnPass.addActionListener(this);
        btnFail.addActionListener(this);
        btnDistinction.addActionListener(this);
        btnAverage.addActionListener(this);
        btnDisplayAll.addActionListener(this);
        btnReset.addActionListener(this);
        btnExit.addActionListener(this);

        add(lblName);
        add(txtName);
        add(lblMark);
        add(txtMark);
        add(lblExam);
        add(txtExam);
        add(btnAddDetails);
        add(lblDisplayName);
        add(txtDisplayName);
        add(lblDisplayMark);
        add(txtDisplayMark);
        add(lblDisplayExam);
        add(txtDisplayExam);
        add(btnDisplayAll);
        add(btnPass);
        add(btnFail);
        add(btnDistinction);
        add(lblPassMark);
        add(txtGrade);
        add(btnAverage);
        add(lblDisplayAverage);
        add(txtDisplayAverage);
        add(btnReset);
        add(btnExit);
    }

    public void adjustmentValueChanged(AdjustmentEvent event)
    {


    }


    // Buttons, and Enter key of TextFields
    public void actionPerformed(ActionEvent event)
    {

        // Button Add Details

        if(event.getSource() == btnAddDetails)
        {
            Names.addElement(txtName.getText());
            Marks.addElement(txtMark.getText());
            Exams.addElement(txtExam.getText());
            txtName.setText("");
            txtMark.setText("");
            txtExam.setText("");
        }

        // Button Display All

        if(event.getSource() == btnDisplayAll)
        {
            txtDisplayName.setText("");
            txtDisplayMark.setText("");
            txtDisplayExam.setText("");
            Enumeration personName = Names.elements();
            Enumeration personMark = Marks.elements();
            Enumeration personExam = Exams.elements();
            StringBuffer bufPersonName = new StringBuffer();
            StringBuffer bufPersonMark = new StringBuffer();
            StringBuffer bufPersonExam = new StringBuffer();

            while (personName.hasMoreElements())
                bufPersonName.append(personName.nextElement()).append(" ");

            txtDisplayName.setText(bufPersonName.toString());

            while (personMark.hasMoreElements())
                bufPersonMark.append(personMark.nextElement()).append(" ");

            txtDisplayMark.setText(bufPersonMark.toString());

            while (personExam.hasMoreElements())
                bufPersonExam.append(personExam.nextElement()).append(" ");

            txtDisplayExam.setText(bufPersonExam.toString());
        }

        //Pass Button

        if(event.getSource()== btnPass)
        {
            /**
             * Not sure what you are trying
             * to do here with the Name Field
             * since you are not displaying
             * the results.
             */

            txtGrade.setText("");
            Enumeration pNameField = Names.elements();
            Enumeration pGradeField = Marks.elements();
            StringBuffer bufPNameField = new StringBuffer();
            StringBuffer bufPGradeField = new StringBuffer();

            while( true )
            {
                boolean b = pNameField.hasMoreElements();

                if( b == false )
                    return;

                boolean b1 = pGradeField.hasMoreElements();

                String strPGrade = ( String ) pGradeField.nextElement();
                Integer integer = new Integer( strPGrade );

                /**
                 * If this is for PASSing and you
                 * stated FAIL is below 40, then
                 * a PASS would be 40 or greater.
                 */
                if( integer.intValue() >= 40) // && integer.intValue() < 50 )
                {
                    bufPNameField.append( pNameField.nextElement()).append(&quot;&quot;);
                    txtGrade.setText (bufPNameField.toString());
                    /**
                     * This change was to place a
                     * space between grades.
                     */                              //    |...|
                    bufPGradeField.append(strPGrade).append(&quot; &quot;);
                    txtGrade.setText (bufPGradeField.toString());
                }
                else
                    pNameField.nextElement();
            }
        }

        //Fail Button
        if(event.getSource()== btnFail)
        {
            txtGrade.setText(&quot;&quot;);
            int currentMark = 0;

            for (int i = 0; i < Marks.size(); i++)
            {
                currentMark = Integer.parseInt((String)Marks.get(i));

                if (currentMark < 40)
                    txtGrade.setText(txtGrade.getText() + currentMark + &quot; &quot;);
            }
        }

        //Distinction Button
        if(event.getSource() == btnDistinction)
        {
            txtGrade.setText(&quot;&quot;);
            int currentMark = 0;

            for (int i = 0; i < Marks.size(); i++)
            {
                currentMark = Integer.parseInt((String)Marks.get(i));

                if (75 < currentMark)
                    txtGrade.setText(txtGrade.getText() + currentMark + &quot; &quot;);
            }
        }

        // Average Button
        if (event.getSource() == btnAverage)
        {
            txtGrade.setText(&quot;&quot;);
            int markTotal = 0;


            for (int i = 0; i < Marks.size(); i++)
                markTotal += Integer.parseInt((String)Marks.get(i));

            txtDisplayAverage.setText(&quot;&quot; + markTotal / Marks.size());
        }

        // Reset Button
        if (event.getSource() == btnReset)
        {
            txtDisplayName.setText(&quot;&quot;);
            txtDisplayMark.setText(&quot;&quot;);
            txtDisplayExam.setText(&quot;&quot;);
            txtGrade.setText(&quot;&quot;);
        }

        // Exit Button

        if (event.getSource() == btnExit)
            System.exit(0);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top