Java Lab Programs Cheat Sheet
1️⃣ Mathematical / Algorithm Formulas
HCF (Euclid Algorithm)
while (b ≠ 0)
{
temp = b
b = a % b
a = temp
}
HCF = a
LCM Formula
LCM = (a × b) / HCF
Triangle Validity
a + b > c
b + c > a
a + c > b
Triangle Area (Heron’s Formula)
s = (a + b + c) / 2
Area = √( s(s-a)(s-b)(s-c) )
Java usage:
[Link](value)
Complex Number Addition
(a + bi) + (c + di)
Real = a + c
Imaginary = b + d
Matrix Trace
Trace = sum of diagonal elements
Condition:
i == j
Matrix Transpose
transpose[i][j] = matrix[j][i]
Sum and Reverse of Digits
digit = n % 10
sum = sum + digit
rev = rev × 10 + digit
n = n / 10
Cube Volume
Volume = a³
Box Volume
Volume = l × b × h
Cylinder Volume
Volume = π × r² × h
π used in program:
3.14
Sphere Volume
Volume = (4/3) × π × r³
Square Area
Area = side²
Triangle Area (simple formula)
Area = ½ × base × height
Cube Surface Area
Area = 6 × a²
Sphere Surface Area
Area = 4 × π × r²
2️⃣ Java Input Functions
Scanner
Usage
// import
import [Link].*;
Scanner sc = new Scanner([Link]);
Reads keyboard input.
nextInt()
Usage
[Link]()
Example
int n = [Link]();
Reads integer.
nextLine()
Usage
[Link]()
Example
String s = [Link]();
Reads full line.
close()
Usage
[Link]();
Closes scanner.
3️⃣ Java String Functions
toLowerCase()
Usage
[Link]()
Example
[Link]()
Converts to lowercase.
replaceAll()
Usage
[Link](pattern, replacement)
Example
[Link]("\\s","")
Removes spaces.
toCharArray()
Usage
[Link]()
Example
char[] arr = [Link]();
Converts string → char array.
4️⃣ Java Array Utility Functions
import
import [Link].*;
[Link]()
Usage
[Link](array)
Example
[Link](arr);
Sorts array.
[Link]()
Usage
[Link](array1,array2)
Example
[Link](a,b)
Checks if arrays are equal.
[Link]()
Usage
[Link](array)
Example
[Link](arr)
Prints array nicely.
5️⃣ Java Integer Conversion Functions
[Link]()
Usage
[Link](string)
Example
int a = [Link](args[0]);
Converts string → integer.
Integer Base Conversions
Binary
[Link](number)
Example
[Link](10)
Output
1010
Octal
[Link](number)
Hexadecimal
[Link](number)
Uppercase example:
[Link](num).toUpperCase()
6️⃣ Java Thread Functions
Thread Creation
Thread t = new Thread(object);
Example
Thread t1 = new Thread(new Even(n));
start()
Usage
[Link]()
Example
[Link]();
Starts new thread.
run()
Usage
public void run()
Contains thread code.
Regular Expression (Regex)
Remove Vowels
Pattern
(?i)[aeiou]
Usage
[Link]("(?i)[aeiou]", "")
Meaning
(?i) → case insensitive
[aeiou] → vowels
Removes vowels from string.