Compilition of Java Programs
Compilition of Java Programs
Compilition of Java Programs
Jonathan D Wong
Table of contents
1. For loop
2. While loop
3. Do while loop
4. If statement
5. If else statement
6. Concatenate
7. Number methods
8. Array sort
9. Comparison
10. Switch statement
Java program 1
1. Display a Text Five Times
public class Main {
public static void main (String[] args) {
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println("Java is fun");
}
}
}
System.out.println();
System.out.println("Sum of numbers till " + input + " is " + sum);
}
}
Java program 2
1. Find the midpoint of two number
public class Main {
public static void main(String args[]) {
int i, j;
i = 10;
j = 20;
while(i<phones.length) {
System.out.println("I have "+phones[i] + " smartphone.");
i++;
}
}
}
Output: I have Apple smartphone.
I have Android smartphone.
I have Xiaomi smartphone.
I have Lenovo smartphone.
import java.util.Scanner;
int sum = 0;
sum += number;
System.out.println("Enter a number:");
number = input.nextInt();
}
System.out.println("Sum = " + sum);
input.close();
}
}
Output: Enter a number: 25
Enter a number: 9
Enter a number: 5
Enter a number: -3
Sum = 39
Java program 3
1. Print even numbers between 1 to 20
public class main {
public static void main(String[] args) {
int i=1;
System.out.println("Even numbers between 1 to 20:");
do{
if(i%2==0)
{
System.out.println(i);
}
i++;
while(i<=20);
}
}
Output: Even numbers between 1 to 20:
2
4
6
8
10
12
14
16
18
20
Output: b is: 6
c is: 4
b is greater than c
The comparison of two numbers
Output: a is: 5
b is: 6
b is the largest number
Output: Student: 79
You are passed the exam! Congratulation
Java program 6
1. Merging two array using concatenate
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array1 = {
1,
2,
3,
4
};
int[] array2 = {
5,
6,
7,
8
};
int length = array1.length + array2.length;
System.out.println("First Array is: ");
for (int i = 0; i < array1.length; i++) {
System.out.print(" " + array1[i]);
}
System.out.println(" ");
System.out.println("Second Array is: ");
for (int i = 0; i < array2.length; i++) {
System.out.print(" " + array2[i]);
}
}
}
System.out.println(s4);
}
}
Java program 7
1. The methods that add two numbers
public class Main {
public int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
Output: 40
2. The method takes a number as its parameter and
returns the square of the number.
public class main {
public static int square(int num) {
Square of 2 is: 4
Square of 3 is: 9
Square of 4 is: 16
Square of 5 is: 25
Java program 8
1. Array elements in ascending order
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
int count, temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of elements you want in the array: ");
count = scan.nextInt();
int num[] = new int[count];
System.out.println("Enter array elements:");
for (int i = 0; i < count; i++)
{
num[i] = scan.nextInt();
}
scan.close();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++)
{
System.out.print(num[i] + ", ");
}
System.oiut.print(num[count - 1]);
}
}
Integer[] IntArray = {52, 45, 32, 64, 12, 87, 78, 98, 23, 7};
Sorted Array: [98, 87, 78, 64, 52, 45, 32, 23, 12, 7]
3. Sort array numbers in random order
import java.util.Arrays;
import java.util.Random;
public class ShuffleArray {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7 };
Output: [6, 4, 2, 1, 5, 3, 7]
Java program 9
1. Compare two strings using equals
public class main {
public static void main(String[] args) {
if(style == style2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Output: Equal
listOne.retainiAll(listTwo);
Java program 10
1. Used the switch statement to find the size
switch (number) {
case 29:
size = "Small";
break;
case 42:
size = "Medium";
break;
case 44:
size = "Large";
break;
case 48:
size = "Extra Large";
break;
default:
size = "Unknown";
break;
}
System.out.println("Size: " + size);
}
}
char operator;
Double number1, number2, result;
switch (operator) {
case '+':
result = number1 + number2;
System.out.print(number1 + "+" + number2 + " = " + result);
break;
case '-':
result = number1 - number2;
System.out.print(number1 + "-" + number2 + " = " + result);
break;
case '*':
result = number1 * number2;
System.out.print(number1 + "*" + number2 + " = " + result);
break;
case '/':
result = number1 / number2;
System.out.print(number1 + "/" + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}