0% found this document useful (0 votes)
8 views7 pages

Ariel

The document contains code for several Java programs that demonstrate working with arrays and methods. The code samples show how to declare and populate arrays, loop through arrays, pass arrays to methods, and call methods from main.

Uploaded by

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

Ariel

The document contains code for several Java programs that demonstrate working with arrays and methods. The code samples show how to declare and populate arrays, loop through arrays, pass arrays to methods, and call methods from main.

Uploaded by

Marvin Talahiban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

public class ArielLoopCall

{
public static void main(String[] args)
{
System.out.println("Hello from the main method.");
for (int i = 0; i < 5; i ++)
displayMessage();
System.out.println("Back in the main method.");
}

public static void displayMessage()


{
System.out.println("Hello from the displayMessage method.");
}
}
OUTPUT

import java.util.Scanner;
public class Ariel_ExerArray1
{
public static void main(String[] args)
{
int[] age = { 12, 4, 5, 2, 5 };
System.out.println("Displaying array element");
for (int index = 0; index < age.length; index++)
{
System.out.println(age[index]);
}
}
}
OUTPUT

import java.util.Scanner;
public class Ariel_ExerArray2
{
public static void main(String[] args)
{
int[] age = { 5, 2, 5, 4, 12 };
System.out.println("Accessing array element");
System.out.println("Fifth Element: " + age[0]);
System.out.println("Fourth Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Second Element: " + age[3]);
System.out.println("First Element: " + age[4]);
}
}
OUTPUT
import java.util.Scanner;
public class Ariel_ExerArray3
{
public static void main(String[] args)
{
int[] numbers = { 2, -9, 0, 5, 12, -25, 22, 9, 8, 12 };
for (int index = 0; index < numbers.length; index++)
if (numbers[index] >=0) {
System.out.println(numbers[index]);
}
}
}
OUTPUT

import java.util.Scanner;
public class Ariel_ExerArray4
{
public static void main(String[] args)
{
int[] age = {12 , 4, 5, 2, 5};
for (int index = age.length -1; index >= 0; index--) {
System.out.println(age[index] + " ");
}
}
}
OUTPUT

public class ArielDeepAndDeeper


{
public static void main(String[] args)
{
System.out.println("I am starting in main.");
deep();
System.out.println("Now I am back in main.");
}
public static void deep()
{
System.out.println("I am now in deep.");
deep();
System.out.println("Now I am back in deep.");
}
public static void deeper()
{
System.out.println("I am now in deeper.");
}
}
OUTPUT

public class Ariel_MainMethod


{
public static void main(String[] args)
{
System.out.println("Sum of numbers from 1 to 10 is:" + sum(1,10));
}
public static int sum(int x, int y) {
int result = 0;
for (int i = x; i <=y; i++)
result += i;
return result;
}
}
OUTPUT
import java.util.ArrayList;
import java.util.Collections;
public class ArielSwap_ArrayList
{
public static void main(String[] args)
{
ArrayList<String> list_col= new ArrayList<String>();
list_col.add("Black");
list_col.add("Orange");
list_col.add("Pink");
list_col.add("Blue");
list_col.add("White");
list_col.add("Yellow");
System.out.println("Array List before Swap : ");
for(String a: list_col)
{
System.out.println(a);
}
Collections.swap(list_col, 1, 4);
System.out.println("Array List after swap : ");
for(String b: list_col)
{
System.out.println(b);
}
}
}
OUTPUT

public class ArielExerArray7 {


public static void main(String[] args)
{

double[] values;
values = getArray();
for(double num : values)
System.out.println(num + " ");
}

public static double[] getArray() {


double[] array = {12,4,5,2,15};
return array;
}
}

OUTPUT
public class ArielSimpleMethod
{
public static void main(String[] args)
{
System.out.println("Hello from the main method.");
displayMessage();
System.out.println("Back in the main method.");
}
public static void displayMessage()
{
System.out.println("Hello from the displayMessage method.");
}
}

OUTPUT

You might also like