Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lab Practice - Java

The document provides an overview of topics and examples to learn Java programming through hands-on practice. It includes: 1) A list of topics to be covered such as variables, operators, conditional statements, loops, arrays, strings, object-oriented programming, and inheritance. 2) Code examples demonstrating basic Java syntax like printing, arithmetic operations, conditional logic, loops, methods, and recursion. 3) The examples are meant to be practiced, tested and run manually to help learn Java programming concepts.

Uploaded by

polonoc581
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Practice - Java

The document provides an overview of topics and examples to learn Java programming through hands-on practice. It includes: 1) A list of topics to be covered such as variables, operators, conditional statements, loops, arrays, strings, object-oriented programming, and inheritance. 2) Code examples demonstrating basic Java syntax like printing, arithmetic operations, conditional logic, loops, methods, and recursion. 3) The examples are meant to be practiced, tested and run manually to help learn Java programming concepts.

Uploaded by

polonoc581
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Practice - Java

Updated Date: 19-Feb-24

Java Syntax:
https://www.w3schools.com/java/default.asp
Online Compiler:
https://www.onlinegdb.com

Topics:
Var + Operators
Comments
Conditional statements (if else , switch)
Type Casting
Loops (for loop)
Methods + Recursion
Characters + Math Functions
Java program run manually (cmd)
Java Scope
Loops (while, do-while, for-each*)
Break Continue in loop

Arrays (1d + 2d)


Linear search*
Strings

OOP + user input


Inheritance + Composition

// Ex: hello world in java


public class Main {
public static void main(String[] args) {
int money = 250;

System.out.printf("I have %d BDT today.\n", money);


System.out.println("Hello World");
System.out.println("Java & SP");
}
}

// Ex: Simple arithmetic operations


public class Main {
public static void main(String[] args) {
int x = 4;
int y = 3;
int r;

r = x + y;
System.out.println("Sum: " + r);

r = x - y;
System.out.println("Subtract: " + r);
r = x * y;
System.out.println("Multiply: " + r);

r = x / y;
System.out.println("Quotient : " + r);

r = x % y;
System.out.println("Remainder: " + r);
}
}

// Ex: Find even or odd status


public class Main {
public static void main(String[] args) {
// define var
int x = 11;

// check condition and find status


if(x % 2 == 0) {
System.out.println(x + " is even");
} else {
System.out.println(x + " is odd");
}

}
}

// Ex: Find the status of an integer


public class Main {
public static void main(String[] args) {
// define var
int x = 10;

// check condition and find status


if(x == 0) {
System.out.println(x + " is zero");
} else if(x > 0) {
System.out.println(x + " is +ve");
} else {
System.out.println(x + " is -ve");
}

}
}

// Ex: pick dice game result


public class Main {
public static void main(String[] args) {
// define var
int x = 6;

// check condition and find status


switch(x) {
case 1:
System.out.println("You have got 1");
break;
case 2:
System.out.println("You have got 2");
break;
case 3:
System.out.println("You have got 3");
break;
case 4:
System.out.println("You have got 4");
break;
case 5:
System.out.println("You have got 5");
break;
case 6:
System.out.println("You have got 6");
break;
default:
System.out.println("Invalid Result");
}

}
}

// Ex: type narrow


public class Main {
public static void main(String[] args) {
// define var
double x = 4.2;
int y;

y = (int) x;
System.out.println(y);
}
}

// Ex: program for multiplication table


public class Main {
public static void main(String[] args) {
int n = 7;

for(int i=1; i<=10; i++) {


// n x i = n*i
System.out.println(n + " x " + i + " = " + (n*i));
}
}
}

// Ex: program for factorial using user-defined method


public class Main {
public static void main(String[] args) {
int n = 4;

int factResult = factorial(n);


System.out.println("Factorial of " + n + " = " + factResult);
}

public static int factorial(int x) {


int fact = 1;
for(int i=1; i<=x; i++) {
fact = fact * i;
}

return fact;
}
}

// Ex: program for factorial using recursion


public class Main {
public static void main(String[] args) {
int n = 5;

int factResult = factorial(n);


System.out.println("Factorial of " + n + " = " + factResult);
}

public static int factorial(int x) {


if(x == 0) {
return 1;
} else {
return x * factorial(x - 1);
}
}
}

// Try sum(n) using recursion

// Ex: sum(n) for natural number set


public class Main {
public static void main(String[] args) {
int n = 5;

int result = sum(n);


System.out.println("Sum = " + result);
}

public static int sum(int x) {


if(x==1) {
return 1;
} else {
return x + sum(x - 1);
}
}
}

// Ex: Find euclidean distance for two points P1, P2


public class Main {
public static void main(String[] args) {
int x1 = 1, y1 = 4;
int x2 = 2, y2 = 3;

double dx = Math.pow((x1 - x2), 2);


double dy = Math.pow((y1 - y2), 2);

double distance = Math.sqrt((dx + dy));


System.out.println("distance = " + distance);
}
}

// try status of a character program (integer, lc alph, uc alph)


// Ex: multiplication table using while loop + methods
public class Main {
public static void main(String[] args) {
int x = 3;

printMT(x);
}

public static void printMT(int n) {


int i = 1;
while(i <= 10) {
System.out.printf("%2d x %2d = %2d\n", n, i, n*i);
i++;
}
}
}

// Ex: count total digits of a n digit number


public class Main {
public static void main(String[] args) {
int a = 321;

int digits = countDigits(a);


System.out.println("Total digits: " + digits);
}

public static int countDigits(int n) {


int c=0;

do {
n = n / 10;
c++; // c = c + 1;
} while(n != 0);

return c;
}
}

// Ex: break example code


class HelloWorld {
public static void main(String[] args) {

for(int i=1; i<=10; i++) {


if(i == 5) {
break;
}
System.out.println(i);
}
}
}

// Ex: continue example code


class HelloWorld {
public static void main(String[] args) {

for(int i=1; i<=10; i++) {


if(i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}

You might also like