2

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".

  1. "Sort and List Student's Scores" button -- display the sorted scores for each student.
  2. "Display Average Score for Each Student"-- display the average score for each student.
  3. "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);
        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    *"Program Requirement: Write a Java Applet.."* I should give a spanking to whoever decided this should be an applet. Is it homework or punishment? It might pay to develop it as an hybrid application/applet. – Andrew Thompson Nov 21 '12 at 02:29
  • E.G. of hybrid can be seen [here](http://stackoverflow.com/a/10839293/418556). – Andrew Thompson Nov 21 '12 at 02:34
  • 1
    *"Rows being number of students, and columns being number of courses."* Will each student have the same number of courses? If so, a `JTable` would be a good GUI component to display the data. See [this question](http://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image) for images of a table (and the code that made them). – Andrew Thompson Nov 21 '12 at 03:00

1 Answers1

0

You should use brackets to include statements between loops to multiply it.

Secondly use another variable name for column indexer if you don't some values will be null.

   for (int  column = 0; column < table_Columns_numbers  ; column++)

   for (int  row = 0; row < table_rows_numbers; row++)
   {

   //Statement 1   ask user to enter the value 

    JOptionPane.showInputDialog("Please enter the value"); 

   //Statement 2   assign the value to your multidimensional array 

    array[row][column] = the value entered


    }

Column number in your code is the number of course number

row number is the number of the student number.

So you use for loop to enter the score of student number index+1 in the course number of col .

And in each time you assign the score number you entered to table cell

for (int  col = 0; col < courseNumber; col++)

      for (int  index = 0; index < studentNumber; index++)
      {
      scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1));
      scores = Double.parseDouble(scoresString);

      //This is statement for explanation  

      System.out.println("scoresString  for student no \t"+(index+1)+"\t and course number \t"+ (col+1)+"\t"+scoresString);

     //Assigning value to   tableStudentScores[index][col] cell.

     tableStudentScores[index][col]=String.valueOf( scores);


    }

    JOptionPane.showMessageDialog( null ,Arrays.deepToString(tableStudentScores));
PHPFan
  • 756
  • 3
  • 12
  • 46