Skip to content

Instantly share code, notes, and snippets.

@bdjunayed
Created November 15, 2023 13:28
Show Gist options
  • Save bdjunayed/e3a2d079638cf072ef87b196a2784747 to your computer and use it in GitHub Desktop.
Save bdjunayed/e3a2d079638cf072ef87b196a2784747 to your computer and use it in GitHub Desktop.
Basic Java examples
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]
}
}
}
}
// 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;
}
}
}
// 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);
}
}
// 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;
}
}
}
// 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);
}
}
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;
}
}
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();
}
}
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;
}
}
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);
}
}
public class Test1 {
public static void printEidBonus(int x)
{
x = x*2;
System.out.print(x);
}
public static void main(String[] args) {
printEidBonus(15000);
}
}
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);
}
}
// 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);
}
}
// 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