Question: Overview This lab will introduce and practice with recursion! We will be doing a few tasks, all using recursion - NO LOOPS ALLOWED! Problem solving

Overview

This lab will introduce and practice with recursion! We will be doing a few tasks, all using recursion - NO LOOPS ALLOWED!

Problem solving with recursion

You will create an R19 project and class and implement the following interface: IR19.java

public interface IR19 { /* Method - sumDigits Precondition: n >= 0 (no need to check). Postcondition: Returns the sum of its base-10 digits (ex. sumDigits 1234 returns 10). Note - easy to split an integer by dividing by 10 (1234/10 = 123, 1234%10 = 4) */ public int sumDigits (int n); /* Method - countCannonballs Precondition: n >= 1 (no need to check). Postcondition: Returns the total number of cannonballs in stack Note: The cannonball stack starts with one on the top of a square of four, sitting on a square of 9, and so forth. */ public int countCannonballs (int n); /* Method - numDigits Precondition: n >= 0 (no need to check). Postcondition: returns the number of digits in a base-10 number (ex. numDigits 1234 returns 4) */ public int numDigits (int n); /* Method - backString Precondition: s is a non-null string (no need to check). Postcondition: returns the string reversed. (ex. backString yes returns sey) */ public String backString (String s); } 

Assignment Overview

Recursion is essentially when a method calls itself, asking for the solution of a smaller instance of the problem. Two things are vital when using recursion: a base case (or base cases) and the recursive case. Complete the code for the defined methods according to the pre and post-conditions.

All of these must be recursive and cannot use loops. Think about what the base cases are and what the recursive case is.

Below is test code for you to run and verify with the TA for credit for today's lab.

public static void main(String[] args) { R19 rec = new R19(); System.out.println("sumDigits(int x):"); System.out.println("Answer (1234): " + rec.sumDigits(1234) + " Expecting: 10"); System.out.println("Answer (7): " + rec.sumDigits (7) + " Expecting: 7"); System.out.println("Answer (6789): " + rec.sumDigits (6789)); System.out.println(); System.out.println("countCannonballs (int x):"); System.out.println("Answer (1): " + rec.countCannonballs(1) + " Expecting: 1"); System.out.println("Answer: " + rec.countCannonballs (4) + " Expecting: 30"); System.out.println("Answer (10): " + rec.countCannonballs (10)); System.out.println(); System.out.println("numDigits(int x):"); System.out.println("Answer (1234): " + rec.numDigits(1234) + " Expecting: 4"); System.out.println("Answer (7): " + rec.numDigits (7) + " Expecting: 1"); System.out.println("Answer (678900): " + rec.numDigits (678900)); System.out.println(); System.out.println("backString(String s):"); System.out.println("Answer (yes): " + rec.backString("yes")); System.out.println("Answer (): " + rec.backString("")); System.out.println("Answer (a): " + rec.backString("a")); System.out.println("Answer (CS163): " + rec.backString("CS163")); } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!