# Need some help with my Assignment

My prof gave us this assigment:

Write a Swing program that declares an empty array of grades with a maximum length of 50. Implement a JOptionPane input box within a while loop to allow the user to enter grades. When the user enters the sentinel value of -1, that will signal the end of the data input loop.

After the grades are entered, a content pane should display the grades sorted from lowest to highest. Write a loop that goes through the array looking for elements that are greater than zero (0). Keep a running count of those items, and also accumulate them into a grand total. Divide the grand total by the number of grades entered to find an average, and display the average at the end of the sorted list of grades. Use the DecimalFormat method to display the average to 2 decimal places.

The problem is I can’t get the averages to work or display properly. What am I missing/doing wrong?

Here’s what I have so far:

``````import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;

public class Average extends JFrame
{
static JLabel title = new JLabel("Grades");
static JTextPane textPane = new JTextPane();
static int total = 0;
static DecimalFormat twoDigits = new DecimalFormat ("##0.00");

static int[] grades = new int[50];

public Container createContentPane()
{
JPanel northPanel = new JPanel();

JPanel centerPanel = new  JPanel();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500,200));

Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));

return c;

}

{
Document doc = textPane.getDocument();
try
{
doc.remove(0, doc.getLength());

",  textPane.getStyle(""));

for(int i= 0; i < numberOfGrades; i++)
{
",null);
}

}

{
}

return textPane;
}

public static void sort(int tempArray[], int length)
{
for (int pass = 1; pass < length; pass++)
{
for (int element = 0; element < length - 1; element++)
if (tempArray[element] > tempArray[element + 1])
{
}
}

}

public static void swap(int swapArray[], int first, int second)
{
Integer hold;
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}

public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Average f = new Average();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int integerInput = Integer.parseInt(
JOptionPane.showInputDialog(
null, "Please enter the grade (0 - 100) or -1 to exit"));
while(integerInput != -1)
{
if (integerInput > 0)
{
total += integerInput;
}
integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));

}

f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}

}

``````

Thanks!

I’m not seeing where you are trying to find the average, much less display it…

I figured, I was missing something like that, but wasn’t sure where to include it. Any code to help me out would be most helpful.

Well, you’ll get no code from me, sorry, not for an assignment.

After you display the grades, you need a line that displays the average, in this line you’ll call ‘findAverage( int )’

In findAverage, you’ll total up the grades and keep count of how many grades were entered (though, it may be best to do this elsewhere, wink, wink)

Using the total, the count and your DecimalFormat object, you’ll return a ready to display String to the caller, which will display your answer.

haha! Truemuck you are in my same class. I remember this assignment.

I was given an almost identical assignment too lol

Where you go to school.