public class Factorial { public int fact(int num) { int temp; // base case: if (num == 1) return 1; else { temp = num * fact(num - 1); return temp; } } public static void main(String[] args) { Factorial f = new Factorial(); int result = f.fact(5); System.out.println("result: " + result); } } --------------------------------------------------------- // find the sum of the first n positive integers public class Sum { public int findSum(int num) { // base case: if (num == 1) return 1; else return num + findSum(num - 1); } public static void main(String[] args) { Sum s = new Sum(); // remember Gauss! int result = s.findSum(100); System.out.println("result: " + result); } } --------------------------------------------------------- import javax.swing.JOptionPane; public class Triangle { private int width; public Triangle(int a_width) { width = a_width; } public int getArea() { if (width == 1) return 1; Triangle tri2 = new Triangle(width - 1); int area2 = tri2.getArea(); return width + area2; } public static void main(String[] args) { String widthStr = JOptionPane.showInputDialog("Triangle width?"); int width = Integer.parseInt(widthStr); Triangle tri = new Triangle(width); int area = tri.getArea(); String msg = "Triangle with width " + width; msg += " has an area of " + area; System.out.println(msg); } }