1. Handle Simple Cases It is a good strategy to identity and deal with simple cases first. In this problem, notice that there are a number of circumstances where you should return an empty list. You should address this simple case first Add an if statement to getAllRotations that checks for the cases where an empty list must be returned, and return the empty rotations list 2. Implement Switch A search typically involves moving from one possibility to another (for example, moving from one die in the Boggle game to another). Doing so involves at least two considerations: a) ensuring that the possibility you're considering is not one you've already considered if you don't do this, your search will never complete because you will continuously revisit cases you've already encountered), and b) ensuring that you only consider legal cases. Ensuring that you don't consider the same case twice was addressed in the Boggle game by using an array of booleans called used, and the code carefully kept track of which dice were already considered in the current search (you may want to inspect the findwords() method in the Boggle game to see how this was done). In this problem you've been given a List of Vegetables called used, which you should use in a similar way, adding vegetables to the list when they're being considered, and checking whether they're already in the set before you try to use them. In the Bogole game, checking for legal cases was doing by using a set of neighbours, so that we knew for each die which were its legal neighbours. In this case you need to implement canfollowe), which will allow your code to determine whether a particular crop is a legal follower of the current crop Use a switch statement to complete the canFollow() method, returning the correct value according the the vegetable groups of the first and next vegetables, 3. Complete the Search You should notice that the findwords () method in the Boggle game is not terribly complex once the other parts are in place. You now need to write the body of getFixedRotation), which will be recursive, Filling a similar role to findwords() method in the Boggle game. Write the search by completing getFixedrotation), and adding a for loop to getAllRotations() that calls getFixedrotation) for each of the vegetables in crops, adding the vegetable to used before caling, and removing it from used afterwards.
import java.util.ArrayList; import java.util.HashSet: import java.util.List: import java.util.Set; public class CropRotation * Each Vegetable is assigned to one of four groups. public enun Group LEGUME, BRASSICA, ALLIUM, FRUITING > public static class Vegetable { String nane; Group group: public Vegetable(String name, Group group) { this.name = name; this group group: ) @Override public String toString() { return name + " " .group.nane().toLowerCase() + ")"; )
- Get alt valid crop rotations that can be composed from the provided set of vegetable crops for the given number of seasons. A crop rotation is a sequence of vegetable crops to plant. One crop is planted per season, and any crop may be planted at most ance. Craps must be planted in order of their Group according to the following • rules: . LEGUME may only be followed by a BRASSICA - a BRASSICA may only be followed by an ALLIUM: - an ALLIUN may only be followed by a FRUITING Crops and - FRUITING crop my only be followed by a LEGUME . For example, the call .getAtt RotationsTomato (fruiting, Onion fallium)], 2) returns a set containing a single rotation: - Onion (alt.), Tonate (fruiting) because an ALLIUM may only be followed by a fruiting crop. The call getAllotations Tomato fruiting), Kate (brassica), Onion (alli), Pes (legume), 4) • returns a set containing four rotations - Kale (brassica), Onion (ollo), Yonato (Fruiting), Poo (legune) - Onion (alt), Tomato fruiting), Pea legume), Kale (brassical - (Poa (legume), Kale (brassica), Onion (allium), Tomato (fruiting) - Ionato fruiting), Pea legume), Kale (brassica), Onion (allium) • sp • If no valid crop rotation can be found, an empty list is returned - For example, the call getAllotations/Tonato (Frusting), Gal Lan (Brassica), 2) returns an empty set, because a FRUITING Crap cannot be followed by • BRASSICA, and a BRASSICA cannot be followed by a FRUITING crop para crops the set of vegetable crops from which to construct a rotation para seasons the number of seasons dreturn the set of all possible rotations of the provided crops for the Anushoc SANSARSA than a ARSO ROSSO hennar
given number of seasons. If there are no crops or no seasons or the number of seasons is greater than the number of crops, return empty list. public static Setelist<Vegetable>> getAllRotations (SetsVegetables crops, int seasons) { List<Vegetables used - new ArrayListo(); // vegetables used so far in a given search Set<List<Vegetables rotations = new HashSet(); // rotations found so far If there are no crops or no seasons or the number of seasons is greater than the number of crops, return an empty list. return null; // FIXE complete this method > • Recursive method to find all rotations given a starting crop. para crops as above + Oparan seasons as above aparan used crops already used in the order in which they are used) euran rotations all rotations found so far. private static void getFixedRotation(SeteVegetable crops, int seasons, List Vegetables used, Set<List<Vegetable>> rotations) { // FIXE complete this method ) / . Determine whether one vegetable can follow another para first the first vegetable + Oparan next the next vegetable return true if next can follow first private static boolean canFollow (Vegetable first, Vegetable next) { return false; // FIXE complete this method >
1. Handle Simple Cases It is a good strategy to identity and deal with simple cases first. In this problem, notice that
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
1. Handle Simple Cases It is a good strategy to identity and deal with simple cases first. In this problem, notice that
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!