Question

Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...

Need help writing four short recursive programs:

  1. Write a program called Permutations.java that takes an integer command-line argument, n, and enumerates all permutations of the first n letters in the English alphabet. Be sure to handle exceptions of the types: i) bad user input (i.e., double, string), ii) n > 26 or n < 0.

  2. Write a program called PermutationsK.java that takes two integers, n and k, as command-line arguments and prints out all P(n,k) permutations that contain exactly k of the n elements. Note: P(n,k) = (n!)/(n-k)!

    1. Example: when n = 4, and k = 2: ab ac ad ba bc bd ca cb cd da db dc

    2. Be sure to handle the same exceptions as in (1).

  3. Write a program called Combinations.java that takes an integer command line argument, n, and prints all 2n combinations of any size.

    1. Example: when n = 3: a ab abc ac b bc c (this should include the empty string)

    2. Be sure to handle the same exceptions as in (1).

  4. Write a program called CombinationsK.java that makes two integers, n and k, as command-line arguments and prints all C(n,k) combinations of size k. Note: C(n,k) = (n!)/(k!(n-k)!)

    1. Example: when n = 5 and k = 3: abc abd abe acd ace ade bcd bce bde cde

    2. Be sure to handle the same exceptions as in (1).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Permuations.java

public class Permutations {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       String str;
      
       try {
       int n = Integer.parseInt(args[0]);
      
       if(n>0 && n<=26) {
          
           str = getStr(n);
          
          
           variations(str,0,n-1);
          
       }
      
       else
           System.out.println("Number should be between 1 and 26 Inclusive");
      
       }
       catch(NumberFormatException e) {
          
           System.out.println(e);
       }

      
      
      
   }
  
  
   public static String getStr(int n) {
      
       int i=0;
       String str = "";
       while(i<n) {
          
           str = str + (char)(i+65);
          
           i++;
       }
      
       return str;
   }
  
   public static void variations(String str, int m, int n)
{
if (m == n)
System.out.println(str);
else
{
for (int i = m; i <= n; i++)
{
str = mix(str,m,i);
variations(str, m+1, n);
str = mix(str,m,i);
}
}
}
  
   public static String mix(String a, int b, int c)
{
char dum;
char[] array = a.toCharArray();
dum = array[b] ;
array[b] = array[c];
array[c] = dum;
return String.valueOf(array);
}

}

When giving argument as HELLO

Out Put with exception

When Argument is given as 0

The output is

When Given an Valid input 10

Permutationsk.java

public class PermuationsK {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
String str;
      
       try {
       int n = Integer.parseInt(args[0]);
       int k = Integer.parseInt(args[1]);
      
       if(n>0 && n<=26 && k>0 && k<=26 ) {
          
           if(n>=k) {
          
           str = getStr(n);
          
           // Calling the method
           printall(str,k);
          
           }
          
           else
               System.out.println("n value should be greater than or eaqual to k");
       }
      
       else
           System.out.println("Numbers should be between 1 and 26 Inclusive");
      
       }
       catch(NumberFormatException e) {
          
           System.out.println(e);
       }

   }
  
public static String getStr(int n) {
      
       int i=0;
       String str = "";
       while(i<n) {
          
           str = str + (char)(i+65);
          
           i++;
       }
      
       return str;
   }
  
public static void permutationsk(String str,String pre,int n, int k ,int k1,int count) {
  
   if(count<npr(n,k1)) {
   if (k == 0)
   {
      
   System.out.println(pre);
   return;
   }
  
     
   for (int i = 0; i < n; ++i)
   {
     
  
   String newPre = pre + str.charAt(i);
  
     
     
   permutationsk(str, newPre,
   n, k - 1,k1,count);
   count = count + 1;
  
     
   }
  
     
   }
  
   else
       return;
  
}
  
static void printall(String str, int k)
{
int n = str.length();
permutationsk(str, "", n, k,k,0);
}
  
public static int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
  
public static int npr(int n, int r)
{
return factorial(n) / factorial(n - r);
}

}

When giving argument as Hi HELLO

OUTPUT is Like

When given 0output

OUTPUT IS LIKE

When n value is less than k value

OutPut is like

When the input is Valid

OUTPUT IS

Add a comment
Know the answer?
Add Answer to:
Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • java) write a program that takes command line argument and compute the average of their maximum...

    java) write a program that takes command line argument and compute the average of their maximum and minimum. make sure there are command line arguments being passed before you attempt to compute anything

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • [THIS SHOULD BE WRITTEN IN C++] The first argument, usually called argc, tells the program how...

    [THIS SHOULD BE WRITTEN IN C++] The first argument, usually called argc, tells the program how many command line arguments were passed to the program on its invocation. The second argument, argv, is an array of strings containing the arguments themselves. Write a main which prints out the arguments passed to the program. For example, when I run my program like this: ./prog apples bananas oranges it outputs: Argument #0 is ./prog Argument #1 is apple Argument #2 is bananas...

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

  • Write a program call p1.py. Add a function called make_rand_list(n) to your program that takes an...

    Write a program call p1.py. Add a function called make_rand_list(n) to your program that takes an integer n as input and returns a list of n random integers in the range [1,100]. Add another function called product(list) that takes a list of random numbers as an argument and return the product of all the random numbers of that list. Sample run: make_rand_list(7) returns list = [34, 18, 35, 26, 53, 9, 48] product(list) returns 12751240320

  • I need help with a java program Write a program Enigma that takes a single String...

    I need help with a java program Write a program Enigma that takes a single String as a command line argument. Enigma should read the file specified by the String argument, add 5 to each byte, and leave the altered data values in a file whose name is the command line argument. Note that this "updating in place" is the most difficult part of this lab: java Enigma sophie.dat should read file sophie.dat, and upon completion, leave the modified data...

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT