0% found this document useful (0 votes)
19 views5 pages

OOP Lab Manual 4

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

OOP Lab Manual 4

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH

OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Name: _____________________________ Roll No: __________________________________

Signature of Lab Tutor: ____________________ Date: ________________________________

LAB 4: ARRAYS AND CONTROL STATEMENTS REVISITED

OBJECTIVES

 To practice different tasks using arrays and control statements in Java

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.

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

This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as
an array of arrays of int.

Alternative Array Declaration Syntax


There is a second form that may be used to declare an array:
type[ ] var-name;
Here, the square brackets follow the type specifier, and not the name of the array variable.

For example, the following two declarations are equivalent:

1
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
int al[] = new int[3];

int[] a2 = new int[3];

The following declarations are also equivalent:

char twod1[][] = new char[3][4];

char[][] twod2 = new char[3][4];


This alternative declaration form offers convenience when declaring several arrays at the
same time. For example,

int[] nums, nums2, nums3; // create three arrays

creates three array variables of type int. It is the same as writing

int nums[], nums2[], nums3[]; // create three arrays


The alternative declaration form is also useful when specifying an array as a return type for
a method.
Arrays are implemented as objects in Java. Because of this, there is a special array attribute that
you will want to take advantage of. Specifically, the size of an array—that is, the number of
elements that an array can hold—is found in its length instance variable. All arrays have this
variable, and it will always hold the size of the array.
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
}
}
This program displays the following output:
length of a1 is 10
length of a2 is 8
length of a3 is 4
As you can see, the size of each array is displayed. Keep in mind that the value of length
has nothing to do with the number of elements that are actually in use. It only reflects the
number of elements that the array is designed to hold.

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.

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

You might also like