Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the following functions using recursion: // Print out \'n\' *\'s on the sa

ID: 3821593 • Letter: W

Question

write the following functions using recursion:

// Print out 'n' *'s on the same line

printStars(int n);

// Return the minimum of the array

min(int[] numbers));

// Return Yes if pattern is found in target, No otherwise

isIn(String pattern, String target);

// Print all the permutations of word

permutations(String word);

// Return the number of permutations of word

countNumberOfPerms(word);

The above methods must be recursive or they may do some non-recursive stuff and make a call to a recursive function that actually performs the computation.

..

the main is (don't change the mian):

public class A8

{

   public static void main(String[] args)

   {

   A8Utils utils = new A8Utils();

   utils.printStars(0);

   utils.printStars(5);

  

   int[] numbers = { 41, 0, 74, -1, 8, 37, 79, 5, 22, -17 };

   System.out.println("Minumum is " + utils.min(numbers));

  

   String[] patterns = { "al ", "Sta", "eat", "eac" };

   for(int ii = 0; ii < patterns.length; ++ii)

   {

   System.out.println("Is pattern '" + patterns[ii] + "' in 'Cal State Long Beach'? " + utils.isIn(patterns[ii], "Cal State Long Beach"));

   }

  

   utils.permutations("1234");

   utils.permutations("ios");

   utils.countNumberOfPerms("LongBeach");

   }

}

the output :

0 stars:
5 stars: *****
Minumum is -17
Is pattern 'al ' in 'Cal State Long Beach'? Yes
Is pattern 'Sta' in 'Cal State Long Beach'? Yes
Is pattern 'eat' in 'Cal State Long Beach'? No
Is pattern 'eac' in 'Cal State Long Beach'? Yes
The 24 permutations of 1234 sorted are:
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321
The 6 permutations of ios sorted are:
ios iso ois osi sio soi
The number of permutations of LongBeach is 362880

Explanation / Answer

-------------------------------------------

A8Utils.java

public class A8Utils
{
   boolean is_first = true;
   static String perm = "";
   static int no_of_perm = 0;

   // Print out 'n' *'s on the same line
   public void printStars(int n) {
       if(is_first) {
           System.out.print( n + " Stars: ");
           is_first = false;
       }
       if(n != 0) {
           System.out.print("*");
           printStars(n-1);
       } else {
           is_first = true;
           System.out.println("");
       }
   }
  
   // Return the minimum of the array
   public int min(int[] numbers) {
       int min = 0;
      
       if(numbers.length > 0)
           min = numbers[0];
          
       for(int i=1; i<numbers.length; i++) {
           if(numbers[i] < min)
               min = numbers[i];
       }
      
       return min;
   }
  
   // Return Yes if pattern is found in target, No otherwise
   public String isIn(String pattern, String target) {
       String is_in = "No";
      
       is_in = target.contains(pattern) ? "Yes" : "No";
      
       return is_in;
   }
  
   // Print all the permutations of word
   public void permutations(String word) {
       no_of_perm = 0;
       perm = "";
       perm = permutations("", word);
       System.out.println("The " + no_of_perm + " permutations of " + word + " sorted are:");
       System.out.print(perm);
   }
  
   private static String permutations(String prefix, String word) {
       int n = word.length();
      
       if (n == 0) {
           perm = perm + prefix + " ";
           no_of_perm ++;
       } else {
           for (int i = 0; i < n; i++)
               permutations(prefix + word.charAt(i), word.substring(0, i) + word.substring(i+1, n));
       }
       return perm;
   }

   // Return the number of permutations of word
   public void countNumberOfPerms(String word){
       perm = "";
       perm = permutations("", word);
       System.out.println(" The number of permutations of " + word + " is " + no_of_perm);
   }
  
}

----------------------------------------------------------

A8.java

public class A8
{
   public static void main(String[] args)
   {
       A8Utils utils = new A8Utils();
       utils.printStars(0);
       utils.printStars(5);

       int[] numbers = { 41, 0, 74, -1, 8, 37, 79, 5, 22, -17 };
       System.out.println("Minumum is " + utils.min(numbers));

       String[] patterns = { "al ", "Sta", "eat", "eac" };
       for(int ii = 0; ii < patterns.length; ++ii)
       {
           System.out.println("Is pattern '" + patterns[ii] + "' in 'Cal State Long Beach'? " + utils.isIn(patterns[ii], "Cal State Long Beach"));
       }

       utils.permutations("1234");
       utils.permutations("ios");
       utils.countNumberOfPerms("LongBeach");
   }
}

/*
0 Stars:
5 Stars: *****
Minumum is -17
Is pattern 'al ' in 'Cal State Long Beach'? Yes
Is pattern 'Sta' in 'Cal State Long Beach'? Yes
Is pattern 'eat' in 'Cal State Long Beach'? No
Is pattern 'eac' in 'Cal State Long Beach'? Yes
The 24 permutations of 1234 sorted are:
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 The 6 permutations of ios sorted are:
ios iso ois osi sio soi
The number of permutations of Long is 362880
*/