PermutationsCalculator class (in java) : create a field which is an unbounded st
ID: 3587083 • Letter: P
Question
PermutationsCalculator class (in java) :
create a field which is an unbounded stack; use the stack implementation from chapter 2 in the text
create a method with a String parameter which will calculate all the permutations of the parameter; store the permutations in a list and return the list
stack.java
public class stack <T>
{
private int top = -1;
private T array[];
private final int CAPACITY = 10;
public stack()
{
array = (T[]) new Object[CAPACITY];
}
public stack(int capacity)
{
array = (T[]) new Object[capacity];
}
public void push(T element)
{
if(isFull())
{
System.err.println("the stack is full.");
}
else
{
top++;
array[top] = element;
}
}
public void pop()
{
if(isEmpty())
{
System.err.println("the stack is empty.");
}
else
{
array[top] = null;
top--;
}
}
public T peek()
{
T topofstack = null;
if(isEmpty())
{
System.err.println("the stack is empty.");
}
else
topofstack = array[top];
return topofstack;
}
public boolean isEmpty()
{
return (top == -1);
}
public boolean isFull()
{
return (top == (array.length - 1));
}
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
class stack <T>
{
private int top = -1;
private T array[];
private final int CAPACITY = 1000;
public stack()
{
array = (T[]) new Object[CAPACITY];
}
public stack(int capacity)
{
array = (T[]) new Object[capacity];
}
public void push(T element)
{
if(isFull())
{
System.err.println("the stack is full.");
}
else
{
top++;
array[top] = element;
}
}
public void pop()
{
if(isEmpty())
{
System.err.println("the stack is empty.");
}
else
{
array[top] = null;
top--;
}
}
public T peek()
{
T topofstack = null;
if(isEmpty())
{
System.err.println("the stack is empty.");
}
else
topofstack = array[top];
return topofstack;
}
public boolean isEmpty()
{
return (top == -1);
}
public boolean isFull()
{
return (top == (array.length - 1));
}
public int size()
{
return top+1;
}
}
public class PermutationsCalculator {
public static stack<String> permutationgenerator(String str) {
stack<String> perm = new stack<String>();
//Handling error scenarios
if (str == null) {
return null;
} else if (str.length() == 0) {
perm.push("");
return perm;
}
char initial = str.charAt(0); // first character
String rem = str.substring(1); // Full string without first character
stack<String> words = permutationgenerator(rem);
int n=words.size();
for (int j=0;j<n;j++) {
String strNew = words.peek();
words.pop();
for (int i = 0;i<=strNew.length();i++){
perm.push(charInsert(strNew, initial, i));
}
}
return perm;
}
public static String charInsert(String str, char c, int j) //generating string..
{
String begin = str.substring(0, j);
String end = str.substring(j);
return begin + c + end;
}
public static void main(String argv[])
{
String s = "abc";
stack<String> list = permutationgenerator(s);//list of permutaions...
int i=0,n=list.size();
System.out.println("Permutations of "+s+":");
while(i<n)
{
System.out.println(list.peek());//printing the permutaions list..
list.pop();
i++;
}
}
}
output:-
run:
Permutations of abc:
bca
bac
abc
cba
cab
acb
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.