JetBrains Academy: Defining methods - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Calculating factorials:
Write a method that calculates the factorial of a given number.
The factorial of n is calculated by the product of integer number from 1 to n (inclusive). The factorial of 0 is equal to 1.
public static long factorial(long n) {
long result = 1;
for (int i = 1; i <= n; i++){
result *= i;
}
return result;
}
Composite numbers:
Write a method that tests the given number is composite or not. The composite number is a positive integer that has at least one divisor other than 1 and itself.
The method should return a value of the boolean type.
public static boolean isComposite(long number) {
for (int i = 2; i < number; i++){
if (number % i == 0){
return true;
}
}
return false;
}
Division:
Given the method named divide
that takes two long numbers and returns a double value.
Write a body of the method. It should return the result of the division the first argument by the second one. It's guaranteed the second argument is not equal to zero.
public static double divide(long a, long b) {
return (double) a / b;
}
Find the max of three numbers:
Given the method named getNumberOfMaxParam
that takes three integer numbers. It should return the number of the first max in the order of the method parameters.
The first parameter has the number 1, the second one is 2, the third one is 3.
public static int getNumberOfMaxParam(int a, int b, int c) {
int max = 0;
if (a >= b && a >= c){
max = 1;
} else if (b > a && b >= c){
max = 2;
} else if (c > a && c > b){
max = 3;
}
return max;
}
Integer part of a number:
Given the method named extractInt
that takes a double value and returns an integer value.
Write a body of the method. It should return only the integer part of the given value.
public static int extractInt(double d) {
int i = (int) d;
return i;
}
Raise to the power:
Given the method power
that takes two int numbers n and m. The method should return the result value as a long value.
Write a body of the method.
public static long power(int n, int m) {
long result = 1; // -- alternatively --
for (int i = 0; i < m; i++){ //
result *= n; //
} //
return result; // return (long) Math.pow(n, m);
}
Sum of numbers in the range:
Implement a method sumInRange
for calculating the sum of numbers in the range from (inclusive), to (exclusive).
/**
* The method calculates the sum of integers in a given range
*
* @param from inclusive
* @param to exclusive
* @return the sum (long)
*/
public static long sumInRange(int from, int to) {
long sum = 0;
for (int i = from; i < to; i++){
sum += i;
}
return sum;
}
The sign of a number:
Write a method with the name sign
that takes an int number and checks whether the number is negative, positive or zero. The method should return -1, +1 or 0 respectively.
public static int sign(int number) {
if (number > 0){
return 1;
} else if (number < 0){
return -1;
} else {
return 0;
}
}
Reversing strings:
Implement a method for reversing a given array of strings. The array can have any size.
The method may reverse and return the existing array or returns a new array.
Example: the sequence of strings "java", "programming", "methods" is reversed as "methods", "programming", "java".
public static String[] reverse(String... words) {
String[] output = new String[words.length];
for (int i = 0; i < words.length; i++){
output[words.length - 1 - i] = words[i];
}
return output;
}
Sorting numbers:
Implement a method for sorting a given array of integers in the ascending order. You can use any algorithm for sorting it.
public static void sort(int[] numbers) {
Arrays.sort(numbers);
}
Vowel or not:
Implement a method for checking the given English letter is a vowel or not. The input may be in any case.
Do not consider the letter 'y' as a vowel.
public static boolean isVowel(char ch) {
List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');
ch = Character.toLowerCase(ch);
return vowels.contains(ch);
}