OOP Lab Manual 4
OOP Lab Manual 4
OBJECTIVES
Arrays in Java
An array is a group of like-typed variables that are referred to by a common name. Arrays of
any type can be created and may have one or more dimensions. A specific element in an array
is accessed by its index. Arrays offer a convenient means of grouping related information.
One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first
must create an array variable of the desired type. The general form of a one-dimensional array
declaration is:
type var-name[ ];
Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look
and act like regular multidimensional arrays. However, as you will see, there are a couple
of subtle differences. To declare a multidimensional array variable, specify each additional
index using another set of square brackets. For example, the following declares a two
dimensional array variable called twoD.
This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as
an array of arrays of int.
1
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
int al[] = new int[3];
2
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Task 1: Write a java program to find smallest number from the integer members of an array.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task 2: Write a java code that prompts user to enter a number and search that number in the
array. If the number is found in array the program should return its location (index number)
otherwise it should print the message that “the given number not found”.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
3
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Task 3: Write a java code that computes and prints the sum of all the elements of an array.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task 4: Write a java code that creates an array to store marks of 10 students. The program should
then display maximum, minimum and average marks of students.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task 5: Write a java code that prints product of two command line arguments.
______________________________________________________________________________
4
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task 6: Write a java code that computes and displays factorial of a number that is entered (input)
by the user.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________