Exercises


  1. Create a test demonstrating the immutability of a BigDecimal. Add a second BigDecimal to the first and show that the first still has the original value.

  2. Create one BigDecimal using the value "10.00" and another using the value "1". Show they are not equal. Use multiplication and scale to create a new BigDecimal from the second so that it equals the first. Now reverse the transformation, starting from 10.00, and get back to 1.

  3. Show that 0.9 and 0.005 * 2.0 are not equal with floats. To what precision are they equal? With doubles?

  4. Why won't the following compile? What are two ways it can be fixed?

     public class CompilerError {    float x = 0.01;         } 

  5. Write a brief spike program to discover the decimal value of 0xDEAD. How could you recode the literal in octal?

  6. Find as many interesting variations of NaN and Infinity calculations as you can.

  7. Challenge: Can you find ways in which the wrappers such as Float act differently than the corresponding primitive?

  8. Create a method (test first, of course) that takes a variable number of int values. Code it to return only numbers in the list that are divisible by 3. Code it twice: The first time, use only the modulus (%) operator. The second time, use the division (/) operator and the multiplication operator if necessary, but not the % operator. Test against the sequence of integers from 1 through 10.

  9. Change the chess code to use casting where appropriate. Eliminate the need for the toChar method on the Board class.

  10. Which of the following lines will compile correctly? Why?

     float x = 1; float x = 1.0; float x = (int)1.0; 

  11. What is the value of (int)1.9?

  12. What is the value of Math.rint(1.9)?

  13. When using Math.rint, how is rounding performed? Is 1.5 equal to 1 or 0? How about 2.5?

  14. What are the final values of the following expressions, assuming x is 5 and y is 10 and x and y are both ints? Flag the lines that wouldn't compile, as well. What are the values of x and y afterward? (Consider each expression to be discretei.e., each expression starts with the values of x and y at 5 and 10.)

     x * 5 + y++ * 7 / 4 ++x * 5 * y++ x++ * 5 * ++y ++x + 5 * 7 + y++ ++y++ % ++x++ x * 7 == 35 || y++ == 0    // super tricky ++x * ++y x++ * y++ true && x * 7 x * 2 == y || ++y == 10    // super tricky x * 2 == y || ++y == 10    // super tricky 

  15. Using only the << operator, convert 17 into 34.

  16. What is the decimal value of ~1?

  17. Demonstrate the difference between >> and >>>. Is there a difference between the two for positive numbers? For negative numbers?

  18. Create a function which uses Math.random to generate a random integer from 1 through 50. How will you go about testing your function? Can you test it perfectly? How confident can you get that your code works?

  19. Create a list of numbers from 1 to 100. Use a Random generator to randomly swap elements of the list around 100 times. Write a test that does a reasonable verification: Ensure that the list size remains the same and that two numbers were swapped with each call to the swapper.

  20. Show that the next double is not the same between a Random seeded with 1 and a Random with no seed. Can you make a test that absolutely proves that this is true?

  21. Challenge: Swap two numbers without a temporary variable by using the xor operator.

  22. Challenge: Programmatically demonstrate the number of bits required to store each integral type (char, byte, short, int, long). Hint: Use shift operators. Remember that signed types require an extra bit to store the sign.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net