Simple Recursions
Factorial (Subroutine): Ass1s2p1.java 
|
Create a method that will return the factorial for any integer, zero or greater: long factorial (long n). Use a counted loop. Then write a program that will test the method by letting you enter any integer. Your testing program should repeat asking for input, until a negative number is entered.
(Note that only numbers <=20 will give you valid answers)
|
// The "Ass1s2p1" class.
import java.awt.*;
import hsa.Console;
public class Ass1s2p1
{
static Console cs = new Console ();
public static void main (String [] args)
{
cs.print ("Enter a number (<0 to stop): ");
int num = cs.readInt ();
while (num >= 0)
{
cs.println ("\n" + num + "! = " + factorial (num));
cs.print ("\nEnter a number (<0 to stop): ");
num = cs.readInt ();
}
cs.println ("good bye!");
} // main method
public static long factorial (long n)
{
if (n < 1)
return 0;
long product = 1;
for (int i = 1 ; i <= n ; i++)
product *= i;
return product;
}
} // Ass1s2p1 class
|
Factorial (Recursion): Ass1s2p2.java 
|
Use recursion to write the same long factorial (long n) and test it with the same program.
|
// The "Ass1s2p2" class.
import java.awt.*;
import hsa.Console;
public class Ass1s2p2
{
static Console cs = new Console ();
public static void main (String [] args)
{
cs.print ("Enter a number (<0 to stop): ");
int num = cs.readInt ();
while (num >= 0)
{
cs.println ("\n" + num + "! = " + factorial (num));
cs.print ("\nEnter a number (<0 to stop): ");
num = cs.readInt ();
}
cs.println ("good bye!");
} // main method
public static long factorial (long n)
{
if (n < 2)
return 1;
return n * factorial(n - 1);
}
} // Ass1s2p2 class
|
Modify a STRING: Ass1s2p5.java 
|
Create the method String expand( String word) that will insert minus signs between letters. For example, expand ("automobile") to: "a-u-t-o-m-o-b-i-l-e"
Single letter words are clearly not affected by expand. Use recursion.
|
// The "Ass1s2p5" class.
import java.awt.*;
import hsa.Console;
public class Ass1s2p5
{
static Console cs = new Console ();
public static void main (String [] args)
{
cs.print ("Enter a word (type \"stop\" to exit): ");
String word = cs.readString ();
while (!word.equalsIgnoreCase ("stop"))
{
cs.println ("\"" + word + "\" becomes \"" + expand (word) + "\"");
cs.print ("\nEnter a word (type \"stop\" to exit): ");
word = cs.readString ();
}
cs.println ("good bye!");
} // main method
public static String expand (String word)
{
int len = word.length ();
if (len < 2)
return word;
String shortWord = word.substring (0, len - 1);
return expand (shortWord) + "-" + word.charAt (len - 1);
}
} // Ass1s2p5 class
|
Greatest Common Divisor: Ass2s2p6.java 
|
The greatest common divisor of (a and b) is the same as the greatest common divisor of (b and c) where c is the remainder, when a is divided by b. Since the remainder of any division is always smaller than the original divisor (i.e. c < b), by repeating the argument, the second term will eventually become so small it is zero:
- gcd(a,b) = gcd(b,c) = ... gcd(d,0) where the greatest common divisor is d.
- Write int gcd (int a, int b) that will find the greatest common divisor of a and b using recursion.
|
// The "Ass2s2p6" class.
import java.awt.*;
import hsa.Console;
public class Ass2s2p6
{
static Console cs = new Console ();
public static void main (String [] args)
{
int a, b;
cs.print ("enter two integers separated by space (enter 0 to stop): ");
a = cs.readInt ();
while (a != 0)
{
b = cs.readInt ();
cs.println ("The greatest common divisor of " + a + " and " + b + " is " + gcd (a, b));
cs.print ("enter two integers separated by space (enter 0 to stop): ");
a = cs.readInt ();
} //end while
cs.println ("bye");
} // main method
static int gcd (int a, int b)
{
if (b == 0)
return a;
return gcd (b, a % b);
} //end repeat
} // Ass2s2p6 class
|
Towers of Hanoi: Ass2s2p8.java 
|
Write a program that will print out the solution of how to move the disk in the "Towers of Hanoi" problem from one pole to another, using recursion, given the number of disks to start: e.g.:
- Here are examples of the 1,2 and 3 disk case.
|
// The "Ass2s2p8" class.
import java.awt.*;
import hsa.Console;
public class Ass2s2p8
{
static Console cs = new Console ();
public static void main (String [] args)
{
double a, b, c, d;
cs.println ("The Tower of Hanoi Problem");
cs.print ("enter the number of disks ");
int n = cs.readInt ();
hanoi (n, 1, 3);
cs.println ("bye");
} // main method
static void hanoi (int num, int from, int to)
{
int middle = 6 - from - to;
if (num == 1)
cs.println ("Move disk from " + from + " to " + to);
else
{
hanoi (num - 1, from, middle);
hanoi (1, from, to);
hanoi (num - 1, middle, to);
}
}
} // Ass2s2p8 class
|
|
Sponsored by ECOO and SIG-Computer Science
.
|
|
|