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

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)
16 views

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
You are on page 1/ 3

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy