Exercises - SIUE Computer Science

Exercises: 1. Write a program that demonstrates the approximate nature of
floating-point values by performing the following tasks: ? Use Scanner to read a
floating-point value x. ... Subtract 1 from the product of x and y and display the
result. .... If the int variable x contains 10, what will the following Java statements
display?

Part of the document


Exercises:

1. Write a program that demonstrates the approximate nature of floating-
point values by performing the following tasks:
. Use Scanner to read a floating-point value x.
. Compute 1.0 / x and store the result in y.
. Display x, y, and the product of x and y.
. Subtract 1 from the product of x and y and display the result.
Try your program with values of x that range from 2e-11 to 2e11. What can
you conclude?

|Solution: |
| |
|See the code in Approximation.java. You cannot count on |
|mathematical identities to always be true. For example, x times |
|1/x may not be equal to 1. You should avoid testing for equality |
|with floating point numbers. |

2. Write a program that demonstrates type casting of double values by
performing the following tasks:
. Use Scanner to read a floating-point value x.
. Type cast x to an int value and store the result in y.
. Display x and y clearly labeled.
. Type cast x to a byte value and store the result in z.
. Display x and z clearly labeled.
Try your program with positive and negative values of x that range in
magnitude from 2e-11 to 2e11. What can you conclude?

|Solution: |
| |
|See the code in TypeCaster.java. As long as the value is not too|
|large, then the type cast acts like truncation. If the value is |
|large enough, however, the result of the type cast is |
|unpredictable. |



3. Write a program that demonstrates the operator % by performing the
following tasks:
. Use Scanner to read a floating-point value x.
. Compute x % 2.0 and store the result in y.
. Display x and y clearly labeled.
. Type cast x to an int value and store the result in z.
. Display x, z, and z % 2 clearly labeled.
Try your program with positive and negative values of x. What implications
do your results have for deciding whether a negative integer is odd?

|Solution: |
| |
|See the code in ModTester.java. If we compute the mod of a |
|positive number, the result is either zero or negative. If we |
|compute the mod of a negative number, the result is either zero or|
|positive. Mathematically, when we mod an integer by 2, we should |
|get 1. But, because of the way mod works with positive and |
|negative values, we can not just check to see if the value of the |
|mod is 1. |



4. If u = 2, v = 3, w = 5, x = 7, and y = 11, what is the value of each of
the following expressions, assuming int variables?
. u + v * w + x
. u + y % v * w + x
. u++ / v + u++ * w

|Solution: |
| |
|u + v * w + x |
|is 2 + 3 * 5 + 7 |
|is 2 + 15 + 7 |
|is 24 |
|u + y % v * w + x |
|is 2 + 11 % 3 * 5 + 7 |
|is 2 + 2 * 5 + 7 |
|is 2 + 10 + 7 |
|is 19 |
| |
|u++ / v + u++ * w |
|is 2 / 3 + 3 * 5 |
|is 2 / 3 + 3 * 5 |
|is 0 + 15 |
|is 15 |
| |
|This code is in Fragments.java. |

5. What changes to the ChangeMaker program in Listing 2.3 are necessary if
it also accepts coins for one dollar and half a dollar?

|Solution: |
| |
|Add variables. |
|int dollars, halfDollars; |
|Change the prompt. |
|System.out.println("Enter a whole number greater than 0."); |
|Compute dollars and halfDollars before the quarters computation. |
|dollars = amount / 100; |
|amount = amount % 100; |
| |
|halfDollars = amount / 50; |
|amount = amount % 50; |
|Add to the output. |
|System.out.println(dollars + " dollars"); |
|System.out.println(halfDollars + " halfDollars"); |

6. If the int variable x contains 10, what will the following Java
statements display?
System.out.println("Test 1" + x * 3 * 2.0);
System.out.println("Test 2" + x * 3 + 2.0);
Given these results, explain why the following Java statement will not
compile:
System.out.println("Test 3" + x * 3 - 2.0)

|Solution: |
| |
|The first statement prints: |
|Test 160.0 |
|The second statement prints: |
|Test 2302.0 |
|In the first print statement, we compute 60.0, convert that into a|
|string and concatenate it with "Test 1". |
|In the second statement, we compute 30 and convert that into a |
|string and concatenate to "Test 2". Then we convert 2.0 to a |
|string and concatenate. Notice that the plus operator is |
|concatenation here not addition and the result is a string. |
| |
|The third print statement does not compile, because Java can not |
|apply the minus operator to a string and 2.0. |
| |
|This code is in Fragments.java. |

7. Write some Java statements that use the String methods indexOf and
substring to find the first word in a string. We define word to be a string
of characters that does not include whitespace. For example, the first word
of the string " Hello, my good friend!" is the string "Hello," and the
second word is the string "my".

|Solution: |
| |
|sentence = sentence.trim(); |
|int space = sentence.indexOf(" "); |
|String word = sentence.substring(0, space); |
| |
|System.out.println("The first word is: " + word); |
| |
|This code is in Fragments.java. |

8. Repeat the previous exercise, but find the second word in the string.

|Solution: |
| |
|sentence = sentence.trim(); |
|int space = sentence.indexOf(" "); |
|String rest = sentence.substring(space).trim(); |
|space = rest.indexOf(" "); |
|String secondWord = rest.substring(0, space); |
| |
|System.out.println("The second word is: " |
|+ secondWord); |
| |
|This code is in Fragments.java. |

9. What does the following Java statement display?
System.out.println("\"\tTest\\\\\rIt\'");
Does replacing the r with an n make a difference in what is displayed?

|Solution: |
| |
|The results depend on the environment being used. NetBeans |
|displayed: |
|" Test\\ |
|It' |
|BlueJ displayed: |
|" Test\\It' |
| |
|The change made a difference in BlueJ, but not in NetBeans. |
| |
|This code is in Fragments.java. |


10. Write a single Java statement that will display the words one, two, and
three, each on its own line.

|Solution: |
|