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...
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...
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...
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. 
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the...
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 arrays are equals(same values in the same indices), false otherwise if(arr1.length != arr2.length){ return false; } for(int i = 0; i < arr1.length; i++){ if(arr1[i] != arr2[i]){ } } return true; } public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1...
For each statement below circle TRUE if the statement is true, otherwise circle FALSE.
For each statement below circle TRUE if the statement is true, otherwise circle FALSE. A hypothesis test uses data from a sample to assess a claim about a population. A randomization distribution will be centered at the value used in the null hypothesis. “Failing to reject the null hypothesis” and “accepting the null hypothesis” mean the same thing in a hypothesis test’s conclusion. The p-value is the probability, assuming the alternative hypothesis is true, of obtaining a sample statistic as...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the value of the variable count is 0 and the value of the variable limit is 10. Give your answer as one of the values true or false. a. (count == 0) && (limit < 20) b. count == 0 && limit < 20 c. (limit > 20) || (count < 5) d. !(count == 12) e. (count == 1) && (x < y) f....
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT