-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
263 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
public class HelloWorld { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, world!"); | ||
|
||
int[] sampleArray = {3, 5, 7, 2, 8}; | ||
System.out.println("Max: " + findMax(sampleArray)); | ||
System.out.println("Min: " + findMin(sampleArray)); | ||
|
||
printArray(sampleArray); | ||
System.out.println("Sum: " + add(5, 3)); | ||
System.out.println("Difference: " + subtract(5, 3)); | ||
System.out.println("Product: " + multiply(5, 3)); | ||
System.out.println("Quotient: " + divide(5, 3)); | ||
System.out.println("Is 4 even? " + isEven(4)); | ||
} | ||
|
||
public static int add(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
public static int subtract(int a, int b) { | ||
return a - b; | ||
} | ||
|
||
public static int multiply(int a, int b) { | ||
return a * b; | ||
} | ||
|
||
public static int divide(int a, int b) { | ||
if (b == 0) { | ||
throw new IllegalArgumentException("Division by zero is not allowed"); | ||
} | ||
return a / b; | ||
} | ||
|
||
public static boolean isEven(int number) { | ||
return number % 2 == 0; | ||
} | ||
|
||
public static void printArray(int[] array) { | ||
for (int element : array) { | ||
System.out.println(element); | ||
} | ||
} | ||
|
||
public static int findMax(int[] array) { | ||
if (array == null || array.length == 0) { | ||
throw new IllegalArgumentException("Array cannot be null or empty"); | ||
} | ||
int max = array[0]; | ||
for (int i = 1; i < array.length; i++) { | ||
if (array[i] > max) { | ||
max = array[i]; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
public static int findMin(int[] array) { | ||
if (array == null || array.length == 0) { | ||
throw new IllegalArgumentException("Array cannot be null or empty"); | ||
} | ||
int min = array[0]; | ||
for (int i = 1; i < array.length; i++) { | ||
if (array[i] < min) { | ||
min = array[i]; | ||
} | ||
} | ||
return min; | ||
} | ||
|
||
// Additional methods to increase line count | ||
public static double add(double a, double b) { | ||
return a + b; | ||
} | ||
|
||
public static double subtract(double a, double b) { | ||
return a - b; | ||
} | ||
|
||
public static double multiply(double a, double b) { | ||
return a * b; | ||
} | ||
|
||
public static double divide(double a, double b) { | ||
if (b == 0) { | ||
throw new IllegalArgumentException("Division by zero is not allowed"); | ||
} | ||
return a / b; | ||
} | ||
|
||
public static String greet(String name) { | ||
return "Hello, " + name + "!"; | ||
} | ||
|
||
public static void printMatrix(int[][] matrix) { | ||
for (int[] row : matrix) { | ||
for (int element : row) { | ||
System.out.print(element + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public static int[][] transposeMatrix(int[][] matrix) { | ||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
int[][] transposed = new int[cols][rows]; | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
transposed[j][i] = matrix[i][j]; | ||
} | ||
} | ||
|
||
return transposed; | ||
} | ||
|
||
public static int sumArray(int[] array) { | ||
int sum = 0; | ||
for (int element : array) { | ||
sum += element; | ||
} | ||
return sum; | ||
} | ||
|
||
public static double averageArray(int[] array) { | ||
if (array == null || array.length == 0) { | ||
throw new IllegalArgumentException("Array cannot be null or empty"); | ||
} | ||
return (double) sumArray(array) / array.length; | ||
} | ||
|
||
public static boolean isPrime(int number) { | ||
if (number <= 1) { | ||
return false; | ||
} | ||
for (int i = 2; i < number; i++) { | ||
if (number % i == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static int gcd(int a, int b) { | ||
while (b != 0) { | ||
int temp = b; | ||
b = a % b; | ||
a = temp; | ||
} | ||
return a; | ||
} | ||
|
||
public static int lcm(int a, int b) { | ||
if (a == 0 || b == 0) { | ||
throw new IllegalArgumentException("LCM of zero is not defined"); | ||
} | ||
return Math.abs(a * b) / gcd(a, b); | ||
} | ||
|
||
public static boolean isPalindrome(String str) { | ||
int left = 0; | ||
int right = str.length() - 1; | ||
|
||
while (left < right) { | ||
if (str.charAt(left) != str.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static int factorial(int number) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException("Factorial of negative number is not defined"); | ||
} | ||
int result = 1; | ||
for (int i = 1; i <= number; i++) { | ||
result *= i; | ||
} | ||
return result; | ||
} | ||
|
||
public static void bubbleSort(int[] array) { | ||
int n = array.length; | ||
boolean swapped; | ||
for (int i = 0; i < n - 1; i++) { | ||
swapped = false; | ||
for (int j = 0; j < n - i - 1; j++) { | ||
if (array[j] > array[j + 1]) { | ||
int temp = array[j]; | ||
array[j] = array[j + 1]; | ||
array[j + 1] = temp; | ||
swapped = true; | ||
} | ||
} | ||
if (!swapped) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public static int binarySearch(int[] array, int key) { | ||
int left = 0; | ||
int right = array.length - 1; | ||
|
||
while (left <= right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if (array[mid] == key) { | ||
return mid; | ||
} | ||
if (array[mid] < key) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public static int fibonacci(int n) { | ||
if (n <= 0) { | ||
throw new IllegalArgumentException("Fibonacci number is not defined for non-positive integers"); | ||
} | ||
if (n == 1) { | ||
return 0; | ||
} | ||
if (n == 2) { | ||
return 1; | ||
} | ||
int a = 0; | ||
int b = 1; | ||
int fib = 0; | ||
for (int i = 3; i <= n; i++) { | ||
fib = a + b; | ||
a = b; | ||
b = fib; | ||
} | ||
return fib; | ||
} | ||
|
||
public static void printFibonacciSeries(int n) { | ||
if (n <= 0) { | ||
throw new IllegalArgumentException("Fibonacci series is not defined for non-positive integers"); | ||
} | ||
int a = 0; | ||
int b = 1; | ||
System.out.print(a + " " + b); | ||
for (int i = 3; i <= n; i++) { | ||
int fib = a + b; | ||
System.out.print(" " + fib); | ||
a = b; | ||
b = fib; | ||
} | ||
System.out.println(); | ||
} | ||
} |