Recursive Algorithms and Problems in Java, Exams of Computer Science

Solutions to various recursive problems in java, including multiplication table, power calculation, and string manipulation. It covers the base and recursive cases, as well as the recursive steps for each problem.

Typology: Exams

Pre 2010

Uploaded on 09/17/2009

koofers-user-7sm-1
koofers-user-7sm-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Problem 1:
Base case: num2 == 0
Recursive case: num2 != 0
Recursive step: result = num2*method(num2, num1/num2)
Problem 2:
method(13,3)
result = 1
result = 3*method(3, 4)
result = 1
result = 4*method(4,0)
result = 1
result = 4
return 4
return 16
return 48
The final result is 48.
Problem 3:
public void multiplicationTableRecursive(int row, int col)
{
recursivelyPrintTable(row, col, col);
}
public void recursivelyPrintTable(int row, int col, int originCol)
{
if(row==0 && col==0){
System.out.print("*");
return;
}
if(col==0){
recursivelyPrintTable(row-1, originCol, originCol);
System.out.print(row);
return;
}
recursivelyPrintTable(row, col-1, originCol);
if(row==0){
System.out.print(" " + col);
}
else
{
System.out.print(" " + col*row);
}
if(col==originCol){
System.out.print("\n");
}
}
Problem 4:
Base case: (row==0 && col==0);
Recursive case: else;
recursive steps:
if(col==0): recursivelyPrintTable(row-1, originCol, originCol);
else: recursivelyPrintTable(row, col-1, originCol);
The symbols are printed after the coresponding recursive steps return. We
start from the right-bottom element of the table. If it is not the first
element in the row, col is reduced by 1 and go on with recursion. If it is
pf3
pf4

Partial preview of the text

Download Recursive Algorithms and Problems in Java and more Exams Computer Science in PDF only on Docsity!

Problem 1:

Base case: num2 == 0 Recursive case: num2 != 0 Recursive step: result = num2*method(num2, num1/num2)

Problem 2:

method(13,3) result = 1 result = 3method(3, 4) result = 1 result = 4method(4,0) result = 1 result = 4 return 4 return 16 return 48

The final result is 48.

Problem 3: public void multiplicationTableRecursive(int row, int col) { recursivelyPrintTable(row, col, col); }

public void recursivelyPrintTable(int row, int col, int originCol) { if(row==0 && col==0){ System.out.print("*"); return; }

if(col==0){ recursivelyPrintTable(row-1, originCol, originCol); System.out.print(row); return; }

recursivelyPrintTable(row, col-1, originCol); if(row==0){ System.out.print(" " + col); } else { System.out.print(" " + col*row); }

if(col==originCol){ System.out.print("\n"); } }

Problem 4: Base case: (row==0 && col==0); Recursive case: else; recursive steps: if(col==0): recursivelyPrintTable(row-1, originCol, originCol); else: recursivelyPrintTable(row, col-1, originCol);

The symbols are printed after the coresponding recursive steps return. We start from the right-bottom element of the table. If it is not the first element in the row, col is reduced by 1 and go on with recursion. If it is

the first element in a row, row is reduced by 1 and col is set to original col value.The recurrsion ends when it reaches the left-top element. Then we began to print out elements. The first is a "". When we at the first row or column, we print the coresponding row/column index, else we print out the value of rowcol. If we at the right end of a row, we print out an additional "\n".

Problem 5:

public static int powerRecursive(int base, int exp) { int result = 1;

if(base == 0) return 0;

if(exp == 0) result = 1; else result = base * powerRecursive(base, exp -1); return result;

}

Problem 6:

Base case: exp == 0 Recursive case: exp != 0 Recursive step: result = power(base, exp-1, base*result);

The variable result will be used to record the current value of exponentiation. At the very beginning, result is 1 and exp is user-defined. Each time you enter the recursive step, the exp will be decremented by 1, and result will be timed by the base. Only until exp becomes 0, the recursive step can be finished.

Problem 7:

class Number { private int value;

public Number() { value = 0; }

public Number(int value) { this.value = value; }

public int getValue() { return this.value; }

public void setValue(int value) { this.value = value; }

public String toString()

Problem 12: Holiday.equalsIngoreCase("Spring Break");

Probelem 13: data.charAt(data.length()-1);

Problem 14: has a/aggregation lays a/association is a/inheritance washes/association has a/aggregation is a/inheritance