Programming and Problem Solving with Java

(やまだぃちぅ) #1
707

Chapter 1 Programming Warm-Up Exercises


1.Each student will come up with a different answer for this problem, depending on
where he or she finds the recipe and how the cookbook is written. A correct
answer would identify most or all of the nouns in the problem as objects—for ex-
ample, individual ingredients, bowls, tube pan, and oven. Loops in recipes are of-
ten things like “Beat the mixture 300 strokes” or “sift the mixture three times.”
Many angel food cake recipes specify different actions to take when cooking at a
high altitude, which provides an example of a branch. Choosing to add an
optional ingredient is another case of a branch. Some recipes include a
complicated series of steps that are set off from the rest of the process or that are
simply divided into stages such as preparing different mixtures. These represent
candidates for subprograms. Each step or group of steps in the recipe should thus
be identified as a sequence, branch, loop, or subprogram.
2.Each student will come up with a different answer for this problem, depending on
what type of appliance he or she chooses. A correct answer would identify most
or all of the nouns in the directions as objects. For example, setting the time and
date on a computer would probably involve working with a window that has
boxes for data values and buttons. Each text box and button would be an object.
There might be check boxes where you could set an option to be on or off. Such
checks would correspond to branching statements. There might be instructions to
click an arrow repeatedly to set a data value, which would correspond to a looping
statement. A button that brings up a separate window might represent a subpro-
gram, depending on what action clicking the button might generate. Each instruc-
tion should correspond to a sequence, branch, loop, or subprogram.

Chapter 2 Exam Preparation Exercises


2.Dwits
a.XYZ Invalid—must end with 1, 2, or 3
b. 123 Invalid—must start with X, Y, or Z
c. Xl Valid
d.23Y Invalid—must start with X, Y, or Z
e.XY12 Valid
f. Y2Y Invalid—must end with 1, 2, or 3
g.ZY2 Valid
h.XY23Xl Valid
5.False; reserved words cannot be used as variable names.
9.Only one character can be stored into a variable of type char.
12.True; a literal string can be assigned to a variable of type String.
15.This program may be corrected in several ways. Here is one correct version:
public classLotsOfErrors
Free download pdf