Vous êtes sur la page 1sur 3

Assignment 1 for CPS109 This assignment is based on Project 5.1 of Big Java (4th ed).

Implement a combination lock class, called Lock. A combination lock has a dial with 26 positions labeled A ... Z. The lock is created with a three letter combination. To open the lock, the dial needs to be set three times. If it is set to the correct combination, the lock can be opened. To open the lock you must pull on it, after which it will be open if the combination was right. While the lock is open, turning the dial has no effect. When the lock is closed again, the combination can be entered and a pull can be given to try to open the lock. If the user sets the dial more than three times, the last three settings determine whether the lock can be opened. For example, if the combination is C, A, T, and the user sets the dial to B, then C, then A, then T, the lock can be opened with a pull. However, if the dial sequence is CATD, then the lock cannot be opened, since the last three positions are not CAT. Example 1: combination = CAT The lock can be opened with: CAT, BIGCAT, ACAT (since the last three letters entered are right) The lock cannot be opened with: EAT, AAT, CAG, CATDOG (since the last three letters are wrong) By default, the lock is perfect in that it responds only to the correct combination. However, a lock can be created with a combination and a tolerance. The tolerance specifies how much slop is in the dial, that is, the total amount that the user can be off in setting the dial and still succeed in opening the lock. Example 2: combination = CAT, tolerance = 1 The lock can be opened with: CAT, DAT, CAS, CBT, BAT (since the total error <= tolerance) The lock cannot be opened with: EAT, AAT, CCT (since the total error > tolerance) Assume the dial is circular, so that Z is next to A as well as Y. Example 3: combination = XYZ, tolerance = 3 The lock can be opened with: XYZ, ZZZ, XYB, XYC, ABXZB (since the total error <= tolerance) (The total errors in this example are XYZ (0), ZZZ (3), XYB (2), XYC (3), ABXZB (3)). The lock cannot be opened with: WWY, since the total error is 4. An example tester class is attached to this assignment. The given tester class does not test everything that should be tested, so you should add to it. The tester used to help mark your assignment will be more complete. Note that the given LockTester.java shows you the names of the methods and the expected results. Clearly, you have to use the same names so that your Lock class will work with our tester. You have to submit to Blackboard the following files (and no more): Lock.java LockTester.java exampleRun.txt The third file (exampleRun.txt) shows the output when you run your LockTester.java. Make sure that you submit the .java files and NOT the .class files. Marking rubric (out of 10):

2 points for documentation in Lock.java. The documentation has to be in the javadoc style. Every class, constructor and method must have the properly tagged javadoc comments. This style is described in Big Java in Section 3.4 Commenting the Public Interface, and you have seen it in the examples given in the labs. Proper indentation and suitable variable names form part of the mark. 1 points for LockTester.java which should thoroughly test the methods of Lock. 1 point for exampleRun.txt. The marking here is based on the clarity of this file, which really depends on LockTester.java, since this is an output file. You can copy/paste output from the run into this file, but you cannot add more explanatory words or formatting. The explanatory words and whitespace should be generated by LockTester. 6 points for Lock.java. This mark comes from the functionality of your class. Your class will be run by a new LockTester class which will test all the required features. You have to work alone when writing your code. You can discuss ideas with your classmates, but you cannot copy code or develop code together (and then change wording). If you write your own code, then it will appear unique. If your code looks strangely similar to someone else's code, then this will affect your mark negatively. If the code is so similar that the marker is sure there is unfair collaboration, then there might be unfortunate charges of academic misconduct. /** This class tests Lock objects. */ import java.util.* ; public class LockTester { public static void main(String[] args) { Scanner scanner = new Scanner(System.in) ; Lock lock = new Lock('C', 'A', 'K') ; lock.set('C') ; // first turn System.out.print("The lock will open? ") ; lock.pull() ; // try System.out.println(lock.isOpen()) ; System.out.println("Expected: false") ; // only one turn lock.set('A') ; // second turn System.out.print("The lock will open? ") ; lock.pull() ; // try System.out.println(lock.isOpen()) ; System.out.println("Expected: false") ; // only two turns lock.set('K') ; // third turn System.out.print("The lock is open? ") ;

System.out.println(lock.isOpen()) ; System.out.println("Expected: false") ; // haven't pulled yet System.out.print("The lock will open? ") ; lock.pull() ; // try System.out.println(lock.isOpen()) ; System.out.println("Expected: true") ; // correct combination lock.set('L') ; // turning dial while lock is open System.out.print("The lock is open? ") ; System.out.println(lock.isOpen()) ; System.out.println("Expected: true") ; // it is still open lock.close() ; // closing the lock resets dials System.out.print("The lock is open? ") ; System.out.println(lock.isOpen()) ; System.out.println("Expected: false") ; // we just closed it System.out.println("Give the combination of a new lock as a 3 letter string") ; String combo = scanner.next() ; System.out.println("Give the tolerance of this lock as an int") ; int tolerance = scanner.nextInt() ; lock = new Lock(combo.charAt(0), combo.charAt(1), combo.charAt(2), tolerance) ; System.out.println("Give a 5 letter string which represents an effort to open the lock.") ; combo = scanner.next() ; lock.set(combo.charAt(0)) ; lock.set(combo.charAt(1)) ; lock.set(combo.charAt(2)) ; lock.set(combo.charAt(3)) ; lock.set(combo.charAt(4)) ; System.out.print("The lock will open? ") ; lock.pull() ; // try System.out.println(lock.isOpen()) ; } }

Vous aimerez peut-être aussi