• No se han encontrado resultados

2. OBJETIVOS

4.2.10 Técnicas e Instrumentos para la evaluación Final o Sumativa

numbers are chosen randomly from two containers of numbered balls. Five white balls are chosen from a container of 49 balls, and one red ball is chosen from a container of 42 balls.

Write a program that simulates the selection of the Powerball lottery numbers. Use the Math.random() function to simulate the random selec- tion of a numbered ball. Keep in mind that you cannot just randomly generate six numbers. The five white numbers must be unique and between 1 and 49. (After a ball is removed from the container, it can’t be selected again.) The one red number must be between 1 and 42. Note that the red number can possibly be the same number as one of the selected white balls.

Summary

■■ The && and || operators are the short-circuit and and or operators,

respectively.

■■ The if and if/else statements are used for decision making.

■■ A switch statement consists of one or more case statements. The value

in a switch statement can be of type byte, short, int, or char. The value of a case must be either a literal or a constant expression.

■■ A while loop is used for repeating tasks. The statements in the body of

a while loop execute until the Boolean expression of the while loop is false. It is possible to write an infinite while loop and also a while loop that never executes.

■■ A do/while loop is similar to a while loop, except that a do/while loop

is guaranteed to execute at least once.

■■ A for loop is useful when you know how many times a task is to be

repeated.

■■ The break keyword terminates the execution of a loop. The continue

keyword causes the loop to jump to the next iteration of the loop. 80 Chapter 3

Review Questions

1. Name the two control structures in Java used for making decisions. 2. Name the three control structures in Java used for repetition. 3. True or False: 2 + 2 = 4 “and” 5 – 3 = 1.

4. True or False: 2 + 2 = 4 “or” 5 – 3 = 1.

5. Which of the following data types can be used in a switch statement? Select all that apply. a. byte b. int c. float d. String e. char f. boolean

6. True or False: The value of each case in a switch statement must be either a final vari- able or a literal.

7. What is the output of the following code?

int k = 20; while(k > 0)

System.out.println(k);

8. What is the output of the following code?

int point = 15; switch(point) { case 0 : System.out.println(“point is 0”); break; case 15 : System.out.println(“point is 15”); case 30 : System.out.println(“point is 15 or 30”); break; case 40 : System.out.println(“point is 40”); default : System.out.println(“Invalid point”); } Control Structures 81

82 Chapter 3

9. In the switch statement in the previous question, what would the output be if point was the value 40?

10. What is the output of the following code?

double rate = 1.5; double price = 0.0;

if(rate >= 0.0 && rate < 1.0) {

price = 20 * rate; }

else if(rate >= 1.0 && rate < 2.0) { price = 15 * rate; } else if(rate >= 2.0) { price = 10 * rate; } System.out.println(price);

11. What would the output be in the code in the previous question if the value of rate was –0.75?

12. How many times does the following loop repeat? What is the output?

byte b = 1; do { b++; }while(!(b < 10)); System.out.println(b);

13. What is the output of the following code?

int y = 100, x = 5; while(y > 0) { y--; if(y%x != 0) { continue; } System.out.println(y); }

Answers to Review Questions

1. if/else and switch.

2. while, do/while, and for loops.

3. False, because one of the two expressions is false. 4. True, because one of the two expressions is true.

5. Only 32-bit integer types can be used in a switch statement, so a, b, and e are the correct answers.

6. True.

7. That is an infinite loop because the value of k does not change. The output will be the number 20 printed over and over until the program is terminated.

8. Because the case of 15 does not contain a break, the output is “point is 15” followed by “point is 15 or 30.”

9. Again, because the case of 40 does not contain a break, the output is “point is 40” and “Invalid point.”

10. Because rate is 1.5, the price is 15 * 15, which is 22.5. The output is 22.5.

11. If rate is –0.75, none of the if expressions will evaluate to true, and price will remain 0. The output is 0.

12. The loop will execute only one time. The value of b will be 2.

13. The loop outputs the multiples of 5, starting at 95 and ending at 0. The output is:

95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 Control Structures 83

85

In the last two chapters, I discussed the fundamentals of the Java programming language: keywords, primitive data types, references, strings, arithmetic oper- ators, and control structures. We are now ready to discuss the most important aspect of learning and understanding Java: object-oriented programming (OOP). In this chapter, I will discuss classes and objects, how object-oriented programs differ from procedural programs, how to write a class in Java, and how to instantiate and use objects.

Documento similar