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

Fundamentals of Programming - Lab 6

Making Functions/Methods, calling function/methods, Arrays, Declaring Arrays, One Dimensional Arrays, Two Dimensional Arrays/ 2D Arrays, Array Methods

Uploaded by

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

Fundamentals of Programming - Lab 6

Making Functions/Methods, calling function/methods, Arrays, Declaring Arrays, One Dimensional Arrays, Two Dimensional Arrays/ 2D Arrays, Array Methods

Uploaded by

uriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like