Exercises


Note

The solutions to these exercises are in Appendix B.

  1. Rewrite the following code to maximize readability:

    switch (x) {   case 100:     System.out.println("x is big");     break;   case 101:     System.out.println("x is big");     break;   case 10:     System.out.println("x is medium");     break;   case -1000:     System.out.println("x is negative");     break; }
  2. Rewrite the following code to make it cleaner:

    boolean flag = false; switch (a) {   case 1:     x = 1000;     flag = true;     break;   case 30:     y = 1000;     flag = true;     break; } if (!flag)   z = 1000;
  3. What happens when the following code is executed with val equal to 10? 100? 1,000? First, decide just by looking at the source code. Then write a program to verify your answer.

    switch (val) {   case 10:     System.out.println("ten");   case 100:     System.out.println("hundred");   default:     System.out.println("thousand"); } 
  4. Run the WhileLab animated illustration by typing java loops.WhileLab. Try changing the value in the condition in the third line. What do you notice about the final value of a?

  5. The description of WhileLab suggests three exercises, which are repeated here. For each desired result, configure the inputs of WhileLab to produce that result. Then verify your work (and make sure WhileLab is trustworthy) by writing an application that duplicates each while loop. The loops should generate the following results:

    • The sum of the numbers 1 through 500, inclusive.

    • The sum of the even numbers from 50 through 60, inclusive.

    • The product of the first 5 odd numbers.

  6. There is a number game called Hotpo that can entertain you for a few minutes while you're stuck in traffic, waiting for a movie to start, or having dinner with someone really boring. Hotpo stands for Half Or Triple Plus One, and it works like this: Think of an odd number. Now mentally calculate another number, as follows: If the first number was even, the next number is half the first one; if the first number was odd, the next number is 3 times the first number, plus 1. Now you can forget the first number and apply the Half Or Triple Plus One formula to your current number. Keep going until the value reaches 1. Let's try this with a starting number of 5. The series is 5 16 8 4 2 1.

    Write a program that plays Hotpo. First, initialize a variable called n to the starting value you're interested in. Then enter a loop that prints out each number in the sequence, along with the current step number. For example, the output for 3 would be

    Step #1: 10 Step #2: 5 Step #3: 16 Step #4: 8 Step #5: 4 Step #6: 2 Step #7: 1

    Should the program use a while loop or a for loop?

  7. What is the value of n after the following code is executed?

    int n = 1; outer: for (int i=2; i<10; i++) {     for (int j=1; j<i; j++)     {         n *= j;         if (i*j == 10)             break outer;     } }




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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