Introduction to Java using NetBeans

Section 8 : 2-D Arrays (a Matrix) and Creating your own Methods


So far, we have restricted our discussion to one-dimensional arrays, which are similar to a single row or single column in a spreadsheet.

Sometimes, though, you will want to create a data structure that is equivalent to a spreadsheet with both rows and columns. When this is the case, you need to add a second dimension.

You can use Java to create a two-dimensional array by creating an array within an array. The elements in two-dimensional arrays can be thought of as being organized into rows and columns. The rows are the first dimension of the array and the columns the second.


Declare and Initialize a Two-Dimensional Array





Display the Array Elements

Nested for loops are often used to access the elements of a two-dimensional array because one loop counter can be used to indicate the row number and the second counter can be used to indicate the column number.




8.0 Practice Assignment - Numbers


// Printing the Array

// Getting Input from the User

Your Turn


Using Methods

This time we will pass the array to the method.

Add the following to your program.

Allow the user to input their own elements.

Print the elements of the array

Print a Row indicated by the user

Print a Col indicated by the user

Modify a Row indicated by the user

Modify a Col indicated by the user






8.1 - Grades(Console)

Create a Grades program that uses a student array and a 2D grades array. Set the number of students to 5 and number of Marks to 4.

For this assignment make the arrays global and then you do not need to pass them.


String [ ] student= new String [5];
int [ ] [ ] grade= new int [5][4];

Sample Data

// Student Names
student[0]= "Chris";
student[1]= "Cory";
student[2]= "Nick";
student[3]= "Zoey";
student[4]= "Jaxon";

// Marks for Test 1

grade [0][0]=94;
grade [0][1]=80;
grade [0][2]=66;
grade [0][3]=82;

// Marks for Test 2

grade [1][0]=32;
grade [1][1]=88;
grade [1][2]=96;
grade [1][3]=55;

// Marks for Test 3

grade [2][0]=94;
grade [2][1]=84;
grade [2][2]=73;
grade [2][3]=99;

// Marks for Test 4

grade [3][0]=34;
grade [3][1]=55;
grade [3][2]=44;
grade [3][3]=76;

// Marks for Test 5

grade [4][0]=94;
grade [4][1]=54;
grade [4][2]=98;
grade [4][3]=82;


1. Print the Array using Nested For Loops

2. Calculate the Average Mark of Each student, the Average mark on each test, and the overall Class Average. You must use nested for loops to accomplish this.

Indicate the student with the Highest Average, Lowest Average.


Highest Student Average is Student - Mark

Lowest Student Average is Student - Mark

Part 2 - Cancelled


Return Values From Methods
(Not required but because you asked)

Example 1 - Returning an Integer Value from a Method

public class Program {

public static void main(String[] args) {

// Call method Compute an assign the return value to result

int result = Compute(10, 3); //Call method and pass parameters
System.out.println(result); //Result will be 30

// A method that returns an integer value

public static int Compute(int height, int width) {
// Return the result to the caller

int answer = height * width;
return answer; //return answer to result


Example 2 - Returning an Array from a method

public static void main(String[] args)

double[] a; // "a" can contain a ref. to an array of double
a = returnArray(); // "a" can receive the return value !!!
System.out.println("Array AFTER calling returnArray:");

for (int i = 0; i < a.length; i++)
System.out.println( a[i] );


public static double[] returnArray( )

double[] x;
x = new double[3]; // Create an array of 3 elements
x[0] = 2.3;
x[1] = 3.4;
x[2] = 4.5;

return( x ); // Return



Assignment 8.2- Bingo Card (Gui)

Using a 2-D Array Generate a Bingo Card.
You must use nested loops to Generate your Card.
Display the card in a GUI.
The range of printed numbers that can appear on the card is normally restricted by column, with the 'B' column only containing numbers between 1 and 15 inclusive, the 'I' column containing only 16 through 30, 'N' containing 31 through 45, 'G' containing 46 through 60, and 'O' containing 61 through 75.
Include a Generate New Card, an exit button, a call button and a BINGO button.

Make sure that there are no duplicate numbers in the card.

Randomly Generate a number. Display each number that is called.

. If it is on the card, mark it.




Assignment 8.3 - Grades (Gui)


Create a student Grades Application that simulates a grade book for a class with 15 students who each have four test scores.


This program is similar to the student Records program that you created earlier

The difference here is that you are now using a two-dimensional array which will allow you to store more information about each of the students.


  • Create an input/output screen as shown below.
  • Use a two-dimensional array to store student data.
  • Create four buttons: Add, List, student Average and Course Average.
  • List Button
    • Show all of the grades for all of the students that have been stored in the array.
    • Your program should be able to handle up to 15 students.
  • Add Button
    • This method should be able to allow the user to enter student data, and then press the Add button to have the data added to the array.
    • The user should be able to verify that the data was added to the array by pressing the List button.
  • Course Average Button
    • This method should calculate the Course Averages for each of the courses.
    • The program should then print the average of each course and all the student information stored.
  • student Average Button
    • This method should be able to allow the user to enter a student's first and last name, and then press the Average button to have the student's average on the four tests calculated and displayed.