0% found this document useful (0 votes)
2 views

Assignment-3

The document outlines a laboratory exercise focused on array manipulation and methods in Java, including objectives such as storing and reading data in arrays, defining and invoking methods, and utilizing the java.util.Arrays class. It provides syntax for declaring and creating arrays, explains the concept of pass-by-value in relation to arrays, and includes several programming tasks that involve user input, array operations, and method overloading. The tasks require students to implement various functionalities such as finding minimum values, shuffling arrays, and using built-in array methods for sorting and searching.

Uploaded by

rahat hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment-3

The document outlines a laboratory exercise focused on array manipulation and methods in Java, including objectives such as storing and reading data in arrays, defining and invoking methods, and utilizing the java.util.Arrays class. It provides syntax for declaring and creating arrays, explains the concept of pass-by-value in relation to arrays, and includes several programming tasks that involve user input, array operations, and method overloading. The tasks require students to implement various functionalities such as finding minimum values, shuffling arrays, and using built-in array methods for sorting and searching.

Uploaded by

rahat hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Laboratory -3: Array Manipulation and Methods

Objectives:
O[1]. To learn how to store data into Array and read data from Array.
O[2]. To learn how to define a method.
O[3]. To develop and invoke method with array arguments and return values.
O[4]. To use the methods in the java.util.Arrays class.

Array stores a fixed-size sequential collection of elements of the same type. We can store 100
numbers into an array and access them through a single array variable.To use an array in a
program, you must declare a variable to reference the array and specify the array’s element type.
Here is the syntax for declaring an array variable:
elementType[] arrayRefVar;
or
elementType arrayRefVar[];

The elementType can be any data type, and all elements in the array will have the same data type.
For example, the following code declares a variable myList that references an array of double
elements.
double[] myList;
or
double myList[];

Unlike declarations for primitive data type variables, the declaration of an array variable does not
allocate any space in memory for the array. It creates only a storage location for the reference to
an array. If a variable does not contain a reference to an array, the value of the variable is null.
You cannot assign elements to an array unless it has already been created. After an array variable
is declared, you can create an array by using the new operator and assign its reference to the
variable with the following syntax:
arrayRefVar = new elementType[arraySize];

This statement does two things: (1) it creates an array using new elementType[arraySize] and (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
elementType[] arrayRefVar = new elementType[arraySize];
or
elementType arrayRefVar[] = new elementType[arraySize];
Here is an example of such a statement:
double[] myList = new double[10];
Task1: Write a program to prompts the user to enter ten numbers, find and display the minimum
value from them.
Task2: Modify the program in Task1 to use foreach loop.
Task3: Modify the program in task1 by writing a method that finds the smallest element in an
array of double values using the following header:
public static double min(double[] array);

Java uses pass-by-value to pass arguments to a method. There are important differences between
passing the values of variables of primitive data types and passing arrays.
• For an argument of a primitive type, the argument’s value is passed.
• For an argument of an array type, the value of the argument is a reference to an array; this
reference value is passed to the method.
Semantically, it can be best described as pass-by-sharing, that is, the array in the method is the
same as the array being passed. Thus, if you change the array in the method, you will see the
change outside the method.

Arrays are in fact objects, so a reference is passed (the reference itself is passed by value, confused
yet?). Quick example:

// assuming you allocated the list


public void addItem(Integer[] list, int item) {
list[1] = item;
}
You will see the changes to the list from the calling code. However you can't change the reference
itself, since it's passed by value:

// assuming you allocated the list


public void changeArray(Integer[] list) {
list = null;
}
If you pass a non-null list, it won't be null by the time the method returns.
Task4: Write a program to define main method and declare a local int array then
• calls a method read_array () which prompts the size of an array from the user. It also
promts the user to enter the size number of elements and stores them into the array.
Returns the array.
• calls another method random_shuffling() to randomly reorder the elements in the array.
Returns the array.
• calls another method to print_array() which prints all the elements of the array.

The java.util.Arrays class contains useful methods for common array operations such as sorting
and searching. The java.util.Arrays class contains various static methods for sorting and searching
arrays, comparing arrays, filling array elements, and returning a string representation of the array.

Task5: Modify the program in Task4 to print all the elements of the array using following
statement:
System.out.println(java.util.Arrays.toString(list));

Task6: Use the following Array class methods in your program


• java.util.Arrays.sort(list);
• java.util.Arrays.binarySearch(list,11); -> key is not @Array return –(insertionIndex+1)
• java.util.Arrays.equals(list1,list2);// true or false

Overloading methods enable you to define the methods with the same name as long as their
parameter lists are different.

Task7: Implement overloading methods with arrays using only two methods -read_array() and
print_array().

You might also like