Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Task: Please help me with two implementations in this small program called Orders. Below is an intro of this task. Please read and help

Java Task:

Please help me with two implementations in this small program called "Orders". Below is an intro of this task. Please read and help me if you can. Thank you

Here is the introduction

Once Upon a Time in the Mushroom Kingdom

Mario and Luigi's Super Italian Bistro is looking to drum up more business during week nights. The brothers propose to attract more customers by offering special discounts on Monday to Thursday night. They have several menu items on which they can offer a discount or simply deduct some money from the overall bill of their customers. This leads to 5 total specials the brothers could offer.

10 Coins Off Crushed Turtle Firey Flower Pasta Mushroom Veal Stewed Goomba 

The brothers are not sure what the most effective order of specials would be to boost business so begin discussing the possible orderings. The brothers would like to offer some sort of special from Monday to Thursday, on 4 nights. This leads to several possible orderings of the specials. For example, one ordering of specials from Monday to Thursday is:

Crushed Turtle Firey Flower Pasta Mushroom Veal 10 Coins Off 

Note that the special Stewed Goomba does not get used in this ordering.

Initially the brothers see no reason that specials cannot repeat, so another possible ordering is the following.

Firey Flower Pasta Crushed Turtle Firey Flower Pasta Crushed Turtle 

3 All Possible Orderings with Replacement

The brothers become curious how many arrangements of the specials there are. While Mario begins busily writing out orders by hand, Luigi fires up his computer to write some code for the problem. (Before the brothers started their bistro, Mario spent quite a bit of time punching bricks, eating mushrooms, and chasing princesses while Luigi always seemed to be second choice, giving him time to pursue more intellectual topics).

After reviewing recursion, Luigi produces the following code which will list all possible orderings with replacement.

// 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; } 

Note: this function is included in the lab code pack in Orders.java. You will need to understand it and create several variants.

Luigi is quite proud of this method as it will work for not just 4 nights of specials, but any number of nights of specials. He simply needs to change the maxSize parameter which controls how long the currentOrder should be before adding it to the result list ofallOrders.

Luigi codes a short main() method and is a bit shocked when it produces 625 possible orderings of the specials.

> javac Orders.java > java Orders 625 orders [10 Coins Off, 10 Coins Off, 10 Coins Off, 10 Coins Off] [10 Coins Off, 10 Coins Off, 10 Coins Off, Crushed Turtle] [10 Coins Off, 10 Coins Off, 10 Coins Off, Firey Flower Pasta] [10 Coins Off, 10 Coins Off, 10 Coins Off, Mushroom Veal] ... [Stewed Goomba, Mushroom Veal, Firey Flower Pasta, Crushed Turtle] 

The brothers agree this is far too many orderings to try. They disagree as to how to limit the possibilities to try though. Mario thinks that they should consider all orders with no adjacent repeats while Luigi feels they should use all orders with no repeats. The brothers are struggling to adapt their code to solve these problems so they turn to a talented young computer scientist for help.

----Here is the task----

All Orders No Adjacent Repeats

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 allOrders) 

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 allOrders 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.

Firey Flower Pasta Crushed Turtle Firey Flower Pasta 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.

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.

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.

5 All Orders No Repeats

In the class Orders.java write the following method.

// 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) 

This method determines all orderings of items in specials which do not contain any repeats. Each order is maxSize long and the results are stored as strings in allOrders.

These orders are NOT allowed this time, as they contain repeated elements.

Firey Flower Pasta Crushed Turtle Firey Flower Pasta Crushed Turtle 10 Coins Off Crushed Turtle Mushroom Veal 10 Coins Off Crushed Turtle Crushed Turtle Mushroom Veal Stewed Goomba 

The following orders ARE allowed, as they contain no repeats.

Crushed Turtle Firey Flower Pasta Mushroom Veal Stewed Goomba Crushed Turtle 10 Coins Off Firey Flower Pasta Mushroom Veal Stewed Goomba Firey Flower Pasta Mushroom Veal Crushed Turtle 

Keep the following in mind as you code this method:

Again, start with the definition of orders() which is provided. This is very close to what you need but will require some tweaks to exclude duplicates.

The currentOrder list contains a list that can have items added to it. Before adding a new item, check if currentOrder already contains that item. There may be some methods of ArrayList which make this a trivial task.

---------Please solve the task from below-----------

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); } }

}

---------Tester cases-------

Please open the link for the tester because it is a little long

https://paste.ee/p/WWKB2

Thank you so much.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions