Skip to content

Commit 3fd4438

Browse files
committed
Java Tutorial for Beginners
1 parent a2488cf commit 3fd4438

24 files changed

+705
-0
lines changed

src/10_TernaryOperator.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
/**
3+
*
4+
* Ternary Operator
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
int a = 2;
11+
int b = 3;
12+
13+
int min = (a < b)? a : b;
14+
15+
System.out.println(min);
16+
}
17+
}

src/11_SwitchCase.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
*
4+
* Switch Case Statements
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
char grade = 'B';
11+
12+
switch (grade) {
13+
case 'A':
14+
System.out.println("Excellent. Grade A.");
15+
break;
16+
case 'B':
17+
System.out.println("Well Done. Grade B.");
18+
break;
19+
case 'C':
20+
System.out.println("You Passed. Grade C");
21+
break;
22+
case 'F':
23+
System.out.println("Failed, Grade F");
24+
break;
25+
default:
26+
System.out.println("Invalid Grade Entered");
27+
}
28+
}
29+
}

src/12_FORLoop.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/**
3+
*
4+
* FOR Loop
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
for (int i = 0; i <= 20; i++) {
11+
if (i % 2 == 0) {
12+
System.out.println(i);
13+
}
14+
}
15+
16+
System.out.println();
17+
18+
for (int i = 20; i >= 0; i--) {
19+
if (i % 2 == 0) {
20+
System.out.println(i);
21+
}
22+
}
23+
}
24+
}

src/13_WhileLoop.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/**
3+
*
4+
* While Loop
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
int i = 0;
11+
while (i <= 20) {
12+
if (i % 2 == 0) {
13+
System.out.println(i);
14+
}
15+
i++;
16+
}
17+
18+
System.out.println();
19+
20+
int j = 20;
21+
while (j >= 0) {
22+
if (j % 2 == 0) {
23+
System.out.println(j);
24+
}
25+
j--;
26+
}
27+
}
28+
}

src/14_DoWhileLoop.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/**
3+
*
4+
* Do-While Loop
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
int i = 0;
11+
do {
12+
if (i % 2 == 0) {
13+
System.out.println(i);
14+
}
15+
i++;
16+
} while (i <= 20);
17+
18+
System.out.println();
19+
20+
int j = 20;
21+
do {
22+
if (j % 2 == 0) {
23+
System.out.println(j);
24+
}
25+
j--;
26+
} while (j >= 0);
27+
}
28+
}

src/15_BreakKeyword.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/**
3+
*
4+
* Break Statement and Labelled FOR Loop
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
outer: for (int i = 1; i <= 3; i++) {
11+
for (int j = 1; j <= 3; j++) {
12+
if (i == 2 && j == 2) {
13+
break outer;
14+
}
15+
System.out.println(i + " " + j);
16+
}
17+
}
18+
}
19+
}

src/16_ContinueKeyword.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/**
3+
*
4+
* Continue Statement and Labelled FOR Loop
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
outer: for (int i = 1; i <= 3; i++) {
11+
for (int j = 1; j <= 3; j++) {
12+
if (i == 2 && j == 2) {
13+
continue outer;
14+
}
15+
System.out.println(i + " " + j);
16+
}
17+
}
18+
}
19+
}

src/17_Arrays.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
*
4+
* Arrays
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
int[] firstSet = {2, 4, 7, 5, 11};
11+
12+
int[] secondSet = new int[5];
13+
secondSet[0] = 2;
14+
secondSet[1] = 4;
15+
secondSet[2] = 7;
16+
secondSet[3] = 5;
17+
secondSet[4] = 11;
18+
19+
for (int i = 0; i < firstSet.length; i++) {
20+
System.out.println(firstSet[i]);
21+
}
22+
23+
System.out.println();
24+
25+
for (int number: secondSet) {
26+
System.out.println(number);
27+
}
28+
}
29+
}

src/18_MultiDimensionalArray.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/**
3+
*
4+
* 2D Array and Jagged Array
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
int[][] myArray = {
11+
{2, 45, 6, 32},
12+
{4, 65, 34},
13+
{7, 8, 12, 5}
14+
};
15+
16+
for (int r = 0; r < myArray.length; r++) {
17+
for (int c = 0; c < myArray[r].length; c++) {
18+
System.out.print(myArray[r][c] + " ");
19+
}
20+
System.out.println();
21+
}
22+
23+
// WAP to find sum
24+
25+
int sum = 0;
26+
for (int[] aMyArray: myArray) {
27+
for (int anMyArray: aMyArray) {
28+
sum = sum + anMyArray;
29+
}
30+
}
31+
32+
System.out.println("Sum is: " + sum);
33+
}
34+
}

src/19_Functions.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
*
4+
* Methods or Functions
5+
* */
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
// WAP to find if a number is even or odd
11+
String result1 = isEven(4);
12+
String result2 = isEven(5);
13+
14+
System.out.println(result1);
15+
System.out.println(result2);
16+
}
17+
18+
private static String isEven(int num) {
19+
20+
String str;
21+
22+
if (num % 2 == 0)
23+
str = num + " is Even";
24+
else
25+
str = num +" is odd";
26+
27+
return str;
28+
}
29+
}

0 commit comments

Comments
 (0)