Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This applet is to find the average of numbers ...It compiles but when I run it..

ID: 3539608 • Letter: T

Question

This applet is to find the average of numbers ...It compiles but when I run it...It gives me the average = 0.0?


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class avg extends JApplet implements ActionListener

{

JLabel num1lab = new JLabel(" num 1");

JTextField num1field = new JTextField(7);

JLabel num2lab = new JLabel("num 2");

JTextField num2field = new JTextField(7);

JLabel num3lab = new JLabel("num 3");

JTextField num3field = new JTextField(7);

JLabel num4lab = new JLabel("num 4");

JTextField num4field = new JTextField(7);

JLabel num5lab = new JLabel("num 5");

JTextField num5field = new JTextField(7);

JLabel answerlab = new JLabel("Average");

JTextField answerfield = new JTextField(9);

JButton calc = new JButton("Find Average");

JButton nbut = new JButton("Reset");

  Container c;

FlowLayout flow = new FlowLayout();

public void init()

{

// NO PROBLEM WITH THIS PART

}

public void actionPerformed(ActionEvent e)

{

// THE PROBLEM IS HERE AND I CAN'T FIGURE IT OUT

int[] b = new int[5];

int sum = 0, i;

double avg;

for (i = 0; i < b.length; i++)

sum += b[i];

avg = (double) sum / b.length;

}

}

Explanation / Answer

This is because you are not copying the numbers from your jTextFields into your Array. You need to copy the numbers from the jtextFields into your array and then the code will work fine.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote