Question

In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the...

In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the specifications in the starter files for the classes. You can run the javadoc program to generate the API (see Tutorial 2) for the classes.

A box has a secret inside it (and maybe a key!) and can be locked. In order to open a box, you need the right key to unlock the lock. Secrets.java will be a program that takes an array of boxes and an array of keys and tries to open as many boxes as it can with those keys to find as many secrets as possible and returns the Proofs that if found them. Remember, a box can also contain a key to another box!

You need to complete the following classes: Lock, Box, and Secrets. You must submit these files.

The following classes are provided: Key, Proof and Secret. Do not change these classes. We will delete these files if you submit them and use our own copies.

package comp1406a2;
/** A Secret object has a secret (string) and a unique identifier (count) */
public class Secret {
    protected static int counter = 0;
    /** The secret */
    protected String secret;
    /** A count for this Secret object (A unique identifier) */
    protected int count;
    /* Creates a secret with the given input
     *  <p>
     * When created, a secret is also given a count that is
     * unique. The count allows us to differentiate two secrets with
     * the same string.
     *
     * @param secret is the secret for this Secret
     */
    public Secret(String secret){
        this.secret = secret;
        this.count = counter;
        counter += 1;
    }
    /** getter for the secret string
     * @return this Secret's secret (string)
     */
    public String getSecret(){ return this.secret;}

    /** getter for the secret count
     * @return this secret's count
     */
    public int getCount(){ return this.count;}
    @Override
    public boolean equals(Object o){
        if( o == null){  // should not need this check...
            return false;  // will include to avoid any crashed
        }
        // if o is a Secret object then
        // check if both secret string and count match this Secret's
        if( o instanceof Secret){
            Secret s = (Secret)o;
            return this.secret.equals(s.secret) && this.count == s.count;
        }
        // if o is not a Secret then return false
        return false;
    }
}
package comp1406a2;
/** A box holds a secret (and maybe a key). You can read the secret if
 * the box is open. Otherwise, you'll need the right key to open it.
 */
public class Box {
    // ADD ATTRIBUTES HERE
    /** Creates a Box object containing the specified secret with the specified lock.
     * Will create a new Lock for this box. The box will be close (locked) after creation.
     *
     * @param secret is the secret the box will hold
     */
        public Box(Secret secret){
        }
     /** Checks if the box is opened (unlocked) or closed (locked)
     *
     * @return true if the box is open (unlocked) and false otherwise.
     */
     public boolean isOpen(){
         return false;
     }
    /** Open this box's lock with the given key (if it matches the lock)
     * <p>
     * An already open box will remain open regardless of input key.
     * A closed box can only be opened if the key matches the lock.
     * Examples:
     *      box b is unlocked, key k does not match the lock of b
     *           b.open(k);
     *           b.isOpen() --> true
     *      box b is locked, key k matches the lock of b
     *           b.open(k);
     *           b.isOpen() --> true
     *      box b is locked, key k does not match the lock of b
     *           b.open(k);
     *           b.isOpen() --> false
     *
     * @param key is a key
     * @param this box (for method chaining purposes).
     */
    public Box open(Key key){
        return this;
    }
    /** Locks the lock on this box. */
    public void lock(){
    }
    /** Retrieves the secret in this box if it is open.
     *
     * @return the secret inside this box is this box is open (unlocked),
     *          and returns null otherwise.
     */
    public Secret getSecret(){
        return null;
    }
    /** Retrives the lock on this box.
     *
     * @return the lock of this box (if the box is open or not).
     */
    public Lock getLock(){
        return null;
    }
    /** Puts a key in this box
     *
     * @param key is a key (that will be added in this box)
     * @return this box (for method chaining purposes).
     */
    public Box addKey(Key key){
        return null;
    }
    /** Checks if this box has a key inside it (when the box is open)
     *
     * @return true if this box is open (unlocked) AND it does have a key inside,
     *         returns false otherwise.
     */
    public boolean hasKey(){
        return false;
    }
    /** Retrieves the key inside this box
     * (if the box is open and it has a key inside)
     *
     * @return the key inside this box if this box is open (unlocked) and
     *         has a key inside it. Returns null otherwise.
     */
    public Key getGet(){
        return null;
    }
}
package comp1406a2;

import java.util.Random;

public class Lock {
    // ADD ATTRIBUTES HERE


    /** Create a lock for the specified box
     * <p>
     * The lock must be initially locked (closed) when created.
     * The lock must be given a UNIQUE ID value when created.
     *
     * @param box is a Box that will have this lock to keep is shut.
     */
    public Lock(Box box){

    }

    /** Retrieve the key for this lock.
     * <p>
     * This method can be called multiple times but is only allowed to
     * return its mathcing key the first time it is called. Every time
     * after the mehtod will return null.
     *
     * @return the key that matches this lock (i.e., the key that can unlock it)
     *         if it is the first time called. Returns null otherwise.
     */
    public Key getKey(){
        return null;
    }


    /** Check if this lock is locked or not.
     *
     * @return true if this lock is locked and false otherwise.
     */
    public boolean isLocked(){
        return false;
    }

    /** Locks this lock (regardless if it was already locked or not). */
    public void lock(){

    }

    /** Unlocks the lock if the input key matches the lock.
     * <p>
     * If this lock is already unlocked this method will return true and
     * do nothing else.
     * If this lock is locked and input key matches this lock, then it
     * unlocks the lock and returns true.
     * If this lock is locked and the input key does not match this lock,
     * then returns false and does nothing else.
     *
     * @param key is key.
     * @return true if the lock was already unlocked OR if the key matches the
     *         lock. Returns false otherwise.
     */
    public boolean unlock(Key key){return false;}

    /** retrieves the ID of the lock
     *
     * @return the ID of this lock.
     */
    public int getID(){
        return -1;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * A box holds a secret (and maybe a key). You can read the secret if the box is
 * open. Otherwise, you'll need the right key to open it.
 */
class Box {

        // ADD ATTRIBUTES HERE
        private Secret secret;
        private boolean locked;
        private Lock lock;
        private Key key;

        /**
         * Creates a Box object containing the specified secret with the specified lock.
         * Will create a new Lock for this box. The box will be close (locked) after
         * creation.
         *
         * @param secret is the secret the box will hold
         */
        public Box(Secret secret) {
                this.secret = secret;
                lock = new Lock(this);
        }

        /**
         * Checks if the box is opened (unlocked) or closed (locked)
         *
         * @return true if the box is open (unlocked) and false otherwise.
         */
        public boolean isOpen() {
                return !lock.isLocked();
        }

        /**
         * Open this box's lock with the given key (if it matches the lock)
         * <p>
         * An already open box will remain open regardless of input key. A closed box
         * can only be opened if the key matches the lock. Examples: box b is unlocked,
         * key k does not match the lock of b b.open(k); b.isOpen() --> true box b is
         * locked, key k matches the lock of b b.open(k); b.isOpen() --> true box b is
         * locked, key k does not match the lock of b b.open(k); b.isOpen() --> false
         *
         * @param key is a key
         * @param     this box (for method chaining purposes).
         */
        public Box open(Key key) {
                if (!key.invalid() && lock.isLocked()) {

                        if (key.lockID() == lock.getID()) {
                                lock.unlock(key);
                        }
                }

                return this;
        }

        /** Locks the lock on this box. */
        public void lock() {
                lock.lock();
        }

        /**
         * Retrieves the secret in this box if it is open.
         *
         * @return the secret inside this box is this box is open (unlocked), and
         *         returns null otherwise.
         */
        public Secret getSecret() {
                if (!lock.isLocked()) {
                        return secret;
                }
                return null;
        }

        /**
         * Retrives the lock on this box.
         *
         * @return the lock of this box (if the box is open or not).
         */
        public Lock getLock() {
                return lock;
        }

        /**
         * Puts a key in this box
         *
         * @param key is a key (that will be added in this box)
         * @return this box (for method chaining purposes).
         */
        public Box addKey(Key key) {
                this.key = key;
                return this;
        }

        /**
         * Checks if this box has a key inside it (when the box is open)
         *
         * @return true if this box is open (unlocked) AND it does have a key inside,
         *         returns false otherwise.
         */
        public boolean hasKey() {
                return (key != null);
        }

        /**
         * Retrieves the key inside this box (if the box is open and it has a key
         * inside)
         *
         * @return the key inside this box if this box is open (unlocked) and has a key
         *         inside it. Returns null otherwise.
         */
        public Key getGet() {
                if (isOpen() && hasKey()) {
                        return key;
                }
                return null;
        }

}

please ask one class at a time.. i am answering box class in this question.. Request you to please other question classes separately.

Add a comment
Know the answer?
Add Answer to:
In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project...

    Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • k * *Given 4 String parameters, add a contact to the contacts array. Before adding a...

    k * *Given 4 String parameters, add a contact to the contacts array. Before adding a contact to the array, do a parameter sanity check in this method. If the array is full, or if first name or last name is null or an empty string, do *not add a contact but return false instead. Middle name can be left as null or an empty String. Note that we allow adding duplicate contacts If the name is acceptable, create a...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. *...

    Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. * */ public class Course implements Serializable { private String prefix; private int number; private String title; private String grade; /** * Constructs the course with the specified information. * * @param prefix the prefix of the course designation * @param number the number of the course designation * @param title the title of the course * @param grade the grade received for the course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT