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

In JAVA please define the following 2 recursion methods

In JAVA please define the following 2 recursion methods In the class

In the class Orders.java write the following method.

Orders.java write the following method. Here is the Java code for class

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 specials, ArrayList currentOrder, int maxSize, ArrayList 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 specials, ArrayList currentOrder, int maxSize, ArrayList 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 specials, ArrayList currentOrder, int maxSize, ArrayList allOrders){ // IMPLEMENT ME return; }

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

ArrayList currentOrder = new ArrayList(); ArrayList allOrders = new ArrayList(); 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 allrders) 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!