Question

In: Computer Science

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 following parameters: numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}, startIndex = 0

Solutions

Expert Solution

// TestCode.java
public class TestCode {
    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);

    }

    public static void main(String args[])
    {
        int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
        int startIndex = 0;

        System.out.println(Method(numbers,startIndex));
    }
}

6

the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}, startIndex = 0 is 6

Related Solutions

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
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 =...
Required methods:: public static void processAccount (Account[] acc, printWriter out{}. public static boolean deposit(Account a){}.
Java programming. Required methods::public static void processAccount (Account[] acc, printWriter out{}.public static boolean deposit(Account a){}.public static boolean withdrawal(Account a){}.public static int findAccount(long accountNumber, Account[] acc){}. 1/ Remove the applyRandomBonus method from the test class2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file:a. Skip any lines that cause an exceptionb. Write information about the exception to the log file, yourlastnameError.txtc. Include exception type and line number in exception message...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Consider the following code: public class Example { public static void doOp(Op op) { boolean result...
Consider the following code: public class Example { public static void doOp(Op op) { boolean result = op.operation(true, false); System.out.println(result); } public static void main(String[] args) { doOp(new AndOperation()); doOp(new OrOperation()); } } main's output: false true Define any interfaces and/or classes necessary to make this output happen. Multiple answers are possible. You may not modify any of the code in Example.
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
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...
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...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call computeFibRecurse(n). private int computeFibRecurse(int n), which should recurse (that is, call itself) unless n is 1 or 2. If n is 1 or 2, the method should return 1. A main method that prints “STARTING”, then constructs a FibGenerator generator and then calls nthFib(), passing in interesting values. To look into this problem, you’re going to use software to analyze software. Add an instance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT