truemuck
04-09-2010, 03:23 PM
Having some trouble getting this assignment to work correctly
Here's what we're trying to do:
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 I'm having is getting the average to work and display correctly.
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 numberOfGrades = 0;
static int total = 0;
static DecimalFormat twoDigits = new DecimalFormat ("##0.00");
static int[] grades = new int[50];
public Container createContentPane()
{
JPanel northPanel = new JPanel();
northPanel.add(title);
JPanel centerPanel = new JPanel();
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500,200));
centerPanel.add(scrollPane);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel, BorderLayout.NORTH);
c.add(centerPanel, BorderLayout.CENTER);
return c;
}
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
doc.remove(0, doc.getLength());
doc.insertString(0,"Grades\n", textPane.getStyle(""));
for(int i= 0; i < numberOfGrades; i++)
{
doc.insertString(doc.getLength(), String.valueOf(grades[i]) + "\n",null);
}
}
catch(BadLocationException ble)
{
System.err.println("Couldnt insert grades.");
}
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])
{
swap(grades, element, 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)
{
grades[numberOfGrades] = integerInput;
total += integerInput;
numberOfGrades++;
}
integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));
}
sort(grades, numberOfGrades);
f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}
}
Any help you could give would be most helpful, as I need to get this in by midnight, thanks!
Here's what we're trying to do:
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 I'm having is getting the average to work and display correctly.
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 numberOfGrades = 0;
static int total = 0;
static DecimalFormat twoDigits = new DecimalFormat ("##0.00");
static int[] grades = new int[50];
public Container createContentPane()
{
JPanel northPanel = new JPanel();
northPanel.add(title);
JPanel centerPanel = new JPanel();
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500,200));
centerPanel.add(scrollPane);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel, BorderLayout.NORTH);
c.add(centerPanel, BorderLayout.CENTER);
return c;
}
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
doc.remove(0, doc.getLength());
doc.insertString(0,"Grades\n", textPane.getStyle(""));
for(int i= 0; i < numberOfGrades; i++)
{
doc.insertString(doc.getLength(), String.valueOf(grades[i]) + "\n",null);
}
}
catch(BadLocationException ble)
{
System.err.println("Couldnt insert grades.");
}
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])
{
swap(grades, element, 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)
{
grades[numberOfGrades] = integerInput;
total += integerInput;
numberOfGrades++;
}
integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));
}
sort(grades, numberOfGrades);
f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}
}
Any help you could give would be most helpful, as I need to get this in by midnight, thanks!