LAB 6
Monday, November 7, 2022 8:36 AM
Legend: code To clear the screen:
normal text [Link]("\033[H\033[2J");
CALLING OF FUNCTIONS
To make functions, you use;
public static void 'function name'() {
}
To make the variables and utility declarations applicable to all of the
functions or to make global declarations, you use;
public static 'datatype' 'variable name';
for scanner;
public static Scanner sc = new Scanner([Link]);
To call out the functions or methods, you use;
'function name'();
ARRAY
Array – stores multiple values in a single variable, instead of multiple
variables for each value
DECLARING ARRAYS
To declare or create arrays, use;
Long method
datatype[] 'array name'= new datatype['number of
values'];
'array name[0] = value;
'array name[1] = value;
'array name[2] = value;
Short method
Datatype 'array name' = {'values'};
ONE DIMENTIONAL ARRAY
Short method
int[] b = {10,20,30};
[Link]("One Dimensional Array
Elements");
[Link](a[0] + ", " + a[1] + ", "
+ a[2] + ".");
Output:
ARRAY FUNCTIONS
[Link]() method
FUNDAMENTALS OF PROGRAMMING Page 1
[Link]() method
This displays all of the array values without needing to specify the array
indexes.
Use the syntax;
[Link]('Array name');
Example:
int[] n = {10, 20, 30, 40, 50};
[Link]("The arrays are: " +
[Link](n));
How to get rid of the brackets when displaying
[Link]()
Use the syntax;
'Array name'.length to loop through every value of an array.
Example:
For (a = 0; a < [Link]; a++){
[Link](n[a]);
}
TWO DIMENTIONAL ARRAYS
are arrays of arrays
2D ARRAY FUNCTION/S
.deepToString method
Is used for multidimensional Java arrays to be converted in to Strings.
Output:
int[] n = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
FUNDAMENTALS OF PROGRAMMING Page 2
int[] n = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
Random rn = new Random([Link]);
[Link](n[[Link]([Link])]);
int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int res = 0;
for (int a = 0; a < [Link] + 1; a++){
res += n[a];
[Link]("\033[H\033[2J");
[Link](res);
}
FUNDAMENTALS OF PROGRAMMING Page 3