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

In JAVA please define the following 2 recursion methods In the class Orders.java

ID: 3709346 • Letter: I

Question

In JAVA please define the following 2 recursion methods

In the class Orders.java write the following method.

Here is the Java code for class Orders

import java.util.*;
public class Orders{

// Produce all possible orders of specials with replacement;
// currentOrder is the current order of specials and maxSize is
// the maximum length desired. allOrderings accumulates string
// results as they are found.
public static void orders(ArrayList<String> specials,
                            ArrayList<String> currentOrder,
                            int maxSize,
                            ArrayList<String> allOrders){
    // If currentOrder contains enough specials, add it to the list of
    // allOrders that have been found
    if(currentOrder.size() == maxSize){
      allOrders.add(currentOrder.toString());
      return;
    }

    // Haven't reached maxSize so add each possible special to the
    // end of allOrders and recurse down to continue the
    // search. Remove the special after finishing the recursive call
    // to replace it with another special.
    for(String special : specials){
      currentOrder.add(special);
      orders(specials, currentOrder, maxSize, allOrders);
      currentOrder.remove( currentOrder.size()-1 );
    }
    return;
}

// Produce all possible orders of specials with replacement but
// ensure that no adjacent specials are identical (no adjacent
// repeats).
public static void ordersNoAdj(ArrayList<String> specials,
                                 ArrayList<String> currentOrder,
                                 int maxSize,
                                 ArrayList<String> allOrders){
    // IMPLEMENT ME
    return;
}

// Produce all possible orders of specials WITHOUT replacement: each
// special in an order in allOrders should be unique.
public static void ordersNoRepeats(ArrayList<String> specials,
                                     ArrayList<String> currentOrder,
                                     int maxSize,
                                     ArrayList<String> allOrders){
    // IMPLEMENT ME
    return;
}

public static void main(String args[]){
    ArrayList<String> specials = new ArrayList<String>();
    specials.add("10 Coins Off");
    specials.add("Crushed Turtle");
    specials.add("Firey Flower Pasta");
    specials.add("Mushroom Veal");
    specials.add("Stewed Goomba");

    ArrayList<String> currentOrder = new ArrayList<String>();
    ArrayList<String> allOrders = new ArrayList<String>();
    int maxSize = 4;
    orders(specials, currentOrder, maxSize, allOrders);

    System.out.printf("%d orders ",allOrders.size());
    for(String order : allOrders){
      System.out.println(order);
    }


    System.out.println();

    // Now without adjacent repeats
    allOrders.clear();
    currentOrder.clear();
    ordersNoAdj(specials, currentOrder, maxSize, allOrders);

    System.out.printf("%d orders ",allOrders.size());
    for(String order : allOrders){
      System.out.println(order);
    }

    System.out.println();

    // Now without any repeats
    allOrders.clear();
    currentOrder.clear();
    ordersNoRepeats(specials, currentOrder, maxSize, allOrders);

    System.out.printf("%d orders ",allOrders.size());
    for(String order : allOrders){
      System.out.println(order);
    }
  
}

}

In the class Orders.java write the following method. // Produce all possible orders of specials with replacement but // ensure that no adjacent specials are identical (no adjacent // repeats). public static void ordersNoAdj (ArrayList specials, ArrayList currentorder, int maxSize, ArrayList allörders) This method determines all orders of the elements in specials with replacement except those that contain adjacent elements that are identical. The results are stored in al 10rders and each result order is maxSize elements long. For example. if maxSize=4. the following is a possible ordering: though it contains repeats, the repeated items are not adjacent. irey Elower Pasta Crushed Turtle Crushed Turtle However, the following order would not be accepted as it contains adjacent equal items Crushed Turtle 10 Coins off 10 Coins Off Mushroom Veal The ordering need not contain any repeats so the following is also a viable order Crushed Turtle Firey Flower Pasta Mushroom Veal Stewed Goomba Keep the following in mind as you code this method. e Start with the definition of orders ) which is provided. This is very close to what you need but will require some tweaks to exclude adjacent repeats The currentOrder parameter is a list that can have items added to it. If items are added at the end via the add ) method, one can prevent adjacent items from being equal by not adding items that are equal to the last element in the list. e Make sure while checking the last element that it exists: currentOrder might be empty. . At the end of the method execution, the allOrders list should contain string representations of all possible orders without replacement.

Explanation / Answer

Here is complete code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote