Source code

Towers of hanoi.

/**
 * TowersOfHanoi.java
 * Created by Stijn Strickx on Aug. 12 2006
 * Copyright 2006 Stijn Strickx, All rights reserved
 */

import java.io.*;

public class TowersOfHanoi {

 static int moves = 0;
 static int totalDisks = 0;
 
 public static void main(String[] arguments) throws java.io.IOException {
  int disks;
  char fromPole = 'A';
  char withPole = 'B';
  char toPole = 'C';
  disks = getNumber("\nHow many disks are there on the tower? ");
  totalDisks = disks;
  if(totalDisks > 10){
   System.out.println("Working...");
   }
  FileOutputStream fos = new FileOutputStream("TowersOfHanoiSolution.txt");
  PrintStream ps = new PrintStream(fos);
  solveHanoi(disks, fromPole, toPole, withPole, ps);
  ps.close();
  System.out.println();
  System.out.println("\nAmount of moves: " + moves + "\n");
  System.out.print("You can now access the TowersOfHanoiSolution.txt file");
  System.out.println(" which is in the same directory as the .class file of this program.");
  }
 
 static void solveHanoi(int disks, char fromPole, char toPole, char withPole, PrintStream ps) {
  if (disks >= 1) {
   solveHanoi(disks-1, fromPole, withPole, toPole, ps);
   moveDisk(fromPole, toPole, ps);
   solveHanoi(disks-1, withPole, toPole, fromPole, ps);
   }
  }
  
 static void moveDisk(char fromPole, char toPole, PrintStream ps) {
  moves++;
  if(totalDisks <= 10){
   System.out.print("Move from " + fromPole + " to " + toPole + ". ");
   ps.print("Move from " + fromPole + " to " + toPole + ". ");
   if (moves%4 == 0){
    System.out.println();
    ps.println();
    }
   }
   else {
   ps.print("Move from " + fromPole + " to " + toPole + ". ");
   if (moves%4 == 0){
    ps.println();
    }
   }
  }
 
 static int getNumber(String question) throws java.io.IOException {
  String theNumber;
  int number = 0;
  BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
  System.out.print(question);
  theNumber = in.readLine();
  System.out.println();
  number = Integer.parseInt(theNumber);
  return number;
  }
 }

Sumber pustaka : http://www.proglogic.com/code/java/puzzle/towersofhanoi.php


LINKED LIST.



INPUT :
package linkedlist;
class simpul {
    int data;
    simpul next;
    simpul (int x){
        data = x;
    }
    public void tampil (){
        System.out.println (data);
    }
}

/**
 *
 * @author lucky
 */





public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        simpul s1 = new simpul (90);
        simpul s2 = new simpul (35);
        simpul s3 = new simpul (20);
        simpul s4 = new simpul (65);
        simpul s5 = new simpul (10);
        System.out.println ("Program Linked List");
        s1.next=s2;
        s2.next=s3;
        s3.next=s4;
        s4.next=s5;
        simpul temp=s1;
        for (;temp!=null;temp=temp.next)
        {
            temp.tampil();
        }

    }
}
OUTPUT :
run:
Program Linked List
90
35
20
65
10
BUILD SUCCESSFUL (total time: 1 second)

0 komentar:

Posting Komentar