0% found this document useful (0 votes)
24 views3 pages

Harleen Kaur Lab 2

Uploaded by

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

Harleen Kaur Lab 2

Uploaded by

Harleen Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Harleen Kaur

Student ID : 229583770

Lab 2

Ans 1) a:

We receive 3 numbers in output which are

Ans 1) b:

The output runs till stackOverflow error

Ans 1) c:
Recursion in Java involves dissecting a problem into identical subproblems, applying the same
method iteratively to progressively smaller input, and assembling the results to solve the original
problem while respecting specific termination criteria.

Ans 1) d:
With each recursive invocation in Java, the problem size undergoes reduction by narrowing the
scope of the function's operation to a more compact and manageable segment of the initial data.
This decrease typically entails subdividing the problem into subproblems structurally identical to the
original one but focusing on a smaller data segment. As successive recursive calls occur, they operate
on a reduced portion of the input, often approaching the base case or termination condition,
progressively diminishing the problem's scale during each recursion step. This recurrent process
continues until the base case is achieved, at which point the problem is directly resolved for that
particular subset, enabling the unwinding of recursion to consolidate outcomes and, ultimately,
resolve the initial, broader problem.

Ans 1) e:
The base case in recursion is the simplest problem instance where the algorithm can directly provide
a solution without further recursion. Its selection depends on the specific problem and serves as the
stopping point, ensuring the recursive calls eventually reach this elementary case. For example, in
factorial calculations, the base case is often factorial(0) = 1, while for Fibonacci sequences, it's
typically fib(0) = 0 and fib(1) = 1. A well-defined base case is vital to prevent infinite recursion and
maintain algorithm accuracy.

Ans 1) f:
Whether a recursive solution reaches the base case depends on how it's set up. If it's designed well,
it should gradually simplify the problem until it hits the base case. But if the setup isn't right—like
missing rules to stop—it might never reach the base case. That can lead to big problems, like the
program running forever or crashing. So, making sure there are clear ways to stop is super important
in recursive solutions.

Ans 1) g:
To correct the solution, ensure clear and appropriate termination conditions in the recursive
algorithm. This means adding checks to stop recursion when needed, aligning them with the
problem's requirements and constraints.

Ans 1) h:

Here's an updated version of the method:

public static void downToZeroByThree(int value) {


if (value > 1) {
System.out.println(value);
downToZeroByThree(value - 3);
}
}

Ans 2)
public class DataLab1
{
public static char maximumChar(String str, char max) {
if (str.isEmpty()) {
return max;
}

char currentChar = str.charAt(0);

if (currentChar > max) {


max = currentChar;
}

return maximumChar(str.substring(1), max);


}

public static void main(String[] args) {


System.out.println(maximumChar("Data Structures", (char) 0));
System.out.println(maximumChar("Algoma U", (char) 0));
System.out.println(maximumChar("COSC2006", (char) 0));
System.out.println(maximumChar("1234", (char) 0));
}
}
Ans 3)
public class Reverse
{
public static String reverse(int number)
{
if (number == 0)
{
return "0";
}

boolean isNegative = false;

if (number < 0)
{
isNegative = true;
number = -number;
}

String reversedNumber = reversePositive(number);

if (isNegative)
{
return reversedNumber + "-";
} else {
return reversedNumber;
}
}

private static String reversePositive(int number) {


if (number < 10) {
return Integer.toString(number);
}
int lastDigit = number % 10;
return Integer.toString(lastDigit) + reversePositive(number / 10);
}

public static void main(String[] args) {


System.out.println(reverse(37142));
System.out.println(reverse(12345));
System.out.println(reverse(-12345));
System.out.println(reverse(-2929211));
}
}

You might also like