Question

In: Computer Science

Create a class that generates permutations of a set of symbols. Requirements The class must be...

Create a class that generates permutations of a set of symbols.


Requirements


The class must be named PermutationGenerator.


The PermutationGenerator class has two methods as follows.
hasNext
This method has no parameters.  It returns true if at least one permutation remains to be generated.
next
This method has no parameters.  It returns an array of the symbols (char[]) in a permutation (if any remain) or null otherwise.


The following main method MUST be used, with NO CHANGES to test your class.

public static void main(String[] args) { int count = 0; PermutationGenerator pg = new PermutationGenerator(new char[] { 'R', 'E', 'G', 'A', 'L' }); while (pg.hasNext()) { count++; char[] permutation = pg.next(); for (char symbol : permutation) System.out.print(symbol + " "); System.out.println(); } System.out.println("Total permutations = " + count); }


Recursion MUST NOT be used.



Solutions

Expert Solution

Here is the program in java

import java.util.ArrayList;
import java.util.List;

class PermutationGenerator{
List<String> permutation;
int currentIdx = 0;
PermutationGenerator(char word[]) {
  
String s = new String(word);
   // create an empty ArrayList to store partial permutations
       permutation = new ArrayList<>();
  
       // initialize the list with the first character of the string
       permutation.add(String.valueOf(s.charAt(0)));
  
       // do for every character of the specified string
       for (int i = 1; i < s.length(); i++)
       {
           // consider previously constructed partial permutation one by one
           // (iterate backwards to avoid ConcurrentModificationException)
           for (int j = permutation.size() - 1; j >= 0 ; j--)
           {
               // remove current partial permutation from the ArrayList
               String str = permutation.remove(j);
              
               // Insert next character of the specified string in all
               // possible positions of current partial permutation. Then
               // insert each of these newly constructed string in the list
  
               for (int k = 0; k <= str.length(); k++)
               {
                   permutation.add(str.substring(0, k) + s.charAt(i) + str.substring(k));
               }
           }
       }
       //System.out.println(permutation);
       //System.out.println(permutation.size());
}
public char[] next() {
  
if((currentIdx+1) <= permutation.size())
return permutation.get(currentIdx++).toCharArray();
return new char[0];
}
public boolean hasNext() {
return (currentIdx < permutation.size());
}
  
}

public class Main
{
   public static void main(String[] args) {
   int count = 0;
   PermutationGenerator pg = new PermutationGenerator(new char[] { 'R', 'E', 'G', 'A', 'L' });
  
   while (pg.hasNext()) {
   count++;
   char[] permutation = pg.next();
   for (char symbol : permutation)
   System.out.print(symbol + " ");
   System.out.println();
   }
  
   System.out.println("Total permutations = " + count);
  
   }
}

The part of Sample output:

It generate 120 permutations of the word. which is correct , because the length of the array is 5 and fact(5) = 120.


Related Solutions

in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
JAVA: Use existing Java Stack class to solve the "Balancing Symbols" problem. The symbols are (),...
JAVA: Use existing Java Stack class to solve the "Balancing Symbols" problem. The symbols are (), [], and {}, and each opening symbol must have a corresponding closing symbol as well as in correct order. Ignore operands and arithmetic operators since they are not relevant to our problem. You can assume each token is separated by spaces. For example: { ( a + b ) * c1 } – valid { ( a + b ) * c1 ] –...
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Please select a set of five requirements management tools and create a list of features provided...
Please select a set of five requirements management tools and create a list of features provided that support the management aspects of requirements engineering.
Assignment requirements You will be creating your custom Set (think a list of unique elements) class...
Assignment requirements You will be creating your custom Set (think a list of unique elements) class in C++. Please do NOT use STL, or any pre-defined library for this assignment. 1. The data type of the set collection: array (or vector). 2. Create three operations (based on Set ADT descriptions -union() -intersection() -difference() 3. Put some test code in main()
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT