Upload
praveen-kumar-singh
View
214
Download
0
Embed Size (px)
Citation preview
7/31/2019 java hw1
1/4
Answers to Homework 2
Question 2: Declare the following:
an int variable with an initial value of 0int i = 0;
a long variable with an initial value of 10000long l = 10000;
a float variable with an initial value of 3.4float f = 3.4f; // or float f = (float)3.4;
a double variable with an initial value of 34.45double d = 34.45;
a char variable with an initial value of 4char c = (char)4; // I'll accept char c = '4'; or char c = '\0004';
a boolean variable with an initial value of trueboolean b = 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';
7/31/2019 java hw1
2/4
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: youcan'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 surethat 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").
7/31/2019 java hw1
3/4
Question 13: Show the result of the following Boolean expressions if the result can be
determined:
Expression Result
(true) && (3 > 4) false: the second part can not be true
!(x > 0) && (x > 0) false: either part can be true, but not both
(x > 0) || (x < 0) false if x == 0, true otherwise
(x != 0) || (x ==
0) true: one or the other must be true, regardless of the value of x
(x >= 0) || (x < 0) true: one or the other must be true, regardless of the value of x
(x != 1) == !(x =
1)
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 thelogic of the code (e.g., not handling bad input, running out of memory, etc.).
Programming: page 51, #1
/*
* F2C.java*
* Version:
* $Id$
*
* Revisions:
* $Log$
*/
7/31/2019 java hw1
4/4
/**
* 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