Java代写:CS1101 Gate

Introduction

本次作业考察Object Oriented Programming的概念,需要按照需求定义编写一个Gate的Class,总体难度偏简单。

1. Aim

The purpose of this assignment is to:

  1. Practice basic object-orient programming.
  2. Read a UML diagram and build the corresponding class.
  3. 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 assignment. Create a project in Eclipse for this assignment. For this assignment, 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 assignment.

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 assignment, 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

  1. 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.
  2. 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.
  3. 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.

Step 5: test your class

Once you have built your class, you cannot run it because it is not executable code, but a class template file that is used to instantiate objects in other classes. You should test your Gate.java file using the provided TestGate.java. This program will attempt to create a Gate object and invoke its methods to test the implementation of the Gate class. The relevant code is already created for you in TestGate.java. If your implementation doesn’t pass a test, you will receive a “FAILURE” message. The provided example execution shows all tests passed. We might test your class using a different test program.

Notes

  • You can assume that the values that will be passed to the methods that you implement in your Gate class will be valid types. That is, methods that will accept an integer will be passed integer values. However, the parameter passed to open() may not be just -1 or 1. For the thru() method, the parameter passed to thru() will be a positive integer.
  • You can assume that before a Gate object will be used, its swing direction will be set to something other than the default value of 0.

Example execution of TestGate.java

Test 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

5. Additional requirements

  1. You must start your program with header comments that provide your name, VUnetID, email address, the date the program was last modified, and a short description of the program
  2. Each method that you write (except main), should be preceded by a block of Javadoc comments.
  3. You should use a consistent programming style. This should include the following:
    a. Meaningful variable names.
    b. Consistent indenting.
    c. Use of “white-space” and blank lines to make the code more readable.
    d. Use of comments to explain pieces of tricky code.
    e. Each line in your program must be less than 100 characters (this includes any whitespace, tabs, code plus associated comments).
  4. Watch out for these unnecessary deductions!
    a. 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).
    b. Submitting the .class file (0 pts for the exercise associated to that program).
    c. Submitting the .class file along with the proper .java file (-2 pts for that submission).
    d. Incorrect file name (-1 pt).
    e. Incomplete or missing header information (-1 pt).
    f. Storing the file in a package (-1 pt).

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

6. Submission for grading

Once you have completed the exercise, submit the file Gate.java for grading by visiting the Blackboard. You do not need to submit TestGate.java.