Java代写:CS1101-Hard-Manager

1. Aim

The purpose of this request is to:

  1. Create client files that use the class to model “real world” scenarios.
  2. Instantiate objects, create arrays of objects.
  3. Generate random integers and random Boolean values.
  4. Call methods on objects.

It is recommended that you read and understand all the instructions below before starting the exercises.

2. Files Needed

You will be provided with starter files for both exercises in this request. Create a project in Eclipse for this request and copy both starter files into this project. Be sure to complete the standard header comments at the top of each file.

3. To be Handed In

The files HerdManager.java and FillTheCorral.java should be submitted on-line via the HW15 page on Blackboard. Be sure to name the file/class exactly as specified. There is no MPL component for this request.

4. Programming Exercises

For both exercises below, a fully functioning Gate class is necessary. If you implemented your Gate class correctly, you can use that class. Note that the toString() method prints out a statement that is different from the toString() output. The toString() method should return the statement after the semicolon (and space) (i.e., after “West Gate: “ and “East Gate: “). You will need to update that method.

Note: when dealing with Gate objects in these exercises, you must use the interface (the methods) that was specified for the Gate class in HW14. You cannot add any new public behavior or modify the interface to your Gate class that was not specified in HW14.

Exercise 1: HerdManager.java

In this exercise, you will build a client program that simulates the movement of an escargatoire of snails in and out of a pen.

  1. Create a public static final integer HERD to indicate the size of your escargatoire. Set the constant to 24 for this simulation.
  2. Create two gates in your main() method and call them westGate and eastGate. You will need to call both methods described below in the main() method.
  3. Create the setGates() method that accepts as input parameters two Gate objects. This method will set the westGate to allow snails to re-enter the pen, leaving the pasture, and set the eastGate to OUT, to allow snails to leave the pen and go to pasture. Note: make sure the order of the parameters that are passed to this method is the following: westGate, eastGate.
  4. Create the simulateHerd() method that accepts two Gate objects and a Random object as input parameters.

Note: make sure the order of the parameters that are passed to this method are as listed above, where the order of the two Gate objects are: westGate, eastGate.

Set the number of snails in the pen equal to the size of the HERD. The method should run ten iterations of the simulation. In each iteration, randomly select one of the gates to the pen and move a random number of snails through that gate (in or out depending on the swing direction), thus changing the number of snails in the pen and out to pasture (an illustration is shown in Figure 1). Suggestion: to select a random gate, use nextBoolean() to get a random boolean value (only two possible values).

You must be sure that neither of these numbers (in the pen or out to pasture) is ever negative and that the sum total of snails is always equal to the size of the HERD. If, during some iteration, there are no snails currently out to pasture, then you should not randomly select a gate, but rather would move a random number of snails through the eastGate and out to pasture (an illustration is shown in Figure 2). You must do a similar thing when there are no more snails in the pen.

Also, the range of random numbers generated should change according to which gate you have selected and how many snails are currently available to go through that selected gate. However, the value should always be greater than zero.

Print out the status of the gates after creating the objects, after setting (opening) the gates, and the necessary information for each iteration as shown in the example execution of HerdManager.java below. Note that the first output line in the simulateHerd() method has been included in your starter file. Your output format must match the example execution exactly.

Example execution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
West Gate: The gate is locked.
East Gate: The gate is locked.

West Gate: This gate is not locked and swings to enter the pen only.
East Gate: This gate is not locked and swings to exit the pen only.

There are currently 24 snails in the pen and 0 snails in the pasture.

There are currently 8 snails in the pen and 16 snails in the pasture.
There are currently 6 snails in the pen and 28 snails in the pasture.
There are currently 4 snails in the pen and 20 snails in the pasture.
There are currently 3 snails in the pen and 21 snails in the pasture.
There are currently 2 snails in the pen and 22 snails in the pasture.
There are currently 0 snails in the pen and 24 snails in the pasture.
There are currently 7 snails in the pen and 17 snails in the pasture.
There are currently 6 snails in the pen and 18 snails in the pasture.
There are currently 5 snails in the pen and 19 snails in the pasture.
There are currently 0 snails in the pen and 24 snails in the pasture.

Example execution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
West Gate: The gate is locked.
East Gate: The gate is locked.

West Gate: This gate is not locked and swings to enter the pen only.
East Gate: This gate is not locked and swings to exit the pen only.

There are currently 24 snails in the pen and 0 snails in the pasture.

There are currently 4 snails in the pen and 20 snails in the pasture.
There are currently 3 snails in the pen and 21 snails in the pasture.
There are currently 0 snails in the pen and 24 snails in the pasture.
There are currently 21 snails in the pen and 3 snails in the pasture.
There are currently 11 snails in the pen and 13 snails in the pasture.
There are currently 2 snails in the pen and 22 snails in the pasture.
There are currently 1 snails in the pen and 23 snails in the pasture.
There are currently 0 snails in the pen and 24 snails in the pasture.
There are currently 20 snails in the pen and 4 snails in the pasture.
There are currently 11 snails in the pen and 13 snails in the pasture.

Exercise 2: FillTheCorral.java

In this exercise, you will build a client program that simulates moving snails from a pasture into four different corrals. It’s a bit of a game really. Following the best practices for writing programs, fill in the methods provided in the starter file to accomplish the following. Use a Random object to initially set each of the four gates to a specific swing direction or closed. So, the gates for each of the four corrals will be set to swing IN to the corral, OUT of the corral, or remain locked (an illustration is shown in Figure 3). Note that one of these gates must be set to IN (more details in a moment).

The program begins with five snails out to pasture and an infinite number of snails in each corral. A random number of snails, not to exceed the number already out to pasture, attempt to pass through a randomly chosen corral gate.