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 it statement to put thetations that checks for the cases where an empty ist must be returned, and return the empty rotations ist. 2. Implement Switch A search typically involves moving from one possibility to another (for example, moving from one die in the Bogole game to another). Doing so involvet 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 reviot 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 booleant 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 Bopole 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 tho Boggle 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 poed to Implement canFollow()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, filing a similar role to findWords () method in the Boogle game. write the search by completing getFixedrotation), and adding a for loop to getal Rotation) that calls getFixedRotation for each of the vegetables in crops. adding the vegetable to used before calling, 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 enum Group { LEGUME, BRASSICA, ALLIUM, FRUITING } public static class Vegetable { String name; Group group; public Vegetable(String name, Group group) { this.name = name; this.group = group; }
* Get all 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 once. * Crops must be planted in order of their Group according to the following * rules: *- a LEGUME may only be followed by a BRASSICA; * -a BRASSICA may only be followed by an ALLIUM; * - an ALLIUM may only be followed by a FRUITING crop; and * -a FRUITING crop may only be followed by a LEGUME. * <p> * For example, the call * getAllRotations ( [Tomato (fruiting), Onion (allium)], 2) * returns a set containing a single rotation: * - [Onion (allium), Tomato (fruiting) * because an ALLIUM may only be followed by a fruiting crop. * <p> * The calz * getAllRotations ( [Tomato (fruiting), Kale (brassica), Onion (allium), Pea (legume)], 4) * returns a set containing four rotations: [Kale (brassica), Onion (allium), Tomato (fruiting), Pea (legume) [Onion (allium), Tomato (fruiting), Pea (legume), Kale (brassica)] [Pea (legume), Kale (brassica), Onion (allium), Tomato (fruiting) [Tomato (fruiting), Pea (legume), Kale (brassica), Onion (allium)] * <p> * If no valid crop rotation can be found, an empty list is returned. * For example, the call: * getal Rotations ( Tomato (fruiting), Gai Lan (brassica)], 2) * returns an empty set, because a FRUITING crop cannot be followed by * a BRASSICA, and a BRASSICA cannot be followed by a FRUITING crop. *- *- 2 * @param crops the set of vegetable crops from which to construct a rotation * @param seasons the number of seasons * @return the set of all possible rotations of the provided crops for the
* 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 Set<List<Vegetable>> getAllRotations (Set<Vegetable> crops, int seasons) { List<Vegetable> used = new ArrayListo(); W vegetables used so far in a given search Set<List<Vegetable>> 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; // FIXME complete this method } * Recursive method to find all rotations given a starting crop. * @param crops as above * @param seasons as above * @param used crops already used in the order in which they are used) * @param rotations all rotations found so far. */ private static void getFixed Rotation (Set<Vegetable> crops, int seasons, List<Vegetable> used, SetsList<Vegetable>> rotations) { // FIXME complete this method } * Determine whether one vegetable can follow another * @param first the first vegetable @param next the next vegetable * @return true if next can follow first private static boolean canFollow(Vegetable first, Vegetable next) { return false; // FIXME 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!