English subtitles

← Using Variables

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. In the next set of quizzes,
  2. I am going to be showing you how
    variables are actually used.
  3. All the programs that you are about
    to do in part 2, will compile.
  4. Which means that the computer can
    understand the code that's been written
  5. for them, and
    we'll be able to create an app.
  6. Now, while the programs will
    be able to run on the phone,
  7. there's a chance that they might
    not do what the intended goal was.
  8. This is something known
    as a logical error.
  9. A logical error means that there's
    something wrong with the logic which you
  10. used to write the code.
  11. Let's look at a short program and
  12. an example scenario and I'll walk you
    through how you solve one of these.
  13. Okay, so here's an example.
  14. Each of these programs will have a goal,
    which will be written out here.
  15. The program itself will
    be in the green box.
  16. Okay, so the goal of this program is to
    calculate something called sleep debt.
  17. Now, how much sleep you need varies
    a little bit by age, but on average,
  18. people need about eight
    hours of sleep a night.
  19. So what this program is trying to do is
    calculate the sleep debt in one week.
  20. The user of the program will go ahead
    and estimate how much they sleep on
  21. an average weekday, and how much they
    sleep on an average weekend day.
  22. For this example, we're assuming the
    user sleeps five hours on a weekday and
  23. nine hours on a weekend.
  24. The code for
    this task is here in the green box.
  25. And the first thing that you're trying
    to do is read through this code and
  26. decide whether or
    not the goal was accomplished.
  27. Then what you're also going to do is
    go through each of the statements that
  28. happened in the code and decide what
    was actually displayed to the screen.
  29. Assume that these method calls here,
    the things that look like display and
  30. have parentheses and
  31. something in between them,
    will display content to the screen.
  32. Now, some of these problems will have
    multiple pieces of content that they
  33. need to display, so you might see
    display1, display2, display3.
  34. Okay, so
    if you're still watching this video,
  35. you're probably wondering how
    to solve one of these problems.
  36. This isn't the first time you're
    interacting with multiple variables,
  37. the values of which might change
    a few times during the problem.
  38. So let me do the first one with
    you before I set you loose.
  39. Now I'm going to use a technique called
    hand simulation and what this technique
  40. involves is trying to figure out
    what the computer is doing, but
  41. without just sticking
    it to the computer.
  42. For us really it's just an organized way
    to keep track of what's going on with
  43. the variables.
  44. Okay, now you're going to go line by
    line so let me start this with you.
  45. In the first line here I see
    a declaration of a variable, so
  46. I'm going to go ahead and write
    the variable name which is weekday.
  47. It's also initialized
    to a value which is 5,
  48. so under weekday I'm
    going to put the value 5.
  49. This is representing that the variable
    weekday currently stores 5.
  50. Okay, so that was the first line,
    I'm going to move to the next line.
  51. We have a variable called weekend,
    so I'm going to write that out,
  52. and the value of that variable is 9.
  53. Okay, so that's looking good so
  54. I'm going to move on to the next
    line which is a declaration and
  55. instantiation of the variable
    optimalHours, so I'll do the same thing.
  56. Okay, so
    I'm not going to write the value 7*8.
  57. I'm actually going to calculate
    that out because that's
  58. what gets stored in optimalHours.
  59. So, with a little bit of mental math,
    7 times 8 is 56.
  60. Okay, we have another declaration here.
  61. We make a variable called actualHours,
    so I'll write that out.
  62. Well, it's equal to
    the value of weekday, and
  63. this is the current value of weekday.
  64. It's not some past value of weekday or
    future value of weekday.
  65. Now, we're keeping track of what the
    current value of weekday is over here.
  66. And so I could just look here and
    see that it's 5.
  67. So I should really think
    about this as the number 5.
  68. So it's saying int
    actualHours is equal to 5, so
  69. I'm going to go ahead and
    write that there.
  70. OK we'll move on to the next line.
  71. So now we're changing actual hours
    again to be equal to the current value
  72. of actualHours plus weekend times 2.
  73. So I can look over here, the current
    value of actualHours is 5 and
  74. the current value of weekend is 9.
  75. And now I have this
    little equation here.
  76. So, I do the multiplication
    before the addition, so,
  77. 9 times 2 is 18 and then I add 5.
  78. And with a little bit more mental math,
    18 plus 5 is 23.
  79. Okay, so this all equals 23.
  80. And what I have to do is cross out
    the old value of actual hours, and
  81. write in the new value of actual hours.
  82. Okay, so that's done.
  83. Let's move on to the next line.
  84. We have n solution, so
    that's a variable declaration.
  85. And it's being set equal to something,
  86. I'm going to write out
    the word solution first.
  87. And solution is equal to optimal
    hours minus actual hours.
  88. So let's go over here, optimal
    hours is 56 and actual hours is 23.
  89. Now it's okay to use your calculator,
  90. I happen to do that earlier and
    I know that 56 minus 23 is 33.
  91. Okay, and
    then we move on to the next and
  92. final line which says display solution.
  93. So, we can look over here to see what
    the value of solution is, which is 33.
  94. So it is going to display the number 33.
  95. So that is how I, or you, or
  96. anyone, could figure out
    what this program just did.
  97. But the real question is,
    did it do the right thing?
  98. Is there a logical error
    somewhere in here or
  99. is somebody who sleeps five hours on
    a weekday and nine hours on a weekend
  100. night going to have thirty three hours
    of sleep debt at the end of the week.
  101. And I want to know what you think.
  102. All right, quick side note, now you've
    created this practice set 2 app, so
  103. I'm just going to show you how you could
    also put the code into the practice
  104. set 2 app if you wanted to test it out.
  105. I really recommend
    hand simulating first,
  106. because it's going to give your brain
    a better grasp about what's going on.
  107. But it's also go to be able to put
    the code into Android Studio and
  108. play around with it a bit.
  109. So my suggestion is that you copy and
    paste the code for the problem,
  110. which will be provided in the instructor
    notes, into the onCreate method.
  111. That's the first method you'll see
    in your main activity class here.
  112. Okay, just to be very clear,
    here's what your onCreate looks like
  113. before it's modified and then you can
    paste over the code, like I've done.
  114. Okay, the other thing you're going to
    need to copy and paste over,
  115. which will make this line of code work,
    is down here.
  116. It is this code, and I've also
    included this in the instructor notes.
  117. The one final thing that you'll
    need to do is go to your XML, and
  118. make sure your that your text
    view has an Id/display_text_view,
  119. and this should exactly
    match what's shown here.
  120. Ok, as long as those two
    things match when you run this
  121. code, you'll get the value
    printed out to the screen.
  122. It's a little bit hard to see but
    it says 33 right there.
  123. Okay, and with all of that,
    then I'll try to hand simulate and
  124. some help with getting it up and
    running on your phone.
  125. Go ahead and try to figure out is this
    program doing what it's supposed to do.