Java代写:CS1101 Hard Manager

Introduction

本次作业仍然和上次一样,考察Object Oriented Programming的概念,实现一个具有现实意义的小项目。

1. Aim

The purpose of this assignment 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 assignment. Create a project in Eclipse for this assignment 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 assignment.

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

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

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.

  • If the gate allows for entry, the snails enter the corral and the number of snails out to pasture is reduced. For example, in Figure 4, two snails entered Corral 0 (through corralGate[0]) leaving 3 snails in the pasture.
  • If the gate is set to exit the corral however, the same number of snails that attempted to enter that corral actually exits the corral thus increasing the number of snails out to pasture. For example, in Figure 5, suppose there were three snails in the pasture. One snail attempts to enter Corral 1 (through corralGate[1]). The gate is set to exit, so the snail can’t enter. Furthermore, a new snail (the same number as that tried to enter Corral 1) is added to the pasture resulting in four snails in the pasture.
  • If the gate is locked, they do not enter and there is no change in the number of snails in the pasture (an illustration is shown in Figure 6).

This continues until finally there are no snails out to pasture.
We must suspend reality a bit to run this game. For example, we must imagine there are an infinite number of snails in each corral waiting to get out to pasture. Note that when setting the gates randomly to IN, OUT, or locked, be sure that at least one gate is set to IN so that the game can finally end. A natural way to do this with such a small number of corral gates is to set them, then test to be sure at least one is IN. If not, try setting all four corral gates again. Another algorithm would randomly set all four gates, then randomly select one of the gates to be set to IN, overriding its original setting.
It is also conceivable that this game goes on infinitely, or that the ever increasing number of snails out to pasture exceeds the size of the largest integer for your computer and the program crashes or begins delivering anomalous results. If this occurs, simply terminate your program and return 0 as the number of tries.

Example execution 1

Gate setup:
Gate 0: This gate is not locked and swings to enter the pen only.
Gate 1: The gate is locked.
Gate 2: This gate is not locked and swings to exit the pen only.
Gate 3: The gate is locked.

3 snails are trying to enter Corral 2.
The gate is set to exit.
As a result, there are now 8 snails still in the pasture.

7 snails are trying to enter Corral 0.
The gate is set to enter.
As a result, there are now 1 snails still in the pasture.

1 snails are trying to enter Corral 0.
The gate is set to enter.
As a result, there are now 0 snails still in the pasture.

It took 3 attempts to corral all of the snails.

Example execution 2

Gate setup:
Gate 0: The gate is locked.
Gate 1: This gate is not locked and swings to enter the pen only.
Gate 2: The gate is locked.
Gate 3: This gate is not locked and swings to exit the pen only.

1 snails are trying to enter Corral 3.
The gate is set to exit.
As a result, there are now 6 snails still in the pasture.

6 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

6 snails are trying to enter Corral 2.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

2 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

6 snails are trying to enter Corral 2.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

1 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

1 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 6 snails still in the pasture.

3 snails are trying to enter Corral 3.
The gate is set to exit.
As a result, there are now 9 snails still in the pasture.

2 snails are trying to enter Corral 1.
The gate is set to enter.
As a result, there are now 7 snails still in the pasture.

7 snails are trying to enter Corral 1.
The gate is set to enter.
As a result, there are now 0 snails still in the pasture.

It took 10 attempts to corral all of the snails.

Example execution 3 (partial printout)

Gate setup:
Gate 0: The gate is locked.
Gate 1: This gate is not locked and swings to exit the pen only.
Gate 2: This gate is not locked and swings to enter the pen only.
Gate 3: This gate is not locked and swings to exit the pen only.

2 snails are trying to enter Corral 2.
The gate is set to enter.
As a result, there are now 3 snails still in the pasture.

...

202 snails are trying to enter Corral 1.
The gate is set to exit.
As a result, there are now 1427 snails still in the pasture.

435 snails are trying to enter Corral 2.
The gate is set to enter.
As a result, there are now 992 snails still in the pasture.

514 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 992 snails still in the pasture.

10 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 992 snails still in the pasture.

485 snails are trying to enter Corral 2.
The gate is set to enter.
As a result, there are now 507 snails still in the pasture.

...

12 snails are trying to enter Corral 2.
The gate is set to enter.
As a result, there are now 1 snails still in the pasture.

1 snails are trying to enter Corral 3.
The gate is set to exit.
As a result, there are now 2 snails still in the pasture.

1 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 2 snails still in the pasture.

2 snails are trying to enter Corral 0.
The gate is locked.
As a result, there are now 2 snails still in the pasture.

2 snails are trying to enter Corral 2.
The gate is set to enter.
As a result, there are now 0 snails still in the pasture.

It took 112 attempts to corral all of the snails.

5. Additional requirements

  1. Each method that you write (except main ), should be preceded by a block of Javadoc comments.
  2. You should use a consistent programming style. This should include the following:
    1. Meaningful variable names.
    2. Consistent indenting.
    3. Use of “white-space” and blank lines to make the code more readable.
    4. Use of comments to explain pieces of tricky code.
    5. Each line in your program must be less than 100 characters (this includes any whitespace, tabs, code plus associated comments).
  3. Watch out for these unnecessary deductions!
    1. Not resubmitting ALL files if you make a re-submissions. If you re-submit one, resubmit all! (0 pts for any program not re-submitted).
    2. Submitting the.class file (0 pts for the exercise associated to that program).
    3. Submitting the.class file along with the proper.java file (-2 pts for that submission).
    4. Incorrect file name (-1 pt).
    5. Incomplete or missing header information (-1 pt).
    6. Storing the file in a package (-1 pt).

See the code examples in the class text for a good formatting style.