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

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

View Full Document Right Arrow Icon
/** * Class to model a Master combination lock. The lock has a three-number * combination; each number is an integer between 0 and 39, inclusive. * Class invariant: (num1 >= 0) && (num1 < 40) * && (num2 >= 0) && (num2 < 40) * && (num3 >= 0) && (num3 < 40) * Brandy Hicks & Allysa Ault, 2005-10-13 */ 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) * && (num2 < 40) && (num3 >= 0) && (num3 < 40) * @ensure (open == false) && (this.num1 == num1) * && (this.num2 == num2) && (this.num3 == num3) */ public MasterLock(int num1, int num2, int num3) { // Check precondition assert (numEntered >= 0) && (numEntered < 40): "Illegal numEntered: " +
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern