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';

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.34. Legal.. The value in "c" will be the character whose Unicode value is 1000. The value in "i" will be the character code for 'A'. float f = 1000. 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"). You could write it as "25.0". double d = 1000. This may be a typo in the text (i. it's a typo: you can't cast an int to be a boolean ("(boolean)i"). using "b" to initialize itself). i = (int)b.34f. Either way. The result is 6. Legal. char c = (char)i. The value in "i" will be 1000. int i = (int)d. Legal. "(double)25 / 4". The value in "i" will be 1000. 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.0". boolean b = (boolean)b. You can't convert from type boolean to type int. boolean b = true.e. and you can't use an object to initialize itself. "25d / 4". int i = (int)f. . int i = 1000. b. Legal. Illegal. "25.0 / 4". "25 / 4f". int i = 1000. "25 / 4. or a host of other ways.0 / 4. "25f / 4".i = (int)c.

They are usually called "syntax errors". regardless of the value of x Assuming that this is a typo. ( (x > 1) && (x < 100) ) || (x < 0) Question 17: How do you denote a comment line? How do you denote a comment paragraph? a.java * * Version: * $Id$ * * Revisions: * $Log$ */ ..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. and involve a violation of the grammar of the language. Comment paragraphs are denoted with /* and */.). not handling bad input. and often involve a failure in the logic of the code (e. since the two tests are the same. Question 18: Describe compilation errors and runtime errors.g. Runtime errors occur during the execution of the program. Compilation errors are detected by compilers. and the "x = 1" is supposed to be "x == 1". #1 /* * F2C. (If it's not a typo. running out of memory. but not both false if x == 0. etc. Programming: page 51.) Question 15: Write a Boolean expression that evaluates to true if the number is between 1 and 100 or the number is negative. regardless of the value of x true: one or the other must be true. this must be true. b. then the code won't compile. Comment lines are usually denoted with //. true otherwise true: one or the other must be true.

print( "Please enter the temperature.out. in degrees Fahrenheit: " ). } // main() } // F2C ." ).out. double celsius = (5. System. * * @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.32). fahrenheit = MyInput.0) * (fahrenheit .readDouble().0/9.println( "That's equal to " + celsius + " degrees Celsius. System./** * This application computes the Celcius equivalent of * temperatures input in Fahrenheit.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.