1. Aim
The purpose of this request is to:
- Practice basic object-orient programming.
- Read a UML diagram and build the corresponding class.
- Create a class file that includes instance variables and methods necessary to interact with objects of the class.
It is recommended that you read and understand all the instructions below before starting the exercises.
2. Files Needed
You will not be provided with any starter files for this request. Create a project in Eclipse for this request. For this request, you will not want Eclipse to create the public static void main method for you. Be sure to include the standard header comments at the top of each file.
3. To be Handed In
The files Gate.java should be submitted on-line via the Blackboard. Be sure to name the file/class exactly as specified. There is no MPL component for this request.
4. Programming Exercise
Create a Gate class that can be used to represent a gate for a livestock pen. The members of the Gate class are listed in the diagram below. You must use the same field names and method names (and class constant names) as listed in the diagram. This will insure that the included test program will work for your implemented class.
Step 1: create the Gate class
In your Eclipse project for this request, create a new class called Gate. Do not indicate that you want Eclipse to create a main method for you.
Step 2: add the instance variables and class constants
There will be two states for this gate, locked, which would be a boolean variable, and swingDirection, which we will create as an int. The field swingDirection will indicate which direction the gate is able to swing. To make our program more readable, create two integer class constants (public static final int) to indicate the swing direction:
IN = 1 (to swing in, i.e., to let animals enter the pen)
OUT = -1 (to swing out, i.e., to let animals leave the pen)
Be sure to use these class constants as much as possible in your class. When a gate is first constructed, it should be locked and the swing direction should be set to 0, the default value.
Step 3: add the methods
Create mutator methods that will change the state of a Gate object.
a. close() – to close the gate by “locking” it (i.e., locked is true),
b. open() – to (i) open the gate by “unlocking” it (i.e., locked is false), (ii) set the swing direction to either swing in or out (i.e., swingDirection is either IN or OUT based on the input parameter), and (iii) return a boolean value indicating if the setting of the swing direction was successful (see the next point),
c. setSwingDirection() – to (i) set the swing direction of the gate based on the input parameter and (ii) return a boolean value to indicate if the setting the swing direction was successfully set (true), or if an invalid swing direction was given (false, not set), and
d. thru() – described in No. 3 below.Create accessor methods that will return information about the state of a Gate object.
a. getSwingDirection() – returns the swing direction of the gate,
b. isLocked() – returns whether the gate is locked, and
c. toString() – returns the string representation of the Gate object that should match the output of the provided example execution of the test program.Another behavior of the gate is that animals will go through it. Suppose n animals attempt to go through the gate. If the gate is set to the swing OUT position, the animals will leave the pen and the total number of animals in the pen will be decreased. If the gate is set to the swing IN position, animals will be entering the pen and the number of animals will be increased. If the gate is locked, there should be no change to the number of animals in the pen. Create a method thru() that given the input parameter n, will return n, -n, or 0 depending on the locked status and the swing direction of the gate. Note: the class you are writing only represents a gate and not a pen. Here you should be thinking like the author of the class—making your class as useful and general as possible. Instead of attempting to alter some total that is in or out of the facility that the gate is controlling, we are simply going to return the net change, to be used by the client as needed.
Step 4: write Javadoc comments
Now that you have completed the Gate class, make sure you document it by adding Javadoc comments. First, add the standard comment header (see section 2 above) at the top of the file. Then add a Javadoc comment just before the class itself, describing the purpose of the class. Then add a Javadoc comment for each method, describing what that method does.
Example execution of TestGate.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31Test locked status of gate upon creation ===============================
SUCCESS: A new gate was successfully created.
Test opening gate to OUT ===============================================
Gate opened.
- SUCCESS: Gate not locked.
- SUCCESS: Gate set to swing in the right direction.
Test opening gate to IN ================================================
Gate opened.
- SUCCESS: Gate not locked.
- SUCCESS: Gate set to swing in the right direction.
Test closing gate ======================================================
SUCCESS: Gate closed.
Test setting of swing direction ========================================
SUCCESS: Gate set to swing IN.
SUCCESS: Gate set to swing OUT.
SUCCESS: Gate swing direction was not set to an invalid direction.
SUCCESS: Gate swing direction was set to a valid direction.
Test thru() method =====================================================
Current count = 10
SUCCESS: Animals have successfully left the pen.
SUCCESS: Animals have successfully entered the pen.
SUCCESS: This gate is locked, no animals can pass through.
toString() method output ===============================================
Locked: Yes
Swing direction: In