0% found this document useful (0 votes)
36 views1 page

Hello World!: Adano, Shara Laine M. BSBA 1 - 11

The document provides 3 sample codes written in Java that demonstrate printing "Hello World", calling methods within a class, and using a for loop to calculate and print factorials up to 100. The codes show the basic structure of a Java class with a main method and additional methods, as well as how to call methods and use control structures like for loops.
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)
36 views1 page

Hello World!: Adano, Shara Laine M. BSBA 1 - 11

The document provides 3 sample codes written in Java that demonstrate printing "Hello World", calling methods within a class, and using a for loop to calculate and print factorials up to 100. The codes show the basic structure of a Java class with a main method and additional methods, as well as how to call methods and use control structures like for loops.
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/ 1

ADANO, SHARA LAINE M.

BSBA 1 – 11

Sample Codes of Java

1. Hello World!
/* HelloWorld.java
*/

public class HelloWorld


{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

2. Calling Methods
/* CallingMethodsInSameClass.java
*
* illustrates how to call static methods a class
* from a method in the same class
*/

public class CallingMethodsInSameClass


{
public static void main(String[] args) {
printOne();
printOne();
printTwo();
}

public static void printOne() {


System.out.println("Hello World");
}

public static void printTwo() {


printOne();
printOne();
}
}

3. For Loop
public class Factorial
{
public static void main(String[] args)
{ final int NUM_FACTS = 100;
for(int i = 0; i < NUM_FACTS; i++)
System.out.println( i + "! is " + factorial(i));
}

public static int factorial(int n)


{ int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
}

You might also like