Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How will you program using Java? SOTYxNDgxODRa In this assignment we are going to design two classes that represent the functionality of a lock. There

image text in transcribed

How will you program using Java?

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

SOTYxNDgxODRa In this assignment we are going to design two classes that represent the functionality of a lock. There are many types of locks Including combination locks, pad locks, and dead bolt locks, Just to name a few. The first class we will design is going to represent the behavior that all locks possess, hence a more general description. Whereas the second class will represent the behavlor of a combination lock and a have a more specific description. Through inheritance the second class will inherit the behavior of the general lock class. Modifications To begin the question must be asked, "What state do all locks have in common?" Answer - they are either locked or unlocked. So in our class we will create an instance variable to store this state. Here is the beginning implementation of the Lock class: public class Lock ll Instance Variable private boolean locked; Notice that the instance variable is named locked and that it is declared as type boolean. Recall that a boolean variable can only have one of two possible states: true or false. This will work nicely with our class since a lock only has two possible states: unlocked or locked Mutator Methods The next question that must be asked is "What behavior do all locks have in common?" Answer they can be locked or unlocked. Recall that behaviors in a class are represented by methods. So we will define two methods in our Lock class that will give us the ability to change the state of a lock to either locked or unlocked. Here is the modified implementation of the Lock class with these two methods added: public class Lock ll Instance Variable private boolean locked ll Mutator Methods public void open) locked false; public void close) locked true; Notice that the methods are named open and closed. When a program sends a lock object the open message the locked state is changed to false which represents an open lock. If a lock object is sent a close message then the locked state is changed to true which represents a closed lock. Since both of these methods change the value of the instance variable they are called mutator methods. Accessor Methods Page 1 Q+ As discussed previously, it is often object. We call these kinds of methods accessor to access or view the state of an since there is only one instance methods. In tne Lock cass, omputer Science (AP) Perioc Avant Login gle.com/c/MTQSOTYxNDgxODRa the locked state is changed to false which represents an open lock. If a lock object is sent a close m then the locked state is changed to true which represents a closed lock. Since both of these methods change the value of the instance variable they are called mutator methods. essage As discussed previously. it is often benefically to Include In a class the abity to access or view the state of an object We call these kinds of methods variable we will only defined one accessor Accessor Methods s accessor methods. In the Lock class, since there is only one instance method. Often accessor method names are preceded with the preftx is not a requirement. In this class, we will name our accessor method - amlLocked. Here is its public class Lock ll Instance Variable private boolean locked; ll Mutator Methods public void open) locked false: public void close) locked true ll Accessor Method public boolean amlLockedo return locked As you can see the am/Locked method simply returns the state of the locked variable. toString Method As you should know by now it is always a good idea to implement a toString method for your class. Here is the implementation of the Lock class with a toString method added; its implementation is self-explanatory public class Lock f Instance Variable private boolean locked ll Mutator Methods public void open) locked false; public void close) X- Avant l Login puter Science (AP) Pero a.com/c/MTQSOTY NDgxODRa l Mutator Methods public vold open) locked false; public vold close() locked true ll Accessor Method public boolean amlLocked) return locked; ll toString Method public String toString) if lockedfalse) return "Lock is open: else return "Lock is closed Constructors Before we finish the implementation of the Lock class there is one more topic we must cover. Do you care to guess what it is? Yes, constructors. When we construct a lock object we must decide if we want the lock to start in an open or closed state. Recall that if you do not create a constructor for a class the compiler will create one for you and assign the instance variables their default values. The default value for a boolean variable is false. That means, if we leave the implementation of our constructor up to the compiler our lock object will start with an open state (ocked false) Since I want lock objects to start in a locked state (locked true) we will create our own default constructor Here is the completed Lock class with a default constructor included: public class Lock I/ Instance Variable private boolean locked Il Default Constructor public Lock() locked = true; // lock starts in a closed state l Mutator Methods public void open() ) Perio Avant l Login P OTYxNDgxODRa l Instance Variable private boolean locked / Default Constructor public Lock0 locked = true; // lock starts in a closed state Il Mutator Methods public void open0 locked false; public void close0 locked true ll Accessor Method public boolean amiLocked0 return locked l toString Method public String toString0 if(locked false) return "Lock is open else return "Lock is closed We have finished the implementation of our Lock class Copy the code from the completed Lock class above into a source file named Lock.java. The Lock class is now our base class. The next step is to derive a class named CombinationLock from our base class. This CombinationLock class will inherit the behavior of the Lock class Combination Lock Class To begin this class, we start with the creation of instance variables that are needed. To do this we must ask ourselves this question, "What are the states of a combination lock?" Well, to start, a combination lock is either locked or unlocked. But since we are going to inherit this state from our superclass, Lock, we do not need to include it in our definition. However, a state that is specific to a combination lock is the combination itself. In order for a combination lock to be opened, it must know its combination. In our implementation we declare an instance variable named combination and declared it type String. Here is the beginning of our CombinationLock clas XAvant] Login gxODRa instance variable named combination and declared it type String. Here is the beginning of our CombinationLock class: import Java.util.". # needed for Scanner public class CombinationLock extends Lock Ili Instance Variable private String combination; Notice the use of the keyword, extends, this means we are extending the definition of the CombinationL ock class to include the definition of the Lockclass. This extending is called inheritance, and the Lock class. t means that the CombinationLock now contains the state (instance variables) and behaviormethods) of Copy the code above to a source file named CombinationLock.java. We now turn to the specific behavior of a combination lock. The question must be asked, "What behavior or action doe behavior that is were inherited from the Lock class, we must now deci s a combination lock perform?" Answer - It locks and unlocks. It tuns out that this is the same y our Lock class in the tw o methods open and close. Since both of these methods de if here behavior needs to be modified in order to reflect the behavior of a combination lock. Lers start with the open method. What do you do to ope n a combination lock? Answer- you enter the combination. Since this is not the action performed by a eral lock, we ill need to override the open method. An overridden method is a method that you declare in the subclass that has the same name and parameter list of a method from the superclass. When you override a method, it is no longer inherited from the superclass. The open Method Create a method named open in the CombinationLock class that overrides the open method declared in the Lock class. This new method should allow an user to enter a combination sequence from the keyboard using the nextLine method. Include a prompt before the nextLinethat says "Enter combination ". Once the combination is entered, it should be compared to the string stored in the combinationinstance variable. If they are the same value, then the locked variable inherited from the Lock class should be set to open (locked false), otherwise it should be set to closed (locked true). . Reminder: To compare strings for equality you should use the equals method not the operator. Example: combination.equals(str) The close Method We now consider the implementation of the close method. It seems that the close operation performed in the Lock class is the same behavior that should be used in the CombinationLock class. Therefore, we do not need to override the close method, we will just use the one inherited from the Lockclass. The toString Method The CombinationLock class inherts the toString method from the Lock class, but it does not include ation about the combination instance variable. Therefore, we will override the toString method. Note: ly you would not want to include code that displays a password or combination to the user but since we are attempting to learn some of the principles of inheritance we will go ahead and overlook that here. Here is P) Penoc Avant | Login OTYxNDgxODRa The close Method We now consider the implementation of the close method. It seems that the close operation performed in the Lock class is the same behavior that should be used in the CombinationL ock class. Therefore, we do not need to override the close method, we will just use the one inherited from the Lockclass. The toString Method The CombinationLock class Inherts the toString method from the Lock class, but it does not include information about the combination instance variable. Therefore, we will override the toString method. Note: Normally you would not want to include code that displays a password or combination to the user but since we are attempting to learn some of the principles of inheritance we will go ahead and overlook that here. Here is ts implementation public String toString) String str super.toString)+ In"+ "Combinationcombination+"n" return str, Notice the statement super.toString). The keyword super gives a subclass access to the superclass that it is inheriting. Since the toString method of the Lock class already returns a string that includes information about an object's locked state, this is the most efficient way to include this information in the overridden toString method 9 Add the code for the toString method to the CombinationLock class . Mutator and Accessor Methods Strictly speaking you probably would not create a mutator or accessor method for a combination lock since you probably would not want a user to be able to modify or view the combination. However, since we are really designing a hypothetical lock, we will add them to our class just to give you more practice In the definition of the CombinationL ock class create a mutator method named setCombination which allows an object to modify the variable combination Create an accessor method named getCombination which allows an object to view the contents of the . variable combination. Constructors We are going to add two constructors to our CombinationLock class. The first will be a default constructor with no parameters and the second will contain one parameter The default constructor will initialize the combination variable to a null string. Something we must consider is how does the variable locked inherted from the Lock class get its initial value. If you recall, we can use the keyword super to invoke a constructor from the superclass. If you wish to perform this operation it must be the first line in the subclasses constructor. Here is the implementation of the default constructor: public CombinationLock() super): II call the default constructor of the Lock class combination- Add the default constructor cdde Page 5 ls + Avant I Login er Science (AP) Perioc X m/c/MTQ5OTYXNDgxODRa public CombinationLock) super): Il call the default constructor of the Lock class combination- . Add the default constructor code to the Combinatlonl ock class The second constructor we are going to create wll allow a user to assign a combination to the object at the implementation: time it s instaaed. Again, we will use the keyword super to callthe conructor in the Locok class. Hore is its public CombinationLock(String combo) super); combination combo The call to super() is not really needed here. By default if you do not call a s's constructor in the subclass constructor, the compiler will call the default constructor of the superclass automatically. In essence, the compiler will insert a super( ) in your constructor for you. However, if you wish to call a constructor in the superclass that is not the default construct explicitly call the constructor yourself using super. Example: or, then you must super( n ) . Add the code for the second constructor to the CombinationLock class Test Class We have finished the implementation of the CombinationL ock class. The next step is to test our class to make sure it works the way we intended. To do this we are going to create a third class named CombinationLockTest . Create a source file named CombinationLockTest.java Copy the code below into this file java.util. public class CombinationLockTest public static void main(Stringl) args) CombinationLock myLock new CombinationLock "11-22-33") current System.out.printin() myLock.open); System.out.printin(myLock) myLock.close): System.out.printin) System out printin("Change combination to 55-66-77% System.out.print( Type new combination - Scanner keyboard new Scanner(System.in) String combo keyboard.nextLine( omputer Science (AP) Penoc x- Avant l Login gle.com/c/MTQSOTYXNDgxODRa myLock.close0: System.out printin); System.out.printin ("Change combination to 55-66.77% System.out.print( Type new combination>): Scanner keyboard new Scanner(System.in): String combo keyboard.nextLine0: myLock.setCombination(combo); System.out.printin"Combination has been changed."); System.out.printin(): myLock.open System.out.printin(myLock) myLock.close): System.out.printin): System.out.printin( Type an incorrect combination.") myLock.open): System.out.printin(myLock) myLock.close: . Compile and execute your program. You can compile each class separately or all at one time using the compile project button. Sample Run The current combination is 11-22-33 Enter combination 11-22-33 Lock is open Combination 11-22-33 Change combination to 55-66-77 ype new combination->55-66-77 Combination has been changed. Enter combination-55-66-77 Lock is open Combination 55-66-77 Type an incorrect combination. Enter combination->99-99-99 Lock is closed Combination 55-66-77

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions