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

In Java Please

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

Creating recursive sequences

You will create an R17 project and class and implement the following interface: IR17.java

public interface IR17 { /* Precondition: n >= 0 (no need to check). Postcondition: returns the nth number in a sequence where each element is twice its predecessor, and starts with 1. */ public int pracSeq1(int n); /* Precondition: n >= 0 (no need to check). Postcondition: returns the nth number in a sequence where each element is the sum of the previous 3, and starts with 1,2,3 */ public int sequence2(int n); /* Precondition: n >= 0 (no need to check). Postcondition: returns the nth number in a sequence where each element is twice its predecessor (n-1) plus 3 times its predecessor (n-2) and starts with 1, 2. 7. */ public int sequence3(int n); } 

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. Next, complete the code for methods

sequence2 

and

sequence3 

according to the pre and post-conditions.

Both 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

public static void main(String[] args) { R17 rec = new R17(); System.out.println("pracSeq1(int x):"); System.out.println("Answer: " + rec.pracSeq1(5) + " Expecting: 32"); System.out.println("Answer: " + rec.pracSeq1(7) + " Expecting: 128 "); System.out.println("sequence(int x):"); System.out.println("Answer: " + rec.sequence2(4) + " Expecting: 11"); System.out.println("Answer: " + rec.sequence2(5) + " Expecting: 20 "); System.out.println("sequence3:"); System.out.println("Answer: " + rec.sequence3(2)); System.out.println("Answer: " + rec.sequence3(3)); System.out.println("Answer: " + rec.sequence3(6)); } 

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!