I will first list what the assignment was. Then I will list my problems. Then lastly I will list my code so far, mind you, its far from complete but I can't continue until I get past this step.
ASSIGNMENT:
The purpose of this Programming Assignment is to give you a hands-on experience of using Java Multidimensional Array, Event-Driven Programming using Button, and the application of AWT and Swing using JFrame and JOptionPane Classes.
Program Requirement:
Write a Java Applet using init() for the course score calculations with the following requirements:
Use JoptionPane allowing the instructor to enter the number of students, G numbers (Student's names are not required.), the number of course, course IDs (CIT230, CIT352, etc.) and a score for each course. The course scores must be stored in a two-dimensional array. An example of the two-dimensional array would be:
and then a table is displayed. Keep in mind we are not using JTables.
Add buttons and use event-driven programming technique to allow users to click on three buttons, "Sort and List Student's Scores", "Display Average Score for Each Student", and "Exit".
- "Sort and List Student's Scores" button -- display the sorted scores for each student.
- "Display Average Score for Each Student"-- display the average score for each student.
- "Exit" -- Create a Yes- or No Dialog Box for users to exit the program.
MY PROBLEMS: I have no idea how to create this multidimensional array who's dimensions are based on what the user enters. Rows being number of students, and columns being number of courses. When I had my program somewhat working, the JOptionPane would display [[null][null]]. I know how to create a two dimensional array not in a JOptionPane, and with previously set dimensions, but this is throwing me way off. I'm not asking for direct answer code, I just need an example or some guidance of the correct way to write this.
MY CODE:
(I have some things commented out that I've tried and didn't work. I've tried a lot of different ways but I just delete them.)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class ScoreApplet extends JApplet implements ActionListener
{
private JLabel mainMenu;
private JButton sortScores;
private JButton displayAverage;
private JButton exit;
private String[][] tableStudentScores;
private int studentNumber;
private int courseNumber;
private String studentNumberString;
private String courseNumberString;
private String scoresString;
private double scores;
public void init()
{
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new FlowLayout());
mainMenu = new JLabel("What would you like to do?");
sortScores = new JButton("Sort And List Student's Scores");
displayAverage = new JButton("Display Average Score for Each Student");
exit = new JButton ("Exit");
contentPane.add(mainMenu);
contentPane.add(sortScores);
sortScores.addActionListener(this);
contentPane.add(displayAverage);
displayAverage.addActionListener(this);
contentPane.add(exit);
exit.addActionListener(this);
mainMenu.setVisible(false);
sortScores.setVisible(false);
displayAverage.setVisible(false);
exit.setVisible(false);
studentNumberString = JOptionPane.showInputDialog("Please enter the number of students:");
studentNumber = Integer.parseInt(studentNumberString);
tableStudentScores = new String[studentNumber][];
courseNumberString = JOptionPane.showInputDialog("Please enter the number of courses:");
courseNumber = Integer.parseInt(courseNumberString);
tableStudentScores = new String[studentNumber][courseNumber];
for (int index = 0; index < studentNumber; index++)
for (index = 0; index < courseNumber; index++)
scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1));
scores = Double.parseDouble(scoresString);
/*
for (int index = 0; index <= (courseNumber - 1); index++)
{
scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1) );
scores = Double.parseDouble(scoresString);
}
*/
JOptionPane.showMessageDialog(null, Arrays.deepToString(tableStudentScores));
mainMenu.setVisible(true);
sortScores.setVisible(true);
displayAverage.setVisible(true);
exit.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Sort And List Student's Scores"))
{
/*
}
for (int index = 0; index < anArray.length - 1; index++)
{ // Place the correct value in anArray[index]
int indexOfNextSmallest = getIndexOfSmallest(index, anArray);
interchange(index, indexOfNextSmallest, anArray);
*/
}
else if (e.getActionCommand().equals("Display Average Score for Each Student"))
{
}
else if (e.getActionCommand().equals("Exit"));
{
int answer =
JOptionPane.showConfirmDialog(null, "End program?",
"Click Yes or No:", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
}