Skip to Content
Java How to Program, Early Objects, 11th Edition
book

Java How to Program, Early Objects, 11th Edition

by Paul J. Deitel, Harvey Deitel
June 2017
Beginner
1296 pages
69h 23m
English
Pearson
Content preview from Java How to Program, Early Objects, 11th Edition
  1. ... peg 1 as a temporary holding area.

The process ends when the last task involves moving n=1 disk (i.e., the base case). This task is accomplished by moving the disk, without using a temporary holding area.

In Fig. 18.11, method solveTowers (lines 5–22) solves the Towers of Hanoi, given the total number of disks (in this case 3), the starting peg, the ending peg, and the temporary holding peg as parameters.

Fig. 18.11

 1   // Fig. 18.11: TowersOfHanoi.java
 2   // Towers of Hanoi solution with a recursive method.
 3   public class TowersOfHanoi {
 4      // recursively move disks between towers
 5      public static void solveTowers(int disks, int sourcePeg,
 6         int destinationPeg, int tempPeg) {
 7         // base case -- only one disk to move
 8         if (disks == 1) {
 9 System.out.printf( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17

Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17

Kishori Sharan, Adam L. Davis
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Java in a Nutshell, 8th Edition

Java in a Nutshell, 8th Edition

Benjamin J. Evans, Jason Clark, David Flanagan

Publisher Resources

ISBN: 9780134751962