0% found this document useful (0 votes)
2 views2 pages

COSC2006 Lab 2 - Recursion

Uploaded by

Aiwu Xu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

COSC2006 Lab 2 - Recursion

Uploaded by

Aiwu Xu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COSC2006 Lab 2 – Introduction to Recursion

In-Lab Discussion
Using the following algorithm, answer the following questions:

public static void downToZeroByThree(int value) {


If(value != 1) {
[Link](value);
downToZeroByThree(value - 3);
}

P
}

4S
(a) What happens when the statement downToZeroByThree(10) is executed? Show the

,2
output.

le
(b) What happens when the statement downToZeroByThree(6) is executed? Show the

so
output.

on
(c) What happens when the statement downToZeroByThree(14) is executed? Show the
output.

C
(d) What is the mathematical pattern? Think about the operations we can do in Java and the
ny
properties of the numbers we used in the previous steps.
n
oh

(e) How can the problem be broken into multiple subproblems of the same type?
.J

(f) How does each recursive call diminish the size of the problem?
of

(g) What instance of the problem space can serve as the base case?
Pr

(h) Will this solution always reach the base case? If not, explain why.
T
H

(i) What change can be made to make the solution correct?


IG

(j) A recursive method is cleaner when it is in the format:


R

public static void recursiveMethod(int parameter) {


PY

if(baseCase) return;
else {
recursiveWork;
O

}
C

Rewrite the downToZeroByThree method in the format provided.

Copyright © 2021-2024 by Prof. Johnny Console, Algoma University.


All rights reserved. No parts of this work may be reproduced by any means without prior
written permission from the author.
COSC2006 Lab 2 – Introduction to Recursion

Coding – Submit all .java files to the LMS. Submit separate files for each question.
Down To Zero. Using the provided method header, rewrite the algorithm discussed in the
discussion part, changing it to get from a starting value to zero by a given value n. Your main
program must ask for the starting value and the value of n and display the sequence as shown in
the discussion. You can use the example executions below to test your work, but your code
should work for different values as well.

public static void downToZeroByN(int value, int n)

P
Examples:

4S
downToZeroByN(10, 3) would show 10 7 4 1
downToZeroByN(16, 2) would show 16 14 12 10 8 6 4 2

,2
downToZeroByN(120, 10) would show 120 110 100 90 80 70 60 50 40 30 20 10

le
The Maximum Character. Using the provided method header, write a method to determine the

so
character with the highest ASCII value within a string. Your main program must ask for the
string. You can use the example executions below to test your work, but your code should work

on
for different values.

C
public static char maximumChar(String str, char max)
ny
Examples:
n
maximumChar(“Data Structures”, ‘\0’) returns the character ‘u‘
oh

maximumChar(“Algoma U”, ‘\0’) returns the character ‘o‘


maximumChar(“COSC2006”, ‘\0’) returns the character ‘S’
.J

maximumChar(“1234”, ‘\0’) returns the character ‘4’


of

Reverse Number. Using the provided method header, write a method to reverse a number. In
Pr

your reverse method, if the number is negative, reverse the number and put the negative sign
after the reversed number. (this check is not in main) Your main program must ask for the
T

number. HINT: you will need to use the divide operation and the modulus operator for this
H

question. You can use the example executions below to test your work, but your code should
also work for different values. You are not allowed to use any string operations here.
IG
R

public static void reverse(int number)


PY

Examples:
Reverse(12345) prints 54321
O

Reverse(-12345) prints 54321-


C

Your methods for this lab must be recursive. Your solutions to each question should only
include the main method and the recursive method that solves the problem. No other
methods are permitted. Your recursive method headers must match the ones provided, and
you must use all parameters. Breaking any of these will result in a zero for the question.
Do not use any static variables in any of your programs. Doing so will result in a grade of zero
for the entire lab.
Copyright © 2021-2024 by Prof. Johnny Console, Algoma University.
All rights reserved. No parts of this work may be reproduced by any means without prior
written permission from the author.

You might also like