1 00:00:00,540 --> 00:00:02,340 ‫Instructor: Hi, within this lecture, 2 00:00:02,340 --> 00:00:05,820 ‫we're going to continue building our calculator app. 3 00:00:05,820 --> 00:00:07,140 ‫So in order to do that, 4 00:00:07,140 --> 00:00:10,530 ‫I'm going to write the first method over here, 5 00:00:10,530 --> 00:00:12,450 ‫which is our sum method. 6 00:00:12,450 --> 00:00:16,050 ‫So first of all, we need to get the strings 7 00:00:16,050 --> 00:00:20,340 ‫or the numbers that our user has given as an input 8 00:00:20,340 --> 00:00:22,650 ‫and we need to add them together 9 00:00:22,650 --> 00:00:26,550 ‫and then finally, show the result 10 00:00:26,550 --> 00:00:30,300 ‫in a text view in the result text to the user. 11 00:00:30,300 --> 00:00:32,880 ‫We already have number1Text over here 12 00:00:32,880 --> 00:00:36,750 ‫and we can reach it under sum method, right? 13 00:00:36,750 --> 00:00:40,623 ‫Now, if I write something like this, number1Text, 14 00:00:41,520 --> 00:00:44,340 ‫as you can see, there is a method called getText. 15 00:00:44,340 --> 00:00:48,390 ‫And if you see the right side of it, it's editable. 16 00:00:48,390 --> 00:00:50,460 ‫So this is kind of a custom class. 17 00:00:50,460 --> 00:00:53,010 ‫It's not a string, it's not an integer, 18 00:00:53,010 --> 00:00:56,760 ‫it's not a data type that we are familiar with. 19 00:00:56,760 --> 00:00:58,140 ‫So you cannot edit, 20 00:00:58,140 --> 00:01:01,260 ‫you cannot do what you can do with strings. 21 00:01:01,260 --> 00:01:04,650 ‫It's a custom class but luckily, 22 00:01:04,650 --> 00:01:07,590 ‫we can convert it to string. 23 00:01:07,590 --> 00:01:11,130 ‫Now, we need to convert it into a string 24 00:01:11,130 --> 00:01:13,380 ‫so that we can do something with it. 25 00:01:13,380 --> 00:01:16,050 ‫We cannot do much with editable. 26 00:01:16,050 --> 00:01:18,930 ‫So this is a class in our class created 27 00:01:18,930 --> 00:01:21,780 ‫in order to write some text 28 00:01:21,780 --> 00:01:25,770 ‫or get some text out of the edit text. 29 00:01:25,770 --> 00:01:27,690 ‫So if you do that, 30 00:01:27,690 --> 00:01:31,080 ‫you can see toString method over here. 31 00:01:31,080 --> 00:01:33,390 ‫So this is what we are looking for. 32 00:01:33,390 --> 00:01:38,390 ‫This is how we get strings out of the edit text. 33 00:01:38,580 --> 00:01:41,100 ‫Now, I can actually make this equal 34 00:01:41,100 --> 00:01:45,870 ‫to into something called myString, for example. 35 00:01:45,870 --> 00:01:48,442 ‫And since this is a string, 36 00:01:48,442 --> 00:01:51,120 ‫I can get the other string as well 37 00:01:51,120 --> 00:01:53,640 ‫but I cannot add them together 38 00:01:53,640 --> 00:01:56,820 ‫because they're not numbers, right? 39 00:01:56,820 --> 00:02:01,260 ‫In order to do that, I can create some kind of an integer 40 00:02:01,260 --> 00:02:06,260 ‫or a double or a float out of those strings. 41 00:02:06,330 --> 00:02:08,130 ‫So you don't know how to do that. 42 00:02:08,130 --> 00:02:09,960 ‫I'm gonna show you, don't worry. 43 00:02:09,960 --> 00:02:12,780 ‫But you know, you get the logic, right? 44 00:02:12,780 --> 00:02:15,090 ‫You cannot add two strings together, 45 00:02:15,090 --> 00:02:18,450 ‫you can actually add them but it's nothing more 46 00:02:18,450 --> 00:02:21,780 ‫than concatenating two strings together, 47 00:02:21,780 --> 00:02:24,240 ‫like adding one next to each other. 48 00:02:24,240 --> 00:02:26,670 ‫But this is not what I want to do. 49 00:02:26,670 --> 00:02:29,790 ‫I want to add two numbers together. 50 00:02:29,790 --> 00:02:34,080 ‫So I have to create a number out of this string, 51 00:02:34,080 --> 00:02:37,380 ‫and you can actually convert one type into another 52 00:02:37,380 --> 00:02:41,130 ‫if they are suitable, if they're a fit for each other. 53 00:02:41,130 --> 00:02:44,670 ‫For example, if you have a string like five, 54 00:02:44,670 --> 00:02:47,970 ‫you can convert it into an integer. 55 00:02:47,970 --> 00:02:49,530 ‫So in order to do that, 56 00:02:49,530 --> 00:02:53,160 ‫I'm going to use Integer class like this. 57 00:02:53,160 --> 00:02:57,180 ‫So write Integer and if you say .parse, 58 00:02:57,180 --> 00:03:00,090 ‫you can see parseInt method 59 00:03:00,090 --> 00:03:03,900 ‫and it actually requires a string as a parameter. 60 00:03:03,900 --> 00:03:05,670 ‫So this is what we are going to use. 61 00:03:05,670 --> 00:03:08,370 ‫We're going to use this parseInt method 62 00:03:08,370 --> 00:03:12,090 ‫and we're going to give our string as a parameter. 63 00:03:12,090 --> 00:03:16,110 ‫It will convert our string into an integer. 64 00:03:16,110 --> 00:03:18,780 ‫Okay? And we don't have to do that anymore. 65 00:03:18,780 --> 00:03:21,150 ‫So let me delete everything from here, 66 00:03:21,150 --> 00:03:24,840 ‫and start by writing Integer.parseInt. 67 00:03:24,840 --> 00:03:28,230 ‫It will ask me for this string. 68 00:03:28,230 --> 00:03:30,420 ‫I'm going to cut this from here. 69 00:03:30,420 --> 00:03:32,580 ‫I'm not gonna take the semicolon, 70 00:03:32,580 --> 00:03:34,170 ‫and add it over here. 71 00:03:34,170 --> 00:03:38,100 ‫And this is what we're gonna use in order 72 00:03:38,100 --> 00:03:42,930 ‫to get an integer out of our editText. 73 00:03:42,930 --> 00:03:47,883 ‫So I'm going to assign this to a variable called number1. 74 00:03:48,720 --> 00:03:52,080 ‫And of course, we can do that for number2 as well. 75 00:03:52,080 --> 00:03:52,913 ‫Right? 76 00:03:52,913 --> 00:03:56,370 ‫Since we have two numbers, this is number one. 77 00:03:56,370 --> 00:03:58,830 ‫I'm gonna get the second one as well. 78 00:03:58,830 --> 00:04:01,800 ‫Now, I'm gonna copy this and paste it over here. 79 00:04:01,800 --> 00:04:03,570 ‫Change it to number two, 80 00:04:03,570 --> 00:04:08,370 ‫and of course, I'm gonna change the text one as well 81 00:04:08,370 --> 00:04:11,670 ‫so that I will have two numbers. 82 00:04:11,670 --> 00:04:14,670 ‫Now, I can actually create a result integer 83 00:04:14,670 --> 00:04:18,270 ‫and add them together like number1 plus number2, 84 00:04:18,270 --> 00:04:20,310 ‫and hit semicolon. 85 00:04:20,310 --> 00:04:21,143 ‫Here you go. 86 00:04:21,143 --> 00:04:24,480 ‫Now I see the final result over here. 87 00:04:24,480 --> 00:04:28,050 ‫Now, I can actually change the resultText 88 00:04:28,050 --> 00:04:31,710 ‫by saying resultText.setText. 89 00:04:31,710 --> 00:04:34,020 ‫And when you say setText, 90 00:04:34,020 --> 00:04:39,020 ‫it actually asks for a string or a character. 91 00:04:39,060 --> 00:04:40,710 ‫You cannot just come over here 92 00:04:40,710 --> 00:04:43,170 ‫and give result as a parameter 93 00:04:43,170 --> 00:04:45,330 ‫because it's an integer. 94 00:04:45,330 --> 00:04:47,640 ‫It will look like it's working 95 00:04:47,640 --> 00:04:50,490 ‫but it's gonna crash once you do that. 96 00:04:50,490 --> 00:04:54,450 ‫So you have to convert this integer into a string. 97 00:04:54,450 --> 00:04:58,860 ‫And luckily, we have a very good method in order to do that. 98 00:04:58,860 --> 00:05:02,214 ‫Once you write something like this, 99 00:05:02,214 --> 00:05:05,910 ‫write any string over here like this, 100 00:05:05,910 --> 00:05:09,180 ‫you can add another integer 101 00:05:09,180 --> 00:05:11,430 ‫or another float or another number 102 00:05:11,430 --> 00:05:14,670 ‫and it will convert it into a string. 103 00:05:14,670 --> 00:05:16,170 ‫Now this is okay. 104 00:05:16,170 --> 00:05:18,690 ‫You can actually test this. 105 00:05:18,690 --> 00:05:22,500 ‫So result plus result variable. 106 00:05:22,500 --> 00:05:23,520 ‫Okay? 107 00:05:23,520 --> 00:05:27,960 ‫So I'm going to test this on my emulator, okay? 108 00:05:27,960 --> 00:05:30,570 ‫So I suggest you do the same thing as well. 109 00:05:30,570 --> 00:05:32,010 ‫So open the emulator 110 00:05:32,010 --> 00:05:35,700 ‫and just write some random numbers and hit them together. 111 00:05:35,700 --> 00:05:38,850 ‫Here you go, result is eight. 112 00:05:38,850 --> 00:05:42,690 ‫So we can try with other numbers like 13. 113 00:05:42,690 --> 00:05:45,750 ‫Let's change it to 3,000, for example. 114 00:05:45,750 --> 00:05:49,260 ‫Here you go. Now it's working. 115 00:05:49,260 --> 00:05:52,770 ‫Of course, it's trivial to do the others as well. 116 00:05:52,770 --> 00:05:54,210 ‫I'm just gonna copy them 117 00:05:54,210 --> 00:05:56,850 ‫and paste them over here, okay? 118 00:05:56,850 --> 00:06:00,330 ‫And paste them over here and paste them over here. 119 00:06:00,330 --> 00:06:04,020 ‫So right now I can just change the sign 120 00:06:04,020 --> 00:06:06,690 ‫like this is deduction, 121 00:06:06,690 --> 00:06:09,003 ‫this is multiply and this is divide. 122 00:06:09,840 --> 00:06:11,190 ‫Here you go. 123 00:06:11,190 --> 00:06:14,640 ‫Now we are ready to test this. 124 00:06:14,640 --> 00:06:17,310 ‫So we can see the other methods, 125 00:06:17,310 --> 00:06:19,833 ‫other operations as well. 126 00:06:20,700 --> 00:06:23,610 ‫So I believe we're coming to an end 127 00:06:23,610 --> 00:06:27,150 ‫but actually, we have some more things to do, 128 00:06:27,150 --> 00:06:29,010 ‫some more things to consider. 129 00:06:29,010 --> 00:06:30,750 ‫So let me try this. 130 00:06:30,750 --> 00:06:33,720 ‫So this is 12. This is eight. 131 00:06:33,720 --> 00:06:36,240 ‫This is 20 and this is 5. 132 00:06:36,240 --> 00:06:38,880 ‫So here you go, it's now working. 133 00:06:38,880 --> 00:06:43,740 ‫Of course, we have some kind of issues in this app. 134 00:06:43,740 --> 00:06:45,720 ‫Since we're doing this for a test, 135 00:06:45,720 --> 00:06:47,340 ‫it's not very important 136 00:06:47,340 --> 00:06:50,370 ‫but it's important to practice 137 00:06:50,370 --> 00:06:52,260 ‫what we have learned so far. 138 00:06:52,260 --> 00:06:55,905 ‫For example, if you just write your name 139 00:06:55,905 --> 00:07:00,360 ‫for an input in your first edit text, 140 00:07:00,360 --> 00:07:03,210 ‫if you click any of the buttons, it will crash. 141 00:07:03,210 --> 00:07:06,060 ‫And whilst you see a screen like this, 142 00:07:06,060 --> 00:07:08,700 ‫you can go to Logcat immediately 143 00:07:08,700 --> 00:07:11,793 ‫in order to see why it's crashed. 144 00:07:12,750 --> 00:07:15,690 ‫So if you go to error section, 145 00:07:15,690 --> 00:07:18,420 ‫you can find the real error message 146 00:07:18,420 --> 00:07:19,650 ‫that you're looking for. 147 00:07:19,650 --> 00:07:20,550 ‫So here you go. 148 00:07:20,550 --> 00:07:22,380 ‫One of the error messages. 149 00:07:22,380 --> 00:07:26,340 ‫So it says that yeah, I cannot do this onClick because why? 150 00:07:26,340 --> 00:07:30,090 ‫Because there is something wrong, as you can see. 151 00:07:30,090 --> 00:07:32,430 ‫We have an input string of atil 152 00:07:32,430 --> 00:07:34,620 ‫and we tried to cast this 153 00:07:34,620 --> 00:07:38,070 ‫as an integer like Integer.parseInt 154 00:07:38,070 --> 00:07:40,260 ‫and it won't work obviously 155 00:07:40,260 --> 00:07:44,310 ‫because it's not a number, it's just a random string, 156 00:07:44,310 --> 00:07:45,783 ‫a random text. 157 00:07:46,710 --> 00:07:50,520 ‫So how do we overcome this problem? 158 00:07:50,520 --> 00:07:53,940 ‫This is one of the things that we should actually consider 159 00:07:53,940 --> 00:07:57,240 ‫when we make calculator or make any apps. 160 00:07:57,240 --> 00:08:00,213 ‫So we're going to do that within the next lecture.