Ok, so I'm having a bit of trouble with understanding how swing interfaces work. Here's the problem. Write a Swing program that declares an empty array of grades with a maxium of 10. Implement a JOptionPane input box within a while loop to allow the user to enter grades using a -1 sentinel value.
So here is the code
/*
Chapter 7: Programming Assignment
Programmer: Your Name
Date:
Filename: Averages.java
Purpose: This program creates a Swing interface for averaging grades
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
public class Averages extends JFrame
{
//construct components
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");
//construct array
static int[] grades = new int[10];
//create the content pane
public Container createContentPane()
{
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(title);
//create the JTextPane and center panel
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);
//create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
return c;
}
//method to add new text to the JTextPane - use the Document class
public static JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//insert title
doc.insertString(0,"Grades\n",textPane.getStyle("large"));
//insert grades and averages; use the for loop; to calculate average divide total by number of grades
for(int j = 0; j<grades.length; j++)
{
doc.insertString(doc.getLength(), grades[j] + "\n",textPane.getStyle("large"));
}
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert text");
}
return textPane;
}
//method to sort arrays
public static void sort(int tempArray[], int length)
{
//keep in mind that you are sorting an integer array, not a string array
...........
}
//method to swap two elements of an array
public static void swap(int swapArray[], int first, int second)
{ .............
}
//main method executes at run time
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Averages f = new Averages();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//accept the first grade
int integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));
//write the while loop to accept more grades and store them in the array; don't forget to keep count of the number of grades;
//also, calculate the total score
while (integerInput != -1)
{
}
//call sort method
//call to create the content pane and set attributes
f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}
}
Don't mind some of the methods that are missing parts. I'm still working on them. The problem is this "//write the while loop to accept more grades and store them in the array; don't forget to keep count of the number of grades;
//also, calculate the total score
while (integerInput != -1)
{
}"
I tried putting in the JOPtionPane box with a for loop to manipulate the count, but that didn't work, so what am I suppose to do to manipulate the while loop?
Thank you for any help.