Prelab Activities Name: Programming Output

June 30, 2017 | Autor: Unknown Unknown | Categoria: Computer Science, Software Engineering, Programming Languages
Share Embed


Descrição do Produto

4 Let’s all move one place on.

Control Statements: Part 1

—Lewis Carroll

The wheel is come full circle. —William Shakespeare

How many apples fell on Newton’s head before he took the hint! —Robert Frost

OBJECTIVES In this chapter you will learn: ■

All the evolution we know of proceeds from the vague to the definite. —Charles Sanders Peirce











To use basic problem-solving techniques. To develop algorithms through the process of top-down, stepwise refinement. To use the if and if…else selection statements to choose among alternative actions. To use the while repetition statement to execute statements in a program repeatedly. To use counter-controlled repetition and sentinel-controlled repetition. To use the assignment, increment and decrement operators.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Control Statements: Part 1

Assignment Checklist Name:

Date:

Section:

Exercises

Assigned: Circle assignments

Prelab Activities Matching

YES

NO

Fill in the Blank

YES

NO

Short Answer

YES

NO

Programming Output

YES

NO

Correct the Code

YES

NO

YES

NO

Lab Exercises Exercise 1 — Credit Follow-Up Questions and Activities Exercise 2 — Palindromes Follow-Up Question and Activity Exercise 3 — Largest Number Follow-Up Question and Activity Debugging

1, 2 YES

NO

1 YES

NO

1 YES

NO

Postlab Activities Coding Exercises

1, 2, 3, 4, 5, 6

Programming Challenges

1, 2

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Date Due

99

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Control Statements: Part 1

101

Prelab Activities Matching Name:

Date:

Section: After reading Chapter 4 of Java How to Program: Seventh Edition, answer the given questions. These questions are intended to test and reinforce your understanding of key Java concepts. You may answer these questions either before or during the lab. For each term in the left column, write the letter for the description that best matches the term from the right column. Term

Description

M

1.

--

a) Java’s only ternary operator.

I

2.

++

A

3.

?:

b) A selection statement that executes an indicated action only when the condition is true.

K

4.

algorithm

H

5.

selection statement

O

6. sentinel-controlled repetition

P

7.

C

8. pseudocode

E

9. repetition statement

Q

10. counter-controlled repetition

G

11. program control

D

12. top-down, stepwise refinement

B

13.

if

F

14.

if…else

J

15. block

L

16.

N

17. primitive types

;

+=, -=, *=, /=, %=

c) An artificial and informal language that helps programmers develop algorithms. d) A process for refining pseudocode by maintaining a complete program representation for each refinement. e) Allows the programmer to specify that an action is to be repeated while some condition remains true. f)

A selection statement that specifies separate actions to execute when a condition is true and when a condition is false.

g) Specifying the order in which statements are to be executed in a computer program. h) Chooses among alternative courses of action in a program. i)

Increment operator.

j)

A set of statements contained within a pair of braces.

k) A procedure for solving a problem in terms of the actions to execute and the order in which the actions should execute. l)

Arithmetic assignment operators.

m) Decrement operator. n) Building blocks for more complicated types in Java. o) Used when the number of repetitions is not known before the loop begins executing. p) Empty statement. q) Used when the number of repetitions is known before the loop begins executing.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Control Statements: Part 1

Prelab Activities

103

Name:

Fill in the Blank Fill in the Blank

Name:

Date:

Section: Fill in the blanks for each of the following statements: 18. Specifying the order in which statements execute in a computer program is called program control . 19. The if selection statement executes an indicated action only when the condition is true. 20. Top-down, stepwise refinement is a process for refining pseudocode by maintaining a complete representation of the program during each refinement. 21. Java requires all variables to have a type before they can be used in a program. For this reason, Java is referred to as a(n) strongly typed language. 22. Java uses internationally recognized standards for both characters and floating-point numbers. 23. A(n) declaration specifies the type and name of a variable. 24. The if…else selection statement specifies separate actions to execute when the condition is true and when the condition is false. 25. The ++ operator and the -- operator increment and decrement a variable by 1, respectively. 26. Unary cast operator ( double ) creates a temporary double-precision, floating-point copy of its operand. 27. A value that contains a fractional part is referred to as a floating-point number and is represented by the types float or double .

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Control Statements: Part 1

Prelab Activities

105

Name:

Short Answer Short Answer

Name:

Date:

Section: Answer the following questions in the space provided. Your answers should be concise; aim for two or three sentences. 28. Explain the purpose of a selection statement. Selection statements choose among alternative actions in a program. They check for certain conditions in a program, and perform different tasks if those conditions are met. 29. Use pseudocode or a UML activity diagram to give an example of sequence control statements. Add this grade into the running total Add one to the grade counter

30. Describe the term “algorithm” and why pseudocode can help programmers develop algorithms. An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which the actions should execute. Pseudocode helps in the development of algorithms because it forces you to “think out” a program during the design process, then you can translate the pseudocode into Java code. 31. Use pseudocode or a UML activity diagram to give an example of an if…else selection statement. If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed”

32. Explain the difference between the if selection statement and the if…else selection statement. The if selection statement performs an indicated action only when the given condition evaluates to true, otherwise the action is skipped. The if…else selection statement allows an action to be performed when the condition is true, and a separate action if the condition is false. 33. Use pseudocode to give an example of a looping construct in which the number of repetitions is known in advance. While student counter is less than or equal to 10 Prompt the user to enter the next exam result

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

106

Control Statements: Part 1

Chapter4

Prelab Activities

Name:

Short Answer Input the next exam result Add the grade to the running total Add one to student counter

34. Use pseudocode to give an example of a looping construct in which the number of repetitions is not known in advance. Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel)

35. Explain how repetition statements are used. Repetition statements specify that an action (or set of actions) should be performed repeatedly while a condition remains true. 36. Explain the difference between counter-controlled and sentinel-controlled repetition. Counter-controlled repetition is used when the number of repetitions is known in advance. Sentinel-controlled repetition is used when the number of repetitions is not known in advance. In this case, a sentinel value specifies when the repetition should terminate.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Control Statements: Part 1

Prelab Activities

107

Name:

Programming Output Programming Output

Name:

Date:

Section: For each of the given program segments, read the code and write the output in the space provided below each program. [Note: Do not execute these programs on a computer.] For questions 37–39 assume the following class definition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

public class Test2 { public static void main( String args[] ) { Scanner input = new Scanner(); int number1; System.out.println( "Enter an integer:" ); number1 = input.nextInt(); if ( number1 % 2 == 0 ) System.out.println( "%d is even\n", number1 ); else System.out.println( "%d is odd\n", number1 ); } // end main } // end class Test2

37. What will be output by lines 11–14 if the user enters the integer 2 at line 9? Your answer: 2 is even

38. What will be output by lines 11–14 if the user enters the integer 3 at line 9? Your answer: 3 is odd

39. What will be the output if the following code is placed at line 10 of the preceding class definition? Assume that the user enters 5. 1

number1 = number1 + 3;

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

108

Control Statements: Part 1

Chapter4

Prelab Activities

Name:

Programming Output Your answer: 8 is even

40. What is output by the following program? 1 2 3 4 5 6 7 8 9 10 11

public class Grade { public static void main( String args[] ) { int grade1 = 65; int grade2 = 50; System.out.println( grade1 >= 60 ? "Passed." : "Failed." ); System.out.println( grade2 >= 60 ? "Passed." : "Failed." ); } // end main } // end class Grade

Your answer: Passed Failed

For questions 41–43, assume the following class declaration: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

public class Value { public static void main( String args[] ) { int x; int xLimit; /* assign values to x and xLimit here */ while ( x = 60 ) System.out.println( "Passed." ); else { System.out.println( "Failed." ); System.out.println( "You must take this course again." ); }



Missing braces before line 4 and after line 5. Logic error.

48. The following while loop should compute the product of all the integers between 1 and 5, inclusive. 1 2 3 4 5

int i = 1; int product = 1; while ( i
Lihat lebih banyak...

Comentários

Copyright © 2017 DADOSPDF Inc.