Need help with College work Please
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Need help with College work Please

  1. #1
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Need help with College work Please

    Ive been given the assignment to make a GUI for a user to enter test scores and it will tell the user how many test scores the average of the scores and the best test score entered with 3 buttons at the bottom labeled "Enter Score", "Clear" , and "Exit".
    I'm having coders block so to speak of i cant figure out how i can have one text field that will hold any number of data entered.

    Can you help me?

    Thanks for any help!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with College work Please

    If you want to display multiple lines of data you would be better of using a JTextArea.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Re: Need help with College work Please

    how do i store the data entered tho? its will look like this
    "Test Score: [ ]"(editable)
    "Number of Scores:[ ]"(non-editable)
    "Average Score:[ ]"(non-editable)
    "Best Score:[ ]"(non-editable)

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with College work Please

    You shouldn't mix editable and non editable content in a single component it adds unnecessary complexity to your code and it confuses the user.

    Have a separate field for entering scores or maybe use a JOptionPane to get the data and have it pop up when the user presses the Enter Score button.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Re: Need help with College work Please

    Its not my choice its part of the assignment lol
    theres no set number of how many scores will be entered so a separate field for each would be too much work.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with College work Please

    You don't need a separate field for each score, you have just one input field which you enter a value into and press the Enter Score button. The score then gets added to your data and the field is wiped ready for the next score.

    If you can't use a separate input field use a JOptionPane to get each score as I suggested earlier.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Re: Need help with College work Please

    ok thanks if i run into any problems ill post here

  8. #8
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help with College work Please

    I think the problem DarkXBlaze is handling that data.

    You could make a large integer array (maybe not more than 30?) and add the scores to a global integer 'index' and increment it each time the user presses submit. Later you could use loops to find best and average scores.

    If you want to handle large amounts of data, you could use one of various data structures like linked lists or vectors. For even larger amounts, you might want to look up file I/O.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with College work Please

    You could make a large integer array (maybe not more than 30?) and add the scores to a global integer 'index' and increment it each time the user presses submit. Later you could use loops to find best and average scores.
    Why? The project does not require you to keep all the entered scores just how many test scores, the average of the scores and the best test score entered. You can do that with 3 variables (eg numScores, totalOfScores, maxScore).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help with College work Please

    Hmm. I guess you're right; you could continually update the above each time the user presses submit. Storing them would be rather pointless and a waste of resources.

  11. #11
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Re: Need help with College work Please

    Lol I was like What??? For a min that's way more skilled than I would even know how to do atm lol
    Nice name btw

  12. #12
    Join Date
    Nov 2011
    Location
    TN,USA
    Posts
    6

    Re: Need help with College work Please

    hey i need some more help.
    how do i find the max value entered?(see "bestScore = " its in Bold text)
    Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
    
    public class A5_assignment extends JFrame
    {
        private static final int WIDTH = 400;
        private static final int HEIGHT = 300;
    
        private JLabel numScoreL, totalOfScoreL, averageOfScoresL, maxScoreL;
        private JTextField numScoreTF, totalOfScoreTF, averageOfScoresTF, maxScoreTF;
        private JButton enterScoreB, clearB, exitB;
        
        private CalculateButtonHandler cbHandler;
        private ExitButtonHandler ebHandler;
        private ClearButtonHandler cbClearHandler; 
        
        public A5_assignment()
        {
    	    
        	numScoreL = new JLabel("Test Score :",SwingConstants.RIGHT);
        	totalOfScoreL = new JLabel("Number of Scores:",SwingConstants.RIGHT);
        	averageOfScoresL = new JLabel("Average Score:",SwingConstants.RIGHT);
        	maxScoreL = new JLabel("Best Score:",SwingConstants.RIGHT);
            
        	numScoreTF = new JTextField(10);
        	totalOfScoreTF = new JTextField(10);
        	totalOfScoreTF.setEditable(false);
        	averageOfScoresTF = new JTextField(10);
        	averageOfScoresTF.setEditable(false);
        	maxScoreTF = new JTextField(10);
        	maxScoreTF.setEditable(false);
            
        	enterScoreB = new JButton("Enter Score");
            cbHandler = new CalculateButtonHandler();
            enterScoreB.addActionListener(cbHandler);
            
            clearB = new JButton("Clear");
            cbClearHandler = new ClearButtonHandler();
            clearB.addActionListener(cbClearHandler);
            
            exitB = new JButton("exit");
            ebHandler = new ExitButtonHandler();
            exitB.addActionListener(ebHandler);
            
            setTitle("Test Scores");
            
            Container pane = getContentPane();
            
            pane.setLayout(new GridLayout(5,2));
            
            pane.add(numScoreL);
            pane.add(numScoreTF);
            pane.add(totalOfScoreL);
            pane.add(totalOfScoreTF);
            pane.add(averageOfScoresL);
            pane.add(averageOfScoresTF);
            pane.add(maxScoreL);
            pane.add(maxScoreTF);
            pane.add(enterScoreB);
            pane.add(clearB);
            pane.add(exitB);
            
            setSize(WIDTH,HEIGHT);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
          	setVisible(true);
        }
    
        private class CalculateButtonHandler implements ActionListener
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		double score, numberOfScore, average, bestScore;
        		
        		score = Double.parseDouble(numScoreTF.getText());
        		numberOfScore = score + 1;
        		average = score / numberOfScore;
        		bestScore = ;
        		
        		totalOfScoreTF.setText(" " + area);
        		averageOfScoresTF.setText(" " + perimeter);
        		maxScoreTF.setText(" " + perimeter);
        	}
        }
      
        private class ClearButtonHandler implements ActionListener
        {
        	public void actionPerformed(ActionEvent e) 
        	{
        		numScoreTF.setText("");
        		totalOfScoreTF.setText("");
        		averageOfScoresTF.setText("");
        		maxScoreTF.setText("");
    		}
        }   
        
        private class ExitButtonHandler implements ActionListener
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		System.exit(0);
        	}
        }
        
        
        public static void main(String[] args)
        {
        	A5_assignment rectProg = new A5_assignment();
        }
    }
    Last edited by DarkXBlaze; November 7th, 2011 at 12:04 PM.

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with College work Please

    As I said in an earlier post you need to maintain 3 values eg numScores, totalOfScores, maxScore. These must be instance variables of the A5_assignment class.

    The logic is simple, everytime you submit a new value do the following:
    • Increment numScores
    • Add the entered value to totalOfScores
    • If the entered value is greater than maxScores set maxScores to the entered value.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center