Вы находитесь на странице: 1из 4

Answers to Homework 2

Question 2: Declare the following: an int variable with an initial value of 0


int i = 0;

a long variable with an initial value of 10000


long l = 10000;

a float variable with an initial value of 3.4


float f = 3.4f; double d = 34.45; // or float f = (float)3.4;

a double variable with an initial value of 34.45 a char variable with an initial value of 4
char c = (char)4; boolean b = true; // I'll accept char c = '4'; or char c = '\0004';

a boolean variable with an initial value of true Question 3: Assume that a = 1 and d = 1.0 and that each expression is independent. What are the results of the following expressions? a = 46 / 9;
a = 5

a = 46 % 9 + 4 * 4 - 2;
a = 1 + 16 - 2 = 15

a = 45 + 43 % 5 * (23 * 3 % 2);
a = 45 + 3 * (1) = 48

a = 45 + 45 * 50 % a--;
a = 45 + 2250 % a-- = 45 + 0 = 45

a = 45 + 1 + 45 * 50 % (--a);
Undefined: (--a) will yield 0, and you can't divide by 0 to do the rest

d += 34.23 * 3 + d++;
d = d + (34.23 * 3 + d++) = 104.69

d -= 3.4 * (a + 5) * d++;
d = d - (3.4 * (a + 5) * d++) = 1.0 - 20.4 = -19.4

a %= 3 / a + 3;
a = a % (3 / a + 3) = 1 % 6 = 1

Question 5: Can different types of numeric values be used together in a computation? Yes. The types can be mixed through numeric conversions called type casts, which may be explicit (e.g., "(double)1 / 2") or implicit (e.g., "1 / 2.0" will automatically cast "1" to be of type double to match the "2.0"). Question 7: Can the following conversions involving casting be allowed? If so, find the converted result.
char c = 'A';

i = (int)c;

Legal. The value in "i" will be the character code for 'A'.
boolean b = true; i = (int)b;

Illegal. You can't convert from type boolean to type int.


float f = 1000.34f; int i = (int)f;

Legal. The value in "i" will be 1000.


double d = 1000.34; int i = (int)d;

Legal. The value in "i" will be 1000.


int i = 1000; char c = (char)i;

Legal. The value in "c" will be the character whose Unicode value is 1000.
int i = 1000; boolean b = (boolean)b;

This may be a typo in the text (i.e., using "b" to initialize itself). Either way, it's a typo: you can't cast an int to be a boolean ("(boolean)i"), and you can't use an object to initialize itself. Question 8: What is the result of 25 / 4? How would you rewrite the expression if you wanted the quotient to be a floating-point number? a. The result is 6. b. You could write it as "25.0 / 4", "25.0 / 4.0", "25 / 4.0", "25f / 4", "25d / 4", "25 / 4f", "(double)25 / 4", or a host of other ways. The important thing would be to make sure that at least one of the numbers was going to be interpreted as a floating-point value. Question 10: What does an explicit conversion from a double to an int do with the fractional part of the double value? The fractional part is dropped (also known as "truncation").

Question 13: Show the result of the following Boolean expressions if the result can be determined: Expression
(true) && (3 > 4) !(x > 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x == 0) (x >= 0) || (x < 0) (x != 1) == !(x = 1)

Result false: the second part can not be true false: either part can be true, but not both false if x == 0, true otherwise true: one or the other must be true, regardless of the value of x true: one or the other must be true, regardless of the value of x Assuming that this is a typo, and the "x = 1" is supposed to be "x == 1", this must be true, since the two tests are the same. (If it's not a typo, then the code won't compile.)

Question 15: Write a Boolean expression that evaluates to true if the number is between 1 and 100 or the number is negative.
( (x > 1) && (x < 100) ) || (x < 0)

Question 17: How do you denote a comment line? How do you denote a comment paragraph? a. Comment lines are usually denoted with //. b. Comment paragraphs are denoted with /* and */. Question 18: Describe compilation errors and runtime errors. Compilation errors are detected by compilers. They are usually called "syntax errors", and involve a violation of the grammar of the language. Runtime errors occur during the execution of the program, and often involve a failure in the logic of the code (e.g., not handling bad input, running out of memory, etc.). Programming: page 51, #1
/* * F2C.java * * Version: * $Id$ * * Revisions: * $Log$ */

/** * This application computes the Celcius equivalent of * temperatures input in Fahrenheit. * * @author: Rob Duncan */ public class F2C { /** * This is the main method for the class * * @param args array of string arguments * * @return none */ public static void main (String[] args) { double fahrenheit; System.out.print( "Please enter the temperature, in degrees Fahrenheit: " ); fahrenheit = MyInput.readDouble(); double celsius = (5.0/9.0) * (fahrenheit - 32); System.out.println( "That's equal to " + celsius + " degrees Celsius." ); } // main() } // F2C

Вам также может понравиться