Recursive method to print all permutations of a string, JAVA Programming

Assignment Help:

Objectives

_ Creating and compiling a simple program

_ Designing classes

_ Use of recursive algorithms

_ Learn basic combinatorics  

Task: string permutation

Write a recursive method to print all permutations of a string. For example, for a string "abc", the output would be:

abc

acb

bac

bca

cab

cba

Write a program to test your method.

The program should prompt the user to enter a string. Then the program should display all the possible permutations. You can assume that all characters in the input string are different.

Hint:

Declare the following two helping methods:

void displayPermutations(String text)

and

void displayPermutations(String done, String todo)

The first method invokes displayPermutations( "", text ). The second method uses a loop to move a character from todo to done and recursively invokes it with a new todo and done. The base case is when todo string is empty. In this case the method prints done

Example

Note:

user entered values are shown in bold.  

Example 1

This program prints out all permutations of a string.

Enter a string? dome

There are the following permutations:

dome

doem

dmoe

dmeo

deom

demo

odme

odem

omde

omed

oedm

oemd

mdoe

mdeo

mode

moed

medo

meod

edom

edmo

eodm

eomd

emdo

emod

Example 2

This program prints out all permutations of a string.

Enter a string? one

There are the following permutations:

one

oen

noe

neo

eon

eno  

Important Notes

1. For the purposes of this assignment, you can assume that the user will only enter  valid values.

2. In your program you must define and make use of at least the two methods:

a. displayPermutations(String text) and

b. displayPermutations(String done, String todo) as described above.

3. Test your program. In particular, be sure to test it on turing before you submit it.


Related Discussions:- Recursive method to print all permutations of a string

write a junit test suite, Objective The objective of this lab exercis...

Objective The objective of this lab exercise is to develop a unit test suite using JUnit Specification of the Program to be Tested You are given the source code of a Java clas

What is the role of fonts in java explain with example, What is the role of...

What is the role of fonts in java explain with example? You've already seen one instance of drawing text in the HelloWorldApplet program of the last chapter. You call the drawS

How the jms is different from rpc, In RPC the method invoker waits for the ...

In RPC the method invoker waits for the method to finish implementation and return the control back to the invoker. Therefore it is completely synchronous in nature. Whereas in JMS

List the java class library, list The Java Class Library? Java holds an...

list The Java Class Library? Java holds an extensive library of pre-written classes you can use in your programs. These classes are separated into groups known as packages. Th

How to write exception subclasses, How to Write Exception Subclasses ? ...

How to Write Exception Subclasses ? Most exception subclasses inherit all their functionality from the superclass. Each subclass majorly serves as a marker for a various types

Write down the class electricitysource, Question : (a) Explain the con...

Question : (a) Explain the concept of polymorphism when used in programs using suitable examples of your own. (b) (i) Distinguish between abstract methods and non-abstrac

Write a java code to draw ovals and circles, Write a java code to draw Oval...

Write a java code to draw Ovals and Circles? Java has techniques to draw outlined and filled ovals. As you'd probably guess these methods are called drawOval() and fillOval() c

How to retrieve warnings, SQLWarning objects are a subclass of SQLException...

SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the implementation of an application, as exceptions do; they easily

Explain intelligent system, What is an intelligent system? Intelligent sy...

What is an intelligent system? Intelligent systems are the programs established to perform complicated jobs in which reflect human brain and thoughts. If the algorithms are too m

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd