In: Computer Science
public boolean isPrime(int num){
// return true if the number is prime, false otherwise
}
public boolean isOdd(int num){
// return true if the number is odd, false otherwise
}
public String reverseMyString(String input){
// using a loop, reverse a string
// i.e. input = "hello", reversed answer: "olleh"
// i.e. input = "bye", reversed answer: "eyb"
}
public int pow(int base, int power){
// using for loop, calculate base raised to power
// i.e. base = 2, power = 3 = 2^3 = 2*2*2 = 8
// i.e. base = 5, power = 4 = 5^4 = 5*5*5*5 = 625
}
public boolean isPrime(int num) { for (int i = 2; i < num; ++i) { if (num % i == 0) { return false; } } return num > 1; } public boolean isOdd(int num) { return num % 2 == 1; } public String reverseMyString(String input) { String reverse = ""; for (int i = 0; i < input.length(); i++) { reverse = input.charAt(i) + reverse; } return reverse; } public int pow(int base, int power) { int p = 1; for (int i = 0; i < power; i++) { p *= base; } return p; }
public class UtilMethods { public boolean isPrime(int num) { for (int i = 2; i < num; ++i) { if (num % i == 0) { return false; } } return num > 1; } public boolean isOdd(int num) { return num % 2 == 1; } public String reverseMyString(String input) { String reverse = ""; for (int i = 0; i < input.length(); i++) { reverse = input.charAt(i) + reverse; } return reverse; } public int pow(int base, int power) { int p = 1; for (int i = 0; i < power; i++) { p *= base; } return p; } public static void main(String[] args) { System.out.println(new UtilMethods().isPrime(2)); System.out.println(new UtilMethods().isPrime(3)); System.out.println(new UtilMethods().isPrime(4)); System.out.println(); System.out.println(new UtilMethods().isOdd(5)); System.out.println(new UtilMethods().isOdd(6)); System.out.println(); System.out.println(new UtilMethods().reverseMyString("hello")); System.out.println(new UtilMethods().reverseMyString("bye")); System.out.println(); System.out.println(new UtilMethods().pow(2, 3)); System.out.println(new UtilMethods().pow(5, 4)); } }