MasterLock

# MasterLock - numEntered; open = false; this.numEntered =...

This preview shows pages 1–2. Sign up to view the full content.

/** * Class to model a Master combination lock. The lock has a three-number * combination; each number is an integer between 0 and 39, inclusive. * * */ public class MasterLock { private boolean open; // Whether the lock is open (unlocked) private int num1, num2, num3; // Combination private int num; // Number you enter private int numEntered; // Numbers entered so far private boolean haveFirst; // First number entered correctly? private boolean haveSecond; // Second numbered entered correctly? /** * Create a new MasterLock with the given combination. The lock is * initially locked. * @require (num1 >= 0) && (num1 < 40) && (num2 >= 0) * * @ensure (open == false) && (this.num1 == num1) * */ public MasterLock(int num1, int num2, int num3) { // Check precondition assert (numEntered >= 0) && (numEntered < 40): "Illegal numEntered: " +

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: numEntered; open = false; this.numEntered = numEntered; assert ((open == false) && (this.numEntered == num)):"Violated postcondition"; } // Query /** * Represent the MasterLock as a String. Use for debugging only!! */ public String toString() { return "MasterLock: open="+open+ " combination: "+num1+"-"+num2+"-"+num3+ " entered so far: "+numEntered; } /** * Is it open? */ public boolean isOpen() { return open; } // Commands /** * Enter the numbers in the combination in the correct sequence. * numEntered == num * */ public void enter (int num) { numEntered = num; haveFirst = (num == num1); haveSecond = (haveFirst && (num == num2)); if ((num == num3) && (haveSecond)){ open = true; } } /** * Close the lock, resetting the entered numbers. * @ensure (open == false) && (num1Entered == -1) && (num2Entered == -1) */ public void close() { open = false; assert (open == false): "Violated postcondition"; } }...
View Full Document

## This document was uploaded on 10/23/2009.

### Page1 / 2

MasterLock - numEntered; open = false; this.numEntered =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online