English subtitles

← Arithmetic Operations 2 - Intro to Java Programming

Get Embed Code
6 Languages

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

  1. Initially, if I compile and run the tester, all
  2. of the methods are actually returning zeros. Let's fix
  3. the amdahl Speedup first. Right now, it's returning 0.
  4. What if I just write it in sort of the
  5. way it is? Now, if I run the tester
  6. again, I'm getting an actual of 2.875. When I
  7. expected 1.6. I think I'd better go back and
  8. fix it. Right now, this is doing the divisions first,
  9. so 1 over s, and s over n.
  10. And then the additions and subtractions in the middle.
  11. And there's no real sense of the fraction
  12. that we started with. This whole piece From s
  13. to n should stay together in the denominator,
  14. and the 1 minus s needs to stay together
  15. in its numerator. Let's try this again. Alright, looks
  16. like Amdalh/s speed up is working, but we still
  17. need to do the cross ratio. I'll try
  18. doing this sort of naively again. I've written this
  19. as it looks but flattened it onto one line.
  20. So, if I return this quantity and I compile
  21. I'm getting an error, unexpected type, required class found
  22. value. This error probably doesn't make a whole lot
  23. of sense to yet but this is a little
  24. hint of what's to come. A pair of parentheses
  25. like this immediately followed by some other quantity has another meaning.
  26. In particular, if you put a type in here instead of
  27. an expression like a minus c. What I need to indicate
  28. is that I want to multiply, and I'll need to do it
  29. over here as well. And now if I try to run
  30. the tester again I'm definitely not getting right answers for the cross
  31. ratio. This is because when we have a bunch of operators
  32. with the same precedents Travel will just start from the left and
  33. move to the right so instead of computing a
  34. minus c times b minus d over in parentheses b
  35. minus c times a minus d. This will do a minus c times b minus d divided by b
  36. minus c and multiply that entire quantity by a
  37. minus d. What I need to do here, is group
  38. the denominator. I could also group the numerator, if
  39. I really wanted to but it wouldn't make much of
  40. a difference. If I compile again, and run the tester, I've
  41. now got two methods working right. Now for the average. I might try retuning
  42. the sum of all of these, all divided by four. Let's
  43. see how this works. If I run the tester, it looks like it works in one case, but
  44. not in the other. Let's look at the second
  45. case. I'm going to go read inside of the tester.
  46. The case that isn't working is when we try
  47. to take the average of 3, 4, 3, and 3.
  48. We should, in fact, expect 3.25, but it looks
  49. like we're losing the decimal. Java is interpreting this as
  50. integer division, because 4 is an integer, and a,
  51. b, c and d are all declared as integers. There
  52. are a few ways I could fix this. I could write 4 as 4., or 4.0 And then it would
  53. get the right answer here. Or I could actually change
  54. all of these ints to doubles, and then I wouldn't actually
  55. need to specify that the 4 was a double. I
  56. can compile this and run the tester, and it still works.
  57. That's because if any of these variables are a double,
  58. this whole expression in parentheses is going to come out as a
  59. double, and a double divided by an int doesn't need
  60. to be done with integer division, that calls for regular division.
  61. This is a really easy thing to mix up. It
  62. looks right to us, but the computer's going to read
  63. it wrong and the compiler won't warn you. When we
  64. ran this with all ints and no doubles we saw that
  65. it ended up being a run time error. This is
  66. a good example of why it's helpful to think about what
  67. you want your answer to be beforehand. Calculate a couple
  68. examples and then write your code. Good book on this quiz.