Introduction to Java using NetBeans

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

 

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.

 

 

 

2D Array Tutorial - Numbers

// Adding elements to the array the long way

int [][] intNumbersArray = new int [5] [4];

// Row 0

intNumbersArray[0] [0] = 1;
intNumbersArray[0] [1] = 3;
intNumbersArray[0] [2] = 5;
intNumbersArray[0] [3] = 7;


// Row 1
intNumbersArray[1] [0] = 2;
intNumbersArray[1] [1] = 4;
intNumbersArray[1] [2] =6;
intNumbersArray[1] [3] =8;

// Row 2
intNumbersArray[2] [0] = 9;
intNumbersArray[2] [1] = 11;
intNumbersArray[2] [2] = 13;
intNumbersArray[2] [3] = 15;

// Row 3
intNumbersArray[3] [0] = 10;
intNumbersArray[3] [1] = 12;
intNumbersArray[3] [2] = 14;
intNumbersArray[3] [3] = 16;

// Row 4
intNumbersArray[4] [0] = 17;
intNumbersArray[4] [1] = 18;
intNumbersArray[4] [2] = 19;
intNumbersArray[4] [3] =20;

// Call the method and pass the array
printarray(intNumbersArray);

 

// Printing the Array

// Getting Input from the User

Your Turn

 

Display a row entered by the user

Display a col entered by the user

Calcualte the sum of all elements in the array

Calculate the sum of each row and each col and display as shown below

 

 

 

 

 

 

 

8.1 - intGradeArrays(Console)

Create a intGradeArrays program that has 5 sutdents and 5 marks for each student

Use the following:

- a 1D strStudentArray

- a 2D intGradeArrays

- a 1D intStudentAverageArray

- a 1D inMarkAverageArray

 


String [ ] strStudentArray= new String [5];
int [ ] [ ] intGradeArray= new int [5][4];

Sample Data

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

// Marks for Student 0

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

// Marks for Student 1

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

// Marks for Student 2

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

// Marks for Student 3

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

// Marks for Student 4

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

 

Use a nested for loop to calculate the test average for each student.

Use a nested for loop to calculate the average for each test

 

Use a nested for loop to rint the Student names, test scores, student average.

Use a single for loop to print the average for each test.

 


Highest strStudentArray Average is strStudentArray - Mark

Lowest strStudentArray Average is strStudentArray - Mark

 

 

 

Part 2 - Cancelled

 

 

Assignment 8.2- Bingo Card (Gui)

Using a 2-D Array Generate a Bingo Card. Since we are not doing any calculations with the card it is easier if you make it a string and convert the random number to a string.


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 i.e. B 13

Indicate if it is on the card or not

Mark the number on the card in some way.

 

Hints

Generate Card Button to Create the Card.

Use a nested for loop with col on the outside and row on the inside to generate each column.

Initialize the random number min value to 1 and max value to 15

To check if the random number has already been generated use if and while loop
(Leave this to the End, Now Above and Beyond)

Outside the row loop but inside the col loop change the min and max values for the random number by 15.

 

 

printCard method

You will need to assign the value of the bingocard to the appropriate text box using setText. Note: We cannot do this in a loop yet so you will need to do it individually.

i.e. textBox00.setText (strBingoArray[0][0]);

callNumber Button

Generate a random number from 1 to 75. Check if it has already been called. If so generate a new number.

Store the called number in an Array List

See if the number is in the list

Print the Array List

Change the number in the bingo array to something else to show it has been called and re-print

 

 

Cancelled - Determine if you have BINGO

 

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.3 - intGradeArrays (Gui)

Cancelled

Create a strStudentArray intGradeArrays Application that simulates a intGradeArray book for a class with 15 strStudentArrays who each have four test scores.

 

This program is similar to the strStudentArray 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 strStudentArrays.

Requirements

  • Create an input/output screen as shown below.
  • Use a two-dimensional array to store strStudentArray data.
  • Create four buttons: Add, List, strStudentArray Average and Course Average.
  • List Button
    • Show all of the intGradeArrays for all of the strStudentArrays that have been stored in the array.
    • Your program should be able to handle up to 15 strStudentArrays.
  • Add Button
    • This method should be able to allow the user to enter strStudentArray 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 strStudentArray information stored.
  • strStudentArray Average Button
    • This method should be able to allow the user to enter a strStudentArray's first and last name, and then press the Average button to have the strStudentArray's average on the four tests calculated and displayed.

 

 

Rubric