QUESTION 1 Write code to complete doublePennies()'s base case. Sampleoutput for below program: Number of pennies after 10 days: 1024 Note: These activities may test code with different test...



QUESTION 1


Write code to complete doublePennies()'s base case. Sample output for below program:


Number of pennies after 10 days: 1024


Note: These activities may test code with different test values. This activity will perform three tests, with startingPennies = 1 and userDays = 10, then with startingPennies = 1 and userDays = 40, then with startingPennies = 1 and userDays = 1. See How to Use zyBooks. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.


public class CalculatePennies { // Returns number of pennies if pennies are doubled numDays times    public static long doublePennies(long numPennies, int numDays) {       long totalPennies = 0;


      /* Your solution goes here */


      else {          totalPennies = doublePennies((numPennies * 2), numDays - 1);       }       return totalPennies;    }


// Program computes pennies if you have 1 penny today, // 2 pennies after one day, 4 after two days, and so on    public static void main (String [] args) {       long startingPennies = 0;       int userDays = 0;


      startingPennies = 1;       userDays = 10;       System.out.println("Number of pennies after " + userDays + " days: "            + doublePennies(startingPennies, userDays));       return;    } }


QUESTION 2


Write code to complete printFactorial()'s recursive case. Sample output if userVal is 5:


5! = 5 * 4 * 3 * 2 * 1 = 120

public class RecursivelyPrintFactorial {    public static void printFactorial(int factCounter, int factValue) {       int nextCounter = 0;       int nextValue = 0;


      if (factCounter == 0) { // Base case: 0! = 1          System.out.println("1");       }       else if (factCounter == 1) { // Base case: Print 1 and result          System.out.println(factCounter + " = " + factValue);       }       else { // Recursive case          System.out.print(factCounter + " * ");          nextCounter = factCounter - 1;          nextValue = nextCounter * factValue;


         /* Your solution goes here */


      }    }


   public static void main (String [] args) {       int userVal = 0;


      userVal = 5;       System.out.print(userVal + "! = ");       printFactorial(userVal, userVal);


      return;    } }


Nov 11, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here