user3764862
user3764862

Reputation: 25

how to use char in a array?

please I'd like to know how to make jtxtScore[6] show letter grade, It calculates grade average but I want to it to show letter grade too.``

public  class ScoreGUI extends JFrame implements ActionListener{
      final int SIZE=6;
      JButton jbtnCalculate = new JButton ("Calculate");

         JTextField [] jtxtScore = new JTextField[SIZE];
       JTextField [] jtxtWeight = new JTextField[SIZE];

    JLabel [] jlblModuleName = new JLabel[SIZE];    

     JPanel scorePanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    double []scores = new double[SIZE]; 
    double []weights = new double[SIZE]; 


    public ScoreGUI(){
        jlblModuleName [0]= new JLabel("Assignments");
        jlblModuleName [1] = new JLabel("Mid term");
        jlblModuleName [2]= new JLabel("final exam");
        jlblModuleName [3]= new JLabel("Final Project");
        jlblModuleName [4]= new JLabel("grade average");
        jlblModuleName [5]= new JLabel("letter grade");

        scorePanel.setBackground(Color.gray);
        scorePanel.setLayout(new GridLayout(7,3,5,9));
    for(int i=0; i<SIZE;i++){
        scorePanel.add(jlblModuleName[i]);
        jtxtScore[i] = new JTextField(8);
        scorePanel.add(jtxtScore[i]);
        jtxtWeight[i] = new JTextField(8);
        scorePanel.add(jtxtWeight[i]);
        //add(jtxtWeight[i]);
        }
        buttonPanel.setBackground(Color.CYAN);
        jbtnCalculate.addActionListener(this);
        buttonPanel.add(jbtnCalculate);

        add(scorePanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public double calculateScore(double[]score, double[]weight){

        double grade=0.0;

    grade = (score[0]*weight[0]+score[1]*weight[1]+score[2]*weight[2]+score[3]*weight[3]);


    return grade;   
    }


    public char calculateLetter(char grade){
        char letter='N';
        if(grade>=90)
            letter = 'A';
        else if(grade>=80)
            letter = 'B';
        else if(grade>=70)
            letter = 'C';
        else if(grade>=60)
            letter = 'D';
        else if(grade<50)
            letter = 'F';
        return letter;
    }
    public void actionPerformed(ActionEvent e){
        for(int i=0;i<4;i++){
        scores[i] = Double.parseDouble(jtxtScore[i].getText());
        weights[i] = Double.parseDouble(jtxtWeight[i].getText());

        }
        jtxtScore[4].setText(calculateScore(scores,weights)+"");
        jtxtScore[5].setText(calculateLetter) // what do I have to do here?
    }   

}

`

Upvotes: 0

Views: 96

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

These lines here:

    jtxtScore[4].setText(calculateScore(scores,weights)+"");
    jtxtScore[5].setText(calculateLetter) // what do I have to do here?

You should calculate scores and first put the value returned into an double variable.

i.e.,

double score = calculateScore(scores,weights);

jtxtScore[4].setText(score + "");

You should then use the score value in your calculateLetter method to get your desired char. Note that you might want to change calculateLetter to take an double parameter and not a char as a char parameter makes no sense.


[edited] As a side note, while I applaud your use of arrays since their is simplifying your code greatly, I don't think that you should use them for the final two JTextFields, since these JTextFields don't all hold the same type of information as the others. In your code, I think that you'd be much better off naming these JTextFields, scoreField and letterGradeField, or something similar. Doing this will make your code self-commenting and make it much easier for others and for your future self to understand your code.

Also, I'd use a for loop here:

grade = (score[0]*weight[0]+score[1]*weight[1]+score[2]*weight[2]+score[3]*weight[3]);

rather than hard-code everything. This will allow you to use a different number of scores later if desired.

Upvotes: 1

Related Questions