Assignment-3
Assignment-3
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:
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));
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().