Created
November 15, 2023 13:28
-
-
Save bdjunayed/e3a2d079638cf072ef87b196a2784747 to your computer and use it in GitHub Desktop.
Basic Java examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void main(String[] args) { | |
// Array iteration | |
int[][] b; | |
b = new int[3][2]; | |
// Value assigne | |
b[0][0] = 56; | |
b[0][1] = 56; | |
b[1][0] = 44; | |
b[1][1] = 56; | |
b[2][0] = 56; | |
b[2][1] = 56; | |
// int[][] b = {{1, 3}, {5 ,8}, {77, 88}}; | |
int row = 3; | |
int col = 2; | |
int[][] temp = new int[3][2]; | |
for(int i = 0; i<row; i++) { | |
for(int j = 0; j<col; j++) { | |
// temp[i][j] == b[i][j] | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return Max number | |
public class Test1 { | |
public static void main(String[] args) { | |
System.out.println( findMax(10, 5) ); | |
System.out.println( findMax(100, 200) ); | |
} | |
public static int findMax(int a, int b) { | |
if(a > b) { | |
return a; | |
} else { | |
return b; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method overloading example: | |
// Find and print the Area | |
public class Test1 { | |
public static void main(String[] args) { | |
printArea(7, 9); | |
printArea(9); | |
} | |
public static void printArea(int a, int b) { | |
System.out.println(a * b); | |
} | |
public static void printArea(int a) { | |
System.out.println(a * a); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find and return Max number | |
public class Test1 { | |
public static void main(String[] args) { | |
System.out.println( findMax(10, 5) ); | |
System.out.println( findMax(100, 200) ); | |
System.out.println( findMax(1.5f, 2.5f) ); | |
} | |
public static int findMax(int a, int b) { | |
if(a > b) { | |
return a; | |
} else { | |
return b; | |
} | |
} | |
public static float findMax(float a, float b) { | |
if(a > b) { | |
return a; | |
} else { | |
return b; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Error code | |
public class Test1 { | |
public static void main(String[] args) { | |
int num = 5; | |
System.out.println(num); | |
} | |
public static void m1(){ | |
double num2 = 6.28; | |
System.out.println(num2); | |
System.out.println(num); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void main(String[] args) { | |
// Declaration | |
int[] a; | |
int[][] b; | |
// int[][] f; | |
// Assignment or initialize | |
a = new int[5]; | |
b = new int[3][2]; | |
// f = { {56, 12, 55}, {55, 66, 12} }; | |
// initialization | |
int[] d = new int[5]; | |
int[] c = {45, 56, 67}; | |
int[][] f = { {56, 12, 55}, {55, 66, 12} }; | |
// Value assignment | |
b[0][0] = 56; | |
b[0][1] = 56; | |
b[1][0] = 44; | |
b[1][1] = 56; | |
b[2][0] = 56; | |
b[2][1] = 56; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void counter(){ | |
for (int x = 1; x <= 5; x++) { | |
System.out.println(x); | |
} | |
} | |
public static void main(String[] args) { | |
counter(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void main(String[] args) { | |
int num = five(); | |
System.out.println(num); | |
} | |
public static int five() { | |
int num = 5; | |
return num; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static int add(int x, int y) { | |
return x + y; | |
} | |
public static void main(String[] args) { | |
int z; | |
z = add(150, 100); | |
System.out.println(z); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void printEidBonus(int x) | |
{ | |
x = x*2; | |
System.out.print(x); | |
} | |
public static void main(String[] args) { | |
printEidBonus(15000); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static int findEidBonusWithSalaries(int x) | |
{ | |
x = x*2; | |
return x; | |
} | |
public static void main(String[] args) { | |
int z; | |
z = findEidBonusWithSalaries(15000); | |
System.out.println("Bonus with the Salaries is: " + z); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find and print the Area | |
public class Test1 { | |
public static void main(String[] args) { | |
printArea(7, 9); | |
} | |
public static void printArea(int a, int b) { | |
System.out.println(a * b); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find and print Even/Odd numbre | |
public class Test1 { | |
public static void main(String[] args) { | |
printEvenOdd(5); | |
printEvenOdd(8); | |
} | |
public static void printEvenOdd(int a) { | |
if(a / 2 == 0) { | |
System.out.print("The number is Even."); | |
} else { | |
System.out.print("The number is Odd."); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment