LAB 6
Monday, November 7, 2022 8:36 AM
Legend: code To clear the screen:
normal text System.out.println("\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(System.in);
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};
System.out.println("One Dimensional Array
Elements");
System.out.println(a[0] + ", " + a[1] + ", "
+ a[2] + ".");
Output:
ARRAY FUNCTIONS
to.String() method
FUNDAMENTALS OF PROGRAMMING Page 1
to.String() method
This displays all of the array values without needing to specify the array
indexes.
Use the syntax;
Arrays.toString('Array name');
Example:
int[] n = {10, 20, 30, 40, 50};
System.out.println("The arrays are: " +
Arrays.toString(n));
How to get rid of the brackets when displaying
to.String()
Use the syntax;
'Array name'.length to loop through every value of an array.
Example:
For (a = 0; a < n.length; a++){
System.out.print(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(n.length);
System.out.println(n[rn.nextInt(n.length)]);
int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int res = 0;
for (int a = 0; a < n.length + 1; a++){
res += n[a];
System.out.println("\033[H\033[2J");
System.out.println(res);
}
FUNDAMENTALS OF PROGRAMMING Page 3