Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will hand in this exercise. Download and complete the starter file here: Combination.java. Implement a class, called Combination, to store three integer values (

You will hand in this exercise. Download and complete the starter file here: Combination.java.
Implement a class, called Combination, to store three integer values (ints).
declare the necessary instance variables to store the three integer values;
create a constructor, public Combination(int first, int second, int third), to initialize the values of this object.
implement the instance method public boolean equals(Combination other), such that equals returns true if other contains the same values, in the same order, as this Combination; the order is defined by the order of the parameters of the constructor, given the following:
Combination c1, c2, c3;
c1= new Combination(1,2,3);
c2= new Combination(1,2,3);
c3= new Combination(3,2,1);
then c1.equals(c2) is true but c1.equals(c3) is false;
finally, implement the method public String toString(), to return a String representation of this object, where the first, second and third values are concatenated and separated by : symbols. E.g.
Combination c1;
c1= new Combination(1,2,3);
System.out.println(c1);
displays 1:2:3.
The interface of the class Combination consists therefore of its constructor, the method equals and the method toString.
Ideally, the input data should be validated. In particular, all the values should be in the range 1 to 5. However, since we do not yet have the tools to handle exceptional situations, we will assume (for now) that all the input data are valid!
Note: Simpler code is better and it is worthwhile spending time cleaning up code even if it was already working.
Hint: my implementation is approximately 20 lines long, not counting blank lines. This is not a contest to write the shortest class declaration. I am providing this information so that you can evaluate the relative complexity of the tasks.
4. DoorLock
You will hand in this exercise. Download and complete the starter file here: DoorLock.java.
Create an implementation for the class DoorLock described below.
declare an integer constant, called MAX_NUMBER_OF_ATTEMPTS, that you will initialize to the value 3;
instance variables. The class DoorLock must have the necessary instance variables to
store an object of the class Combination
to represent the property of being opened or closed
to represent its activation state (the door lock is activated or deactivated), and
to count the number of unsuccessful attempts at opening the door;
the class has a single constructor, DoorLock( Combination combination ), which initializes this instance with a combination. When a door lock is first created, the door lock is closed. Also, when the object is first created, it is activated and the number of failed attempts at opening it should be zero;
implement the instance method public boolean isOpen() that returns true if this door lock is currently opened and false otherwise;
implement the instance method public boolean isActivated() that returns true if this door lock is currently activated and false otherwise;
implement the instance method public void activate(Combination c) that sets the instance variable activated to true if the parameter c equals to the combination of this object;
finally, implement the instance method public boolean open( Combination combination ) such that
an attempt is made at opening this door lock only if this door lock is activated
if the parameter combination equals to the combination of this door lock, set the state of the door to be open, and the number of failed attempts should be reset to zero
otherwise, i.e. if the wrong Combination was supplied, the number of failed attempts should be incremented by one,
if the number of failed attempts reaches MAX_NUMBER_OF_ATTEMPTS, this door lock should be deactivated.
Hint: my implementation is approximately 40 lines long, not counting the blank lines.
5. SecurityAgent (if there is time)
Implement the class SecurityAgent described below.
instance variables. A security agent is responsible for a particular door lock. Declare the necessary instance variables such that a SecurityAgent
remembers (stores) a Combination and
has access to this particular DoorLock, i.e. maintains a reference to a DoorLock object;
implement a constructor with no parameter such that when a new SecurityAgent is created
it creates a new Combination and stores it,
it creates a new DoorLock with this saved Combination. For the sake of simplicity, you may decide to always use the same combination:
Combination secret;
secret = new Combination(1,2,3);
if secret is the name of the instance variable that is used to remember the Combination. Or, you can let your SecurityAgents use their imagination, so that each SecurityAgent has a new Combination that it only knows.
int first, second, third;
first =(int)(Math.random()*5)+1;
second =(int)(Math.random()*5)+1;
third =(int)(Math.random()*5)+1;
secret = new Combination(first, second, third);
Alternative w

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Please describe the current IS technology at Benton?

Answered: 1 week ago