Big Java Late Objects
Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
Question
Book Icon
Chapter 18, Problem 11PE
Program Plan Intro

Generic Permutation Generator

Program plan:

Filename: “PermutationGenerator.java”

This code snippet creates a class “PermutationGenerator”. In the code,

  • Import the required packages.
  • Define a generic class “PermutationGenerator”.
    • Declare a “List” named “list”.
    • Define the constructor “PermutationGenerator ()”.
      • Set the “list”.
    • Define the method “getPermutations()”.
      • Create an array list “result”.
      • If “list.size()” is equal to zero,
        • Add “list” to result.
        • Return “result”.
          • Iterate “for” loop,
            • Create an array list named “shorterList” of type “List<E>”.
            • Remove an element “i” from “shorterList”.
            • Generate permutations of “shorterList” and save to “shorterPermutationGenerator”.
            • Create an array list “shorterListPermutations”.
            • Iterate a “for” loop,
              • Create an array list named “lst” of type “List<E>”.
              • Add element “i” of “list” to “lst”.
              • Add elements “l” to “lst”.
              • Add “lst” to “result”.
          • Return “result”.

Filename: “PermutationTester.java”

This code snippet creates a class “PermutationTester”. In the code,

  • Import the required packages.
  • Define a class “PermutationTester”.
    • Define the “main” method.
    • Define a “ArrayList” named “list”.
    • Add elements to “list” using the method “add()”.
    • Create an object “generator” of “PermutationGenerator”.
    • Define another array list “permutations” to store the permutations of “list”.
    • Print the first permutation from array list “permutations”.
    • Print the expected result.
    • Print the second permutation from array list “permutations”.
    • Print the expected result.
    • Print the third permutation from array list “permutations”.
    • Print the expected result.
    • Print the size of “permutations”.
    • Print the expected size.

Blurred answer
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education