Professional Documents
Culture Documents
Apcs CH 4 Ws Answers
Apcs CH 4 Ws Answers
1. Numbers are essential to computing. In Java there are two fundamental numeric types: integers and
floating-point numbers. Integers have no fractional part, whereas floating-point numbers do. Variables of
either type are represented by user-specified names. Space in the computer's memory is associated with the
name so that the variable acts like a container for the numeric data.
Declaring a variable reserves space in memory and associates a specified name with that space. When a
variable is declared, it contains whatever value is left over from the previous use of the memory space.
Initializing a variable sets it to a specific value. If no specific value is known, it's a good idea to initialize a
new variable to zero in order to avoid having it contain a random value.
Actually, Java permits you to use other alphabetical characters, such as Ä and ç in
variable names as well, but these characters may cause difficulty if you want to move your source code to
other systems. It is best to stick to Roman letters without accents.
A variable name cannot begin with a digit, and the name must be unique. Neither you nor the compiler
would be able to distinguish between variables with the same name. For some simple cases, using single
letters such as i, j, k is enough, but as programs grow larger this becomes less informative. Like the
name for a product or service, a variable name should do two things:
- be unique
- express the purpose
A good variable name is descriptive enough to indicate to a human reader how a variable is being used, for
example countMessages,
userPreference, or customerName.
Complete the following table by renaming the bad variable names and providing descriptions.
4. Values on both sides of an assignment (=) operator should be checked to verify that the data received
matches the data type of the variable to which it will be assigned. Fix the right-hand sides of the following
assignments so that they are the correct type for the variable on the left side.
Statement Error
a + 2 = 3; 1)can’t assign value to
an expression
Math.PI = 3; 2)Can’t change a
constant value
x == x + 1; 3)Can’t use == for
assignement
6. Computations with floating-point numbers have finite precision. What values are assigned in the following
assignments? in:
Statement Value
double x = Math.pow(10, 20) + 1; 1) 1 X 10 20
double y = x - 1; 2) 1 X 10 20
double z = x - y;; 2) 0.0
Why? The precision of double only allows for 15 significant decimal digits so the 1 in x is lost.
7. Recalling what you've learned about integers and floating-point values, what value is assigned to x by each
of the following?
int x = 6 / 3; 1) 2
int x = 7 / 3; 2) 2
double x = 7 / 3; 3) 2
int x = 7 % 3; 4) 1
int x = 6 % 3; 5) 0
a. y = x + 1/2 y = x + 1 / 2.0;
b. y = x2 + 2x + 1
y = Math.pow( x, 2)+ 2 * x + 1;
X
c. y = --- y = x / (1-x);
1-x
9. Many programs manipulate not just numbers but also text. Java stores text in string variables. They are
declared and assigned in a manner similar to a numeric variable. The sequence may have any length. A
string may even have a length of zero. Such a string is called an empty string, and it is denoted as "". For
example,
Given:
"JoanWilson"
System.out.println(outputString);
}
}
12. The number 3 and the string "3" are completely different values. But for various purposes, numeric types
sometimes need to be used as strings, and for calculation purposes, strings that contain numbers need to be
used as numbers.
In Java, it is very easy to convert a number to a string. Simply concatenate with the empty string "". For
example, if x has the value 3, then "" + x is the string "3".
class SalesActivity
{
. . .
private int year;
private double percentIncrease;
private String article;
}
Write a method String getDescription to return a sentence of the form "In ..., there was
a net increase of ...% in sales of ...".
year: 1996
increase: 14.7
what: widgets
return sentence;
}
13. Using JOptionPane.showInputDialog, ask the user to supply two integers a and b. Then print out
the values a / b and a % b.
Note that you need to use the Integer.parseInt method to convert the input strings to integer values.