Vous êtes sur la page 1sur 4

APCS Chapter 4

4.1
1. Translate the following statements to equivalent statements that use extended
assignment operators:
a. x = x * 2;
This is also x *= 2;
b. y = y % 2;
This is also y %=2;
2. Translate the following statements to equivalent statements that do not use t
he extended assignment operators:
a. x += 5;
This is also x =x+5;
b. x *= x;
This is also x = x * x;
4.2
1. Assume that x has the value 3.6 and y has the value 4. State the value of the
variable z after the following statements:
a. z = Math.sqrt(y);
z is 2.0.
b. z = Math.round(x);
z is 4.
c. z = Math.pow(y, 3);
z is 46.656.
d. z = Math.round(Math.sqrt(x));
z is 2.
2. Write code segments to print the following values in a terminal window:
a. A random integer between 1 and 20, inclusive
generator.nextInt(20) + 1;
b. A random double between 1 and 10, inclusive
generator.nextDouble() + 1 + generator.nextInt(9);
4.3
1. Why does Jill use a while statement in her instructions to Jack?
Jill wants to use a statement because Jill wants Jack to keep doing the job if t
here are any cows in the paddock.
2. Why does Jill use an if-else statement in her instructions to Jack? Write pse
udocode control statements that are similar in style to the farm example for the
following situations:
a. If a checker piece is red, then put it on a red square; otherwise, put it on
a black square.
if (checker piece is red) {
put it on a red square;
}else{
put it on a black square;
}
b. If your shoes are muddy, then take them off and leave them outside the door.
if (shoes are muddy) {
take them off and leave them outside;;
}
c. Pick up all the marbles on the floor and put them into a bag.
if(marbles on the floor){
pick them up and put them into a bag;
}
3. Describe in English what the following code segments do:
a. If x is greater than y, then the value for x will be assigned to x and the va
lue for y is assigned to x, which means temp value is also assigned to y.
b. When the count s value is less than assigned value of total, there is a value o
n x, and the sum of the assigned value plus the absolute value is what is assign
ed. If the total is greater than 0 the program will print the value of sums divi
ded by the total.
4.4
1. What type of expression must the condition of an if statement contain?
The expression must contain boolean.
2. Describe the role of the curly braces ({}) in an if statement.
The braces allow multiple statements for if statements.
3. What is the difference between an if statement and an if-else statement?
If-else statements must have an if statement with an else statement right after
it.
4. Assume that x is 5 and y is 10. Write the values of the following expressions
:
a. x <= 10
This is true.
b. x - 2 != 0
This is true.
c. x > y
This is false.
5. Given the following minispecifications, write expressions involving relationa
l operators:
a. Determine if an input value x is greater than 0.
It can be (x > 0).
b. Determine if a given number of seconds equals a minute.
It can be (seconds / 60 = 1)
c. If a, b, and c are the lengths of the sides of a triangle and c is the larges
t side, determine if the triangle is a right triangle. (Hint: Use the Pythagorea
n equation and round the operand before comparing.)
It can be ((a * a) + (b * b) = (c * c))
6. Write the outputs of the following code segments:
a. The output is 5.
b. The out is Not Equal.
7. Given the following minispecifications, write expressions involving if-else s
tatements and output statements:
a. Print the larger of two numbers.
The expression can be
if (numa < numb)
out(numb);
else
out(numa);
b. Prompt the user for two whole numbers and input them. Then print the numbers
in numeric order.
int x, y, temp;
x = reader.readInt( Enter first integer: );
y = reader.readInt( Enter second integer: );
if (y > x) {
temp = x;
x = y;
y = temp;
}
System.out.println (x + , + y);
1. When does a while loop terminate execution?
A loop terminates when the condition is no longer met.
2. List the three components of a while loop.
A while loop has condition, statements, brackets, and value control.
3. What happens if the condition of a while loop is false from the outset?
If the condition is false from the outset it is never executed.
4. Describe in English what the following code segments do:
a. The segment outputs 2 ^ power, bu the expnent was never increased so it staye
d as 1 2.
b. It continued to multiply the entered numbers until -999 is entered.
5. Write code segments to perform the following tasks:
a. Print the squares and cubes of the first 10 positive integers.
int num = 1;
while (num <= 10)
{
System.out.println(Math.pow(num,2));
System.out.println(Math.pow(num,3));
num++;
}
b. Print 10 random integers between 1 and 10 inclusive.
int counter = 1;
while (counter <= 10)
{
System.out.println(gen.nextInt(10) + 1);
counter++;
}
c. Input names and ages of people until a person s age is 100.
int age = 0;
String name;
while (age != 100)
{
age = reader.readInt("Enter age: ");
name = reader.readString("Enger name: ");
}
4.6
1. Describe in English what the following code segments do:
a. The code segment prints out 2 ^ power until limit.
b. It starts at expo until the value is done and then takes the value and square
it.
2. Write code segments that use for loops to perform the following tasks:
a. Print the squares and cubes of the first 10 positive integers
for (int i = 1; i <= 10; i++)
{
System.out.println (Math.pow(i,2)\nMath.pow(i,3));
}
b. Build a string consisting of the first 10 positive digits in descending order
.
int base = 2;
for (int count = expo; count > 1; count-)
base = base * base;
3. Translate the following for loops to equivalent while loops:
a. int base = 2;
for (int count = expo; count > 1; count-)
base = base * base;
b. int base = 2;
for (int count = expo; count > 1; count-)
base = base * base;
4.7
1. Describe in English what the following code segments do:
a. This displays all even integers until limit.
b. This will loop until the right number is guessed.
2. Write code segments that use loops to perform the following tasks:
a. Print the squares and cubes of the first 10 positive, odd integers.
for (int i = 1; i <= 19; i++)
{
System.out.println(Math.pow(i,2)\nMath.pow(i,3))
}
b. Build a string consisting of the first 10 positive, even digits in descending
order.
do
{
guess = reader.readInt("Enter integer");
} while (guess != secret);
1. Describe the logic errors in the following loops:
a.
b. // Print the first ten positive odd numbers int number = 1; while (number != 10) System
); number += 2; }

Vous aimerez peut-être aussi