Homework 1, CS110

advertisement
Homework 1, CS110
Due February 3, 2006 (5 points per problem)
1. Given:
int n=1, m=2, l=3;
double x = 5.0, y = 4.0;
Which of the following assignments are valid. If an assignment is not valid state
the reason. If the assignment is valid show the result of the calculation.
a. n = m = 5; VALID, n = 5 & m = 5
b. x = 2 * n + 5.3; VALID: (2 * 5) + 5.3 = 10 + 5.3 = 15.3
c. n = x % 5; INVALID loss of precision.. no automatic conversion from a
double to an int.
d. x / y = x * y; INVALID, you need a variable on the left hand side of an
expression.
e. x = x + 5; VALID X = 20.3
f. n = 3 + 4.6; INVALID loss of precision.. no automatic conversion from a
double to an int. 4.6 would cause the value of the expression to be of type
double.
2. Suppose x, y, z, and w are int variables. What value is assigned to each variable after the
last statement executes?
x = 5; z = 3; x = 5 & z = 3
y = x – z; x = 5, z = 3, & y = 2
z = 2 * y + 3; x = 5, y = 2, & z = 7
w = x – 2 * y + z; x = 5, y = 2, z = 7, & w = 8
z = w – x; x = 5, y = 2, w = 8 & z = 3
w++; x = 5, y = 2, z = 3 & w = 9
3. The following program has syntax mistakes. Indicate and Correct them. On each
successive line, assume that any preceding error has been corrected.
public class ProgWithErrorsA
{
public static void main (String args[]) { missing keyword static
final int PRIME = 11,213; -- comma is invalid…should be 11213
final int RATE = 15.6; RATE must be double.. to be assigned
15.6
int i, x, y, w;
x = 7;
y = 3;
x = x + w; w is not initialized…
PRIME = x + PRIME; A final variable can not have a value
assigned to it.
system.out.println(PRIME); System instead of system
wages = rate * 36.75; Wages is undefined & rate should be
capitalized
system.out.println(“wages = “ wages); missing +
}
missing }
4. Suppose a, b, and c are int variables and a = 5 and b = 6. What value is assigned to each
variable after each statement executes? If a variable is undefined at a particular statement
report it as UND (undefined).
a= 5, b = 6, c = UND
a = (b++) + 3; a = 9, b = 7, c = UND
c = 2 * a + (++b); a = 9, b = 8, c = 26
b = 2 * (++c) – (a++); a = 10, b = 45, c = 27
5. Write a program that prompts the user to input five decimal numbers. The program
should then add the five numbers, and convert the sum to an integer and print the result.
public class prob5 {
public static void main (String args[]) {
double test1, test2, test3, test4, test5;
long sum;
System.out.println("Please enter five test scores one per
line");
test1 = ReadItem.getNumber();
test2 = ReadItem.getNumber();
test3 = ReadItem.getNumber();
test4 = ReadItem.getNumber();
test5 = ReadItem.getNumber();
sum = (long) (test1 + test2 + test3 + test4 + test5);
System.out.println("The test scores entered are: " + test1 +
", " + test2 + ", " + test3 + ", " + test4 + ", " + test5);
System.out.println("The sum of the tests are: " + sum);
}
}
6. How does a variable of a primitive type differ from a reference variable?
A variable of a primitive type, contains the actual data.. for example an int variable would store
an actual integer value, a reference variable however contains the location of an object, not the
data stored in the object..
For example:
X
int x = 5;
5
String name = new String (“Camille”);
X
Camille
Download