Question

In: Computer Science

What would be the result of each of the following programs? 1 public class RecursionInJava1{   public...

What would be the result of each of the following programs?

1

public class RecursionInJava1{
  public static void main(String args[]) {
       System.out.println(guess(9));

   }

Public static int guess(int num){

         if (num == 1)

                  return 0;

         else

                  return (guess(num-2)-num);

}

}

2

public class RecursionInJava2{
  public static void main(String args[]) {
   System.out.println(sum(10));

    }

  
  public static int sum(int number){     
  //base case
  if(number == 7){
  return 0;
  }
  return sum(number-1)+number

   }

}

Solutions

Expert Solution

Please find the answers for your questions below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Answer for question 1:

The program will output -24.

Explanation:

Let’s go through each recursive call of the method guess().

Initially the value passed is 9.

guess(9) :- Since 9 is not 1, else block gets executed which will call guess(9-2) and subtract 9 from it before returning it. So the method will return guess(7)-9

guess(7) :- Since 7 is not 1, else block gets executed which will call guess(7-2) and subtract 7 from it before returning it. So the method will return guess(5)-7

guess(5) :- Since 5 is not 1, else block gets executed which will call guess(5-2) and subtract 5 from it before returning it. So the method will return guess(3)-5

guess(3) :- Since 3 is not 1, else block gets executed which will call guess(3-2) and subtract 3 from it before returning it. So the method will return guess(1)-3

guess(1) :- Since 1 is equal to 1, this method simply returns 0

Combining all the return statements, we get

(((((0)-3)-5)-7)-9) => -24

Answer for question 2:

The program will output 27

Explanation:

Let’s go through each recursive call of the method sum().

Initially the value passed to sum() from the main is 10.

sum(10) :- Since 10 is not 7, sum(10-1)+10 or sum(9)+10 is returned

sum(9) :- Since 9 is not 7, sum(9-1)+9 or sum(8)+9 is returned

sum(8) :- Since 8 is not 7, sum(8-1)+8 or sum(7)+8 is returned

sum(7) :- Since 7 equals 7, 0 is returned

Combining all the return statements, we get

((((0)+8)+9)+10) = 27


Related Solutions

write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public static void main(String[] args) { // your implementation } public Ram() { // your implementation } } public class Truck extends LandVehicle { public Truck() { // your implementation } public Truck(String s) { // your implementation } } public class LandVehicle extends Vehicle { public LandVehicle() { // your implementation } } public class Vehicle { public Vehicle() { // your implementation
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.
A. What effects would result from surgical removal of each of the following? (6 pts) •...
A. What effects would result from surgical removal of each of the following? (6 pts) • Stomach • Gall bladder • Pancreas B. Removal of which one would have the biggest impact on digestion? The smallest impact? Explain your reasoning.
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
1. For the following statement, indicate whether what is described would result in an increase or...
1. For the following statement, indicate whether what is described would result in an increase or decrease in genetic diversity within population. a. Inbreeding due to a small population that is geographically isolated b. A 10,000 person population growing from the movement of 5,000 people into the population coming from another geographically isolated location. c. A catastrophic tsunami that drastically reduces the size of population. d. A small group of individuals from one population moving to a geographically distant site...
1.) Indicate whether each of the following items would result in net cash flow from operating...
1.) Indicate whether each of the following items would result in net cash flow from operating activities being higher (H) or lower (L) than net income.             a.) Decrease in accounts payable.             b.) Depreciation expense.             c.) Decrease in inventory.             d.) Gain on sale of assets.             e.) Increase in accounts receivable.             f.) Increase in deferred tax liabilities.             g.) Decrease accrued liabilities.             h.) Increase in prepaid expenses.             i.) Increase in deferred revenue.             j.)...
Select each of the following that would result in elevated cardiac output for a given heart...
Select each of the following that would result in elevated cardiac output for a given heart rate. In other words, which of the following would result in increased stroke volume? elevated end diastolic volume increased contractility physiologic cardiac hypertrophy pathologic cardiac hypertrophy reduced ejection fraction reduced preload increased blood volume increased hematocrit due to blood doping increased afterload increased circulating epinephrine
Problem 1: For each of the transactions below, record whether it would result in an increase...
Problem 1: For each of the transactions below, record whether it would result in an increase or decrease in assets and/or liabilities by placing an X in the appropriate boxes. Use the template provided to document your answers. An organization: Purchases supplies with cash Purchases supplies on account Takes out a loan Repays loan principal Receives payment on a pledge made by a donor Makes a payment on an amount it owes Prepays for insurance Pays employee wages that were...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT