Monday, September 19, 2011

Challenge 015’s Solution

In Challenge 015, Statement I creates an error message. This is because it first makes “x” a double value of 14.7. Next, it tries to say that “int y = x”. This cannot be so, as an “int” value cannot equal a non−integer number. To correct this, one must make “x” an “int” value by placing “(int)” before 14.7, yielding the output “14”. Next, Statement II correctly yields the output “14”. While this statement has “x” as a double value of 14.7, the statement has the term “(int)” in front of “x”, making it an integer value before it claims that it is equal to “int y”. Therefore, no correction is necessary for Statement II. Finally, Statement III also creates an error message. This is because it tries to say that “int x” is equal to a non−integer number of 14.7. This should (again) be corrected by placing “(int)” before 14.7. This allows the statement to produce the number “14.0”, as the number 14 can be a double value. In conclusion, one could state that “int” values can be “double” values, but “double” values cannot be “int” values; similar to how integer numbers can be rational numbers, while rational numbers cannot be integer numbers.
 
−Phoenix

3 comments:

  1. Most excellent, Phoenix, and such an important concept this is to the topic of computer science! Great work here.

    Just one item of follow-up:

    When we place (int) or (double) before a term, as in:

    int num1 = (int) 34.6675;

    What is the name of this process? One-word answer here.

    ReplyDelete
  2. The name of this process is called a “cast”.

    −Phoenix

    ReplyDelete
  3. Indeed, great work, thank you for the follow-up. 15XPs awarded for this completion.

    ReplyDelete