Answers to Lab 1

  1. Operators + and - correspond to addition and subtraction of the integers they appear between. If - doesn't occur between two numbers, it negates the sign of the number to its right (changes plus to minus, and vice-versa).
    (return to questions)

  2. Operation * performs multiplication of integers. Multiplication is conventionally carried out before addition (or subtraction), and you need parentheses to force Java to change the order and carry out the addition first.
    (return to questions)

  3. The operators / and % are for integer division: ``13 divided by 5 is 2 with a remainder of 3.'' In symbols, integer division x / y yields an integer quotient q and an integer remainder r so that x = q*y + r. Note that r is either 0 or positive if x is positive and negative if x is negative. The / operator delivers the quotient, the % operator delivers the remainder. (return to questions)

  4. There is a limit to how large and small we can make an integer in the Java programming language. The values returned by Integer.MAX_VALUE is the largest integer that can be represented in Java and Integer.MIN_VALUE is the smallest. These numbers ``roll over'' when you increase them. So if you increase the largest value, you get a very negative number, and if you decrease the smallest value you get a very positive number. In lecture, we will discuss why these limits exist. (return to questions)

  5. Because the numbers have decimal points, Java does not use integer division. Instead, it uses what is called floating point division. Any number with a decimal point is called a floating point number. (return to questions)

  6. If one of the operands of a mathematical operator is an integer and the other is a floating point number, the integer operand is converted to a floating point number before performing the operation. All rules of operator precedence still apply so (1 / 3) + 5.0 first does the division. Since both operands of the division are integers, Java does integer division yielding 0. The second operation is addition, and one of its operands is a floating point number so the integer 0 is converted to a floating point 0.0 before doing the addition. (return to questions)

  7. The first division is done with integers (9 and 5), so the result is an integer quotient, 1. You can force the division to be floating point by making one or both of the numbers a floating point number, yielding a more realistic 212 degrees F:
    9.0 / 5.0 * 100 + 32
    (return to questions)

  8. The result is approximated, rather than 3.6 you get:
    Java does not represent numbers in base 10, but instead it represents the numbers in base 2. This does not cause a problem with integers, but it does with floating point numbers. The fractional part of the number is approximated to the closest base 2 floating point number available to Java. The error is usually quite small, but the errors will accumulate when performing operations such as addition. As a result, you can get different answers to mathematically equivalent expressions. Try 1.3 + 1.3 + 1. Lesson: "equality" of floating-point calculations probably means "equal up to some tolerance." The difference between the true value and the one calculated by Java is less than $10^{-15}$. (return to questions)

  9. x is storing the integer value 5. When you use x in an expression, you get what ever value is stored in x. x is called a variable in Java. (return to questions)

  10. y is storing the floating point value 3.0. When you use y in an expression, you get what ever value is stored in y. y is called a variable in Java. (return to questions)

  11. The double in front of a means that the variable a will store a floating point number. When you try to store the integer 10 into a, the value is automatically converted to the floating point value 10.0. Likewise, the int in front of b means that b will store an integer value. When you try to store the floating point value 3.0 you get an error because 3.5 is not an integer. (return to questions)

  12. The = operator assigns values to a variable. We call the = operator the assignment operator. The expression on the right hand side of the = is evaluated to a value. If any variables appear in the right hand side, the values stored in the variables are used in the expression. Then the value of the right hand side expression is assigned to the variable on the left hand side. Notice that variables can have long names. (return to questions)

  13. Initially (on the first couple of lines) r has the value 5. In the assignment statement the right side occurs first, so r is evaluated as 5, this is added to 1, and then the entire value of this expression (6, in other words) is stored in r. So r is never ``equal'' to one more than itself, but rather 1 is added to r, and then the new value is stored in r. (return to questions)

  14. The == operator tests for equality of each operand. It is an unfortunate design decision of Java that makes == behave like mathematical = and = be an assignment operator. This decision has lead to a large number of bugs in Java programs. (return to questions)

  15. The variable celsius can't be used until it has been declared by a declaration statement. Once that happens it can hold and use the values 100 in one expression, and 32 in another expression. (return to questions)

  16. Java will not allow you to re-declare a variable that has already been declared. After clearing the old definition with the ``Reset'' button, you can declare celsius as a double. Now that you see how Reset works, be prepared to use it during the rest of the lab.

    Note that in Java you must give a variable a value before you can use it. The DrJava interactions pane is forgiving of this error because it always gives a variable an initial value of 0. However, you need to remember that in Java, you must declare a variable and assign a value to it before you can use it. Don't worry if you forget, a Java error message will remind you! (return to questions)

  17. Integers can be assigned to doubles, since a number with a 0 fractional part has the same information as an integer. On the other hand, doubles can not be assigned to integers, since this would result in the loss of the information about the fractional part. (Note, even if the double has a .0 fractional part, it still can not be assigned to an int. (return to questions)

  18. Java refuses to assign a double in an int -- information will be lost. However, you can apply the typecast operator to a double so that it will be evaluated as an integer (the fractional portion is truncated). In this case, Java allows you to assign an integer to an integer. Applying the typecast to dNum changes the way it is evaluated in that particular expression, but has no effect on the value stored in dNum. (return to questions)

  19. Unary operators such as - and typecast -- (int) -- are applied first. Then multiplication, division, and modulus (%) are carried out, from left to right. Finally addition and subtraction are carried out, from left to right. (return to questions)

  20. A char variable can store a character value. Characters are represented by letters or symbols inside single quotes. Java actually represents these characters as integers so we can store the character in an int variable where the value will be interpreted as an integer. However, we can not store an integer in a char variable without a type cast. Try changing the last expression to
    c = (char) (d + 1)
    There are about 65,000 characters, and not all of them can be displayed in all windows, so you may get some strange results doing arithmetic on char. (return to questions)

  21. A boolean expression can be evaluated to either true or false. Just as with other values and types, we can store the result of a boolean expression into a variable that has type boolean. Three is less than five, so truthVal is assigned the value true. However three is not greater than five, so truthVal is then assigned the value false. The expression to the right of the "=" operator is evaluated to either true or false, and then stored in the variable to the left of the ``='' operator. (return to questions)