OOP – ASSIGNMENT VIII

Lab Manual Download : Write a Program in Java to add two matrices. The objective of this assignment is to learn Arrays in Java

ASSIGNMENT NO 8

 Title: Demonstrate use of arrays in Java.

Objectives: To learn creation and use of arrays in Java.

Problem Statement:  Write a Program in Java to add two matrices.

Outcomes: Students will be able to demonstrate use of arrays in Java programs.

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

Theory:

An array is a collection of similar data types. Array is a container object that hold values of homogeneous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.

An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.

Declaring Array Variables: To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable −

Syntax : dataType[] arrayRefVar;   // preferred way.

or

dataType arrayRefVar[];  // works but not preferred way.

Creating Arrays: You can create an array by using the new operator with the following syntax −

Syntax : arrayRefVar = new dataType[arraySize];

The above statement does two things −

  1. It creates an array using new dataType[arraySize].
  2. It assigns the reference of the newly created array to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below −

dataType[] arrayRefVar = new dataType[arraySize];

Alternatively you can create arrays as follows −

dataType[] arrayRefVar = {value0, value1, …, valuek};

The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1.

Processing Arrays : When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known.

Jagged Array : It is a new feature supported by Java. In Jagged arrays, each row, in a two-dimensional array, may contain different lengths. Let us design a two-dimensional array with 4 rows where the first row contains 5 elements, the second row with 3 elements, the third row with 2 elements and the fourth row with 4 elements and etc

public class arrayExample2 {
            public static void main(String[] args) {
            //Declare, Initialize, and fill the array
            int[][] array = {{0,1,2,3,4},{0,1,2},{0,1,2,3},{0,1},{0},{0},
            {0,1,2,3,4,5},{0},{0,1,2,3},{0,1,2,3}};
//Test the array
for (int i = 0; i<array.length; i++) {
            for (int j = 0; j<array[i].length; j++) {
            System.out.print(array[i][j] + " ");
            }
            System.out.println();
            }
            }
        }

The output would be something like this:

0 1 2 3 4
0 1 2
0 1 2 3
0 1
0
0
0 1 2 3 4 5
0
0 1 2 3
0 1 2 3

Accessing array element: As mention ealier array index starts from 0. To access nth element of an array.

Syntax : arrayname[n-1];

 Example : To access 4th element of a given array

int[ ] arr = {10,20,30,40};

System.out.println(“Element at 4th place” + arr[3]);

The above code will print the 4th element of array arr on console.

foreach or enhanced for loop:  J2SE 5 introduces special type of for loop called foreach loop to access elements of array. Using foreach loop you can access complete array sequentially without using index of array. Let us see an example of foreach loop.

class Test
{
public static void main(String[] args)
  {
    int[] arr = {10, 20, 30, 40};
            for(int x : arr)
            {
                        System.out.println(x);
            }
   }
}

Output :

10
20
30
40

Conclusion : In this assignment, we have studied arrays in Java and demonstrated their use in program.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty
 

 

Program

class MatrixAdd {
 public static void main(String args[])
 {
 int mat1[][]=new int[3][3];
 int mat2[][]=new int[3][3];
 System.out.println("Enter values of first 3x3 matrix");
 for(int i=0;i<3;i++)
 for(int j=0;j<3;j++)
 mat1[i][j]=Integer.parseInt(System.console().readLine());
 System.out.println("Enter values of second 3x3 matrix");
 for(int i=0;i<3;i++)
 for(int j=0;j<3;j++)
 mat2[i][j]=Integer.parseInt(System.console().readLine());
 System.out.println("*****Matrix Addition*****");
 for(int i=0;i<3;i++)
 {
 for(int j=0;j<3;j++)
 System.out.print(mat1[i][j]+" ");
 System.out.println();
 }
 
 System.out.println(" + ");
 for(int i=0;i<3;i++)
 {
 for(int j=0;j<3;j++)
 System.out.print(mat2[i][j]+" ");
 System.out.println();
 }
 
 System.out.println(" = ");
 for(int i=0;i<3;i++)
 {
 for(int j=0;j<3;j++)
 System.out.print(mat1[i][j]+mat2[i][j]+" ");
 System.out.println();
 }
 }
 
}

OUTPUT:

ass-iv-op

Leave a Reply

Your email address will not be published. Required fields are marked *