Inheritance Objective: The objective from this lab is to understand the importance of the inheritance concept in an object-oriented language (Java). Part#1 keyboard simulator: This lab is one of many labs that handle the creation of the keyboard simulator program. As you know, all keyboards talk to the computer using zeros and ones, which is not what we are going to do in this lab. This means that there is a special code for every button you press on the keyboard. So, the simulator main loop is pressing a button and generating a series of codes that the computer is translates into a value we see on the screen. Therefore, if we know the code of all the characters, we want the computer to show on screen, we can mimic the code sending to computer and the characters printing on the screen. In fact, we will give codes to all the characters, the ones we need, and if we input the right code, the keyboard simulator should print the corresponding character on the screen. Here is a sample of what the output should look like. 1->Backspace 11->h 21->r 31->1 12->Sapce | 12->i 22-> 132->2 13->) 123->t 133->3 14-> |24-> 34-41 15->1 25- 35-5 16-) 126-W 36-6 (17->n 27-> 37-7 18-> (28-> 138->8 19->p (29-> 139-9 10-> 120-> 30-> Please enter the code of the button: 8 Texted entered: 3->Enter Is-ob 16-> 7-> 18-> 19-> Please enter the code of the button: 3 Texted entered: ee Please enter the code of the button: 33 Texted entered: lee3 Please enter the code of the button: 36 Texted entered: ee36 Please enter the code of the button: 34 Texted entered: lee 364
1. 1+1 As you can see in the previous example the program starts with the table that shows all the characters in our keyboard (QWERTY keyboard) and the code for each one. After that, the program waits for the user to enter the code of the selected character and print the corresponding character between two the dash-lines. Every input is added in one place displayedText). As in every keyboard, there are 3 general 3-e 10-> buttons (Backspace, Space, Enter). Therefore, all 11->1 111-> 2->2 112->/ the keyboards you are going to write in this lab 3->3 13-> should have these general buttons. In this lab, you -->4 (14-> shall write two classes that mimic the actions of $->5 115-> 6.56 116->) two keyboard (QWERTY keyboard and Calculator ->7 17->Backspace keyboard). The buttons and the codes of the 3->8 18->Sapce QWERTY keyboard is shown in the above output ->9 19->Enter sample. The calculator keyboard output should please enter the code of the button: 1 Texted entered: look like the sample on the right The calculator keyboard acts a bit different than the QWERTY keyboard. The codes from 0 to 9 Please enter the code of the button: 10 are reserved for numbers from 0 to 9. The user Texted entered: input the expression, one by one, and the calculator does nothing until the user enters the equal sign (=). Then the calculator evaluates the Please enter the code of the button: 1 Texted entered: expression, prints the result next to the expression and jumps to a new line. The story does not end here! The user can enter another expression in the same way and ask for the results. The calculator is Please enter the code of the button: 14 Texted entered: smart enough to distinguish the current expression from the previous one. They should not be mixed. 1+1- 2 Since both keyboards should store two arrays, one for the codes list and one for the values please enter the code of the button: list, there should be one object that holds the data for both of them and acts relatively the same. This 1+1= 2 object is an instance of the keyboard class. This instance can store the two lists, act with the Please enter the code of the button: 13 pressed buttons, add new character to the stored Texted entered: text (called displayed Text in keyboard class), and return the two lists. 1+1-2 The action of pressing a button is simply Please enter the code of the button: 5 accepting the entered code, looking for the code in Texted entered: the code list, getting the corresponding value of the button from the characters list and adding the 1+1-2 new character to the displayed text. This action is forwarded from the two class (QWERTY keyboard Please enter the code of the button: 14 and Calculator keyboard). However, the button Texted entered: pressed method in the calculator class checks if the entered value is the equal sign (=). If so, then it evaluates the expression using the evaluate method in the Evaluate String class (one of the provided classed). Finally, the button pressed method adds the result to the displayedText in the keyboard class. Texted entered: sº 55 1+12 55- 25
The constructor of the two keyboards (QWERTY keyboard and Calculator keyboard) should create the two arrays with fixed characters (as shown in the sample output). Then creates the instance from the Keyboard class that accept those to lists and store them. In addition, the keyboard class constructor should generate the general buttons and add them to the list of buttons with their own codes. The value of the general buttons should be defined as an enumeration constant and used all over the program. Here is the UML diagram for all the classes. <<Java Class>> <Java Class>> Calculator Keyboard LAB2_2 LAB2_2 mainkeyboardObj. Keyboard buttonCodeList. ArrayList<String> o buttonCodeList ArrayList<String> button ValueList: ArrayList<String o button ValueList ArrayList<String - displayedText: String of Calculator) of Keyboard(ArrayList<String> ArrayList<String>> buttonPressed(String):void getButtonCodeindex(String); int getDisplayedText():String buttonPressed(String):void getAlButtons Value() ArrayList<String> backspace():void getAlButtons Code(): ArrayList<String space():void newLine():void getAliButtons Value()ArrayList<String> <<Java Class>> QWERTY getAllButtons Code():ArrayList<String LAB2_2 getDisplayedText(: String addToDisplayed Text(String):void a mainkeyboardObj: Keyboard button ValueList. ArrayList<String> a buttonCodeList ArrayList<String <Java Enumeration>> QWERTYO SpecialButtons LAB2_2 buttonPressed(String):void getDisplayedText():String Sapce: SpecialButtons getAlButtons Value(): ArrayList<String> Backspace: SpecialButtons getAlButtonsCode(): ArrayList<String> Enter SpecialButtons SpecialButtons Note: You should use the Table Viewer class, the EvaluationString class and the Testing class in your work for this lab.
«Java Class>> Calculator LAB2_2 mainkeyboardObj: Keyboard o buttonCodeList ArrayList<String> o button ValueList. ArrayList<String> Calculator button Pressed(String):void getDisplayedText():String getAllButtons Value(): ArrayList<String> getAllButtonsCode(): ArrayList<String> «Java Class>> Keyboard LAB2_2 buttonCodeList. ArrayList<String> buttonValueList: ArrayList<String> displayedText: String Keyboard(ArrayList<String> ArrayList<String>> getButtonCodeIndex(String); int buttonPressed(String) void backspace():void space():void newLine():void getAllButtonsValue(): ArrayList<String> getAllButtonsCode(): ArrayList<String> getDisplayedText(): String addToDisplayed Text(String):void <Java Class>> QWERTY LAB2_2 mainkeyboardObj: Keyboard buttonValueList ArrayList<String> buttonCodeList ArrayList<String> QWERTYO buttonPressed(String):void getDisplayedText():String getAllButtons Value():ArrayList<String> getAll Buttons Code():ArrayList<String> «Java Enumeration>> SpecialButtons LAB2_2 Sapce: SpecialButtons SF Backspace: SpecialButtons Enter. SpecialButtons SpecialButtons
The objective from this lab is to understand the importance of the inheritance concept in an object-oriented language (Java). Part#1 keyboard simulator: This lab is one of many labs that handle the creation of the keyboard simulator program. As you know, all keyboards talk to the computer using zeros and ones, which is not what we are going to do in this lab. This means that there is a special code for every button you press on the keyboard. So, the simulator main loop is pressing a button and generating a series of codes that the computer is translates into a value we see on the screen. Therefore, if we know the code of all the characters, we want the computer to show on screen, we can mimic the code sending to computer and the characters printing on the screen. In fact, we will give codes to all the characters, the ones we need, and if we input the right code, the keyboard simulator should print the corresponding character on the screen. Here is a sample of what the output should look like.
1->Backspace (11->h 21->r 31->1 12->Sapce |12->i (22-> (32->2 3->Enter (13->j 23->t 33->3 | 4->a (14->k 24->u 34-4 | 5-> 15->1 25-> (35->5 16-> | 16->m 26->W 36->6 7->d (17->n 27->x 37->7 | 8->e (18-> (28->y (38->8 | 9->f 19->p 29->z 39->9 | 10-g 120-> 30-01 Please enter the code of the button: 8 Texted entered: Please enter the code of the button: 8 Texted entered: ee Please enter the code of the button: 33 Texted entered: ee3 Please enter the code of the button: 36 Texted entered: ee36 Please enter the code of the button: 34 Texted entered: ee364
. As you can see in the previous example the program starts with the table that shows all the characters in our keyboard (QWERTY keyboard) and the code for each one. After that, the program waits for the user to enter the code of the selected character and print the corresponding character between two the dash-lines. Every input is added in one place (displayed Text). As in every keyboard, there are 3 general - 10-> 1->1 21-5- buttons (Backspace, Space, Enter). Therefore, all -->2 112->/ the keyboards you are going to write in this lab -->3 13- should have these general buttons. In this lab, you 1-4 14-) shall write two classes that mimic the actions of ->5 115-> -6 116->) two keyboard (QWERTY keyboard and Calculator ->7 17-Backspace keyboard). The buttons and the codes of the 3->8 28->Sapce QWERTY keyboard is shown in the above output ->9 19-> Enter sample. The calculator keyboard output should please enter the code of the button: 1 Texted entered: look like the sample on the right. The calculator keyboard acts a bit different than the QWERTY keyboard. The codes from 0 to 9 Please enter the code of the button: 10 are reserved for numbers from 0 to 9. The user Texted entered: input the expression, one by one, and the calculator does nothing until the user enters the equal sign (=). Then the calculator evaluates the please enter the code of the button: 1 expression, prints the result next to the expression Texted entered: and jumps to a new line. The story does not end here! The user can enter another expression in the same way and ask for the results. The calculator is Please enter the code of the button: 14 Texted entered: smart enough to distinguish the current expression from the previous one. They should not be mixed. 2+1-2 Since both keyboards should store two arrays, one for the codes list and one for the values Please enter the code of the button: s Texted entered: list, there should be one object that holds the data for both of them and acts relatively the same. This 11- 2 object is an instance of the Keyboard class. This instance can store the two lists, act with the Please enter the code of the button: 13 pressed buttons, add new character to the stored Texted entered: text (called displayedText in keyboard class), and return the two lists. 1-1-2 The action of pressing a button is simply accepting the entered code, looking for the code in exted entered: Please enter the code of the button: 5 the code list, getting the corresponding value of the button from the characters list and adding the 1+1= 2 new character to the displayed text. This action is forwarded from the two class (QWERTY keyboard please enter the code of the button: 14 and Calculator keyboard). However, the button Texted entered: pressed method in the calculator class checks if the 1-1-2 entered value is the equal sign (=). If so, then it evaluates the expression using the evaluate method in the Evaluate String class (one of the provided classed). Finally, the button pressed method adds the result to the displayed Text in the keyboard class Þ's 55 25
10-> 18->+ 11->1 |11-) 12->2 12->/ 3->3 13-> 4-14 14-> 5 - >5 15-> 16->6 16->) 7->7 17->Backspace 18->8 18->Sapce 19-29 19->Enter Please enter the code of the button: 1 Texted entered: Please enter the code of the button: 10 Texted entered: 1+ Please enter the code of the button: 1 Texted entered: 1+1 Please enter the code of the button: 14 Texted entered: 1+1= 2 Please enter the code of the button: 5 Texted entered: 1+12 5 Please enter the code of the button: 13 Texted entered: 1+1= 2 15 Please enter the code of the button: 5 Texted entered: 1+1= 2 $5 Please enter the code of the button: 14 Texted entered: 11+1= 2 15*5= 25
LAB2_2 mainkeyboardObj: Keyboard o buttonCodeList: ArrayList<String> o button ValueList: ArrayList<String> of Calculator button Pressed(String):void getDisplayedText():String getAllButtonsValue():ArrayList<String> getAllButtons Code():ArrayList<String> <<Java Class>> QWERTY LAB2_2 mainkeyboardObj: Keyboard .button ValueList: ArrayList<String> .buttonCodeList: ArrayList<String> QWERTYO button Pressed(String):void getDisplayedText():String getAllButtonsValue():ArrayList<String> getAllButtonsCode():ArrayList<String>
<<Java Class>> Keyboard LAB2_2 buttonCodeList: ArrayList<String> button ValueList: ArrayList<String> displayedText: String Keyboard(ArrayList<String> ArrayList<String>> getButtonCodeIndex(String):int button Pressed(String):void backspace():void space():void newLine():void getAllButtonsValue():ArrayList<String> getAllButtons Code():ArrayList<String> getDisplayedTexto:String addToDisplayedText(String):void
<<Java Enumeration> SpecialButtons LAB2_2 SF Sapce: SpecialButtons SF Backspace: SpecialButtons Enter: SpecialButtons SpecialButtons() S.
The constructor of the two keyboards (QWERTY keyboard and Calculator keyboard) should create the two arrays with fixed characters (as shown in the sample output). Then creates the instance from the Keyboard class that accept those to lists and store them. In addition, the keyboard class constructor should generate the general buttons and add them to the list of buttons with their own codes. The value of the general buttons should be defined as an enumeration constant and used all over the program. Here is the UML diagram for all the classes. <<Java Class>> <<Java Class>> Calculator Keyboard LAB2_2 LAB2_2 mainkeyboardObj: Keyboard - buttonCodeList. ArrayList<String o buttonCodeList: ArrayList<String .button ValueList ArrayList<String> o button ValueList: ArrayList<String> - displayedText: String of Calculator of Keyboard(ArrayList<StringArrayList<String>) buttonPressed(String):void getButtonCodeindex/String)int getDisplayedText(): String button Pressed(String) void getAlButtons Value():ArrayList<String> backspace():void getAlButtons Code():ArrayList<String> space():void newLine():void getAlButtonsValue():ArrayList<String> <<Java Class>> QWERTY getAllButtons Code():ArrayList<String getDisplayed Text(): String LAB2_2 addToDisplayedText(String} void mainkeyboardobi Keyboard button ValueList. ArrayList<String> .buttonCodeList ArrayList<String> «Java Enumeration> QWERTYO SpecialButtons LAB2_2 buttonPressed(String) void getDisplayedText():String Sapce SpecialButtons getAllButtons Value(): ArrayList<String> Backspace: SpecialButtons getAllButtonsCode():ArrayList<String> Enter. SpecialButtons SpecialButtons Note: You should use the TableViewer class, the EvaluationString class and the Testing class in your work for this lab.