Question

In: Computer Science

public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public...

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

}

Solutions

Expert Solution

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));
    }
}


Related Solutions

(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
boolean isEligibile (int, char, char) using Drjava isEligibile will return true when the customer is eligible...
boolean isEligibile (int, char, char) using Drjava isEligibile will return true when the customer is eligible to book a vacation package, and false otherwise. It takes as input 3 pieces of information, first an int representing the customer’s age. Second, a char representing the country where the rental is being made (‘c’ for Canada, ‘u’ for United States, ‘f’ for France). The third input is a char representing if the customer has a valid Canadian passport ‘y’ for Yes, ‘n’...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap. For example: countSubstrings("aa", "aaaaa", false) returns 2 countSubstrings("aa", "aaaaa", true) returns 4 countSubstrings("aa", "ababab", true) returns 0 Parameters: t - string we are looking for ("target") s - string in which we are looking ("source") allowOverlap - true if occurrences of t are allowed to overlap Returns: number of times t...
IN C int count_primes(int start, int end) {   //TODO: return the count of prime numbers in...
IN C int count_primes(int start, int end) {   //TODO: return the count of prime numbers in range [start, end] inclusive.   return 0; }
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.
Given:   S = [ 2, 1, 3, 4, 7, 5 ] Z = 8   Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.   Code in Java 8. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT