Professor Kit Romano's Web Site

 



Site Index

About Me

Welcome
Definition of Program
Variable Types
Understanding Memory
Declaring Variables
Simple Commands
Concatenation and Strings
Sleep-Threads-Try Catch
Campus Scenes
Pictures
Java simplified
Application Shell Styles
Reading Data From File
Overview of Applets
Excellent Graphics Applet
Online Reference
Nested if statements
Arrays
Sorting Arrays
Using Strings
Functions
Top 20 Replies 
First program 211
Important Class Example
JAVA in SDK Environment
Understanding OOP
OOP simple example
Recursion example
Example of Inheritance
     and use of JTextArea



*** Web Related **********
Build Student Web Page ?
What is FTP
Download WS ftp95
Download Java Plug In
Applets from jbuilder to sdk
Practice Problems

 

Programming Examples

 

 

                                                

 

Recursion example

// TOWER of HANOI PROBLEM

// Triple level recirsion (difficult to follow)

// 3 pegs with n disks on the first peg  (smallest on top  largest on bottom)
// move disks one at a time till smallest on top and largest on the bottom of 3rd peg
// when moving a larger disks cannot rest on top of smaller disk
// example of recursion for the tower of hanoi problem
// complicated --  keep as a thought exercise when you are bored

 

import java.lang.*;
import javax.swing.*;
public class Application1
{
public static void main(String[] args)
  {
  int n=3;    // number if disks
  function(n,"left","right","middle");
  JOptionPane.showMessageDialog(null,"finished");
  }
public static void function(int n,String source,String destination,String extra)
 {
  if(n == 1)
   {
    System.out.println("move disk from "+ source +"  to "+destination);
   }
    else
   {
    function(n-1,source,extra,destination);
    function(1,source,destination,extra);
    function(n-1,extra,destination,source);
   }
 }
}