Exercise on Lesson 5
Answer key
Part two
Unless otherwise instructed in the following problems, state what gets printed.
Write code that will create a constant E that’s equal to 2.718.
final double E = 2.718;
Write the simplest type constant that sets the number of students, NUM_STUDENTS, to 236.
final int NUM_STUDENTS = 236;
What’s wrong, if anything, with the following code in the main method? final double Area;
Area = 203.49;
Nothing wrong
int cnt = 27.2; System.out.println(cnt); What’s printed?
Will not compile
double d = 78.1; int fg = (int)d; System.out.println(fg); What’s printed?
78
Is double f4 = 22; legal?
Yes, it is legal because it is allowed for a integer to be stored in a double, but not a decimal in an int.
The following code stores a 20 in the variable j: double j = 61/3; What small change can you
make to this single line of code to make it produce the “real” answer to the division?
You could cast it as a double: double j = (double)61/3;
System.out.println( (double)(90/9) );
10.0
System.out.println(4 + 6.0/4 + 5 * 3 – 3);
17.5
int p = 3; double d = 10.3; int j = (int)5.9; System.out.println(p + p * d – 3 * j);
18.9
int p = 3; double d = 10.3; int j = (int)5.9; System.out.println(p + p * (int)d – 3 * j);
18
The following code applies to 12 – 15 (The next three questions) :
int dividend = 12, divisor = 4, quotient = 0, remainder = 0;
int dividend2 = 13, divisor2 = 3, quotient2 = 0, remainder2 = 0; quotient = dividend/divisor;
remainder = dividend % divisor;
quotient2 = dividend2 / divisor2;
remainder2 = dividend2 % divisor2;
System.out.println(quotient);
System.out.println(remainder);
System.out.println(quotient2);
System.out.println(remainder2);
Write a line of code in which you divide the double precision number d by an integer variable
called i. Type cast the double so that strictly integer division is done. Store the result in j, an
integer.
int j = (int)d / i;
Suppose we have a line of code that says final String M = “ugg”; Later in the same program,
would it be permissible to say the following? M = “wow”;
No, if it final, you cannot change the value.
Is the following code legal? If so, what is printed? If not, why?
int k = 7; k*=.5; System.out.println(k);
Yes, it prints 3
Exercise on Lesson 6
Write code that will take the square root of x and store the result in y.
y = Math.sqrt(x);
Write code that will multiply the value of the integer j times the absolute value of the integer
m and then store the result in the integer k.
int k = j * Math.abs(m);
Is the following legal? If not, what would you do to make it legal? int k = Math.abs(-127.5);
It is legal, but in order to show the decimal you would need to use a double variable type instead.
1.5
Write a statement that will print the result of 2 .
System.out.println(Math.pow(2, 1.5));
System.out.println( Math.ceil(-157.2) );
-157.0
System.out.println( Math.floor(-157.2) );
-158.0
System.out.println( Math.ceil(157.2) );
158.0
System.out.println( Math.floor(157.2) );
157.0
System.out.println( Math.round(-157.2) );
-157
System.out.println( Math.ceil(-157.7) );
-157.0
System.out.println( Math.ceil(157) );
157.0
Part 2
Answer key
Lesson 6
System.out.println( Math.ceil(157.7) );
-158.0
Write a statement that will print the natural log of 18.... same as ln(18) on a calculator.
System.out.println(Math.log(18));
Write a line of code that multiplies double p times π and stores the result in b.
double b = p * Math.PI;
Exercise on Lesson 8
In problems1 – 5 assume the following: int z = 23, x = -109;
double c = 2345.19, v = 157.03; boolean a = false, s = true;
1. booleangus=(x>0)&&(c==v); System.out.println(!gus);
2. System.out.println(a | | s);
3. System.out.println( ( (-1 * x) > 0) && !a );
4. booleanr=z==x; System.out.println( r | | false );
5. 5. System.out.println( z!=x );
6. 6. Fill in the following charts.
a b false false false true true false true true (!a && b) a
b (a||!b) false false false true true false true true
In order given on the book: (!a &&b) false, true, false, false,
(a||!b) False, true, true, true
7. Assume b, p, and q are booleans. Write code that will assign to b the result of AND- ing p and
q.
b=p&&q;
boolean w=(x>0) || (y==z);
8. Assign to the boolean variable w the result of OR-ing the following two things: A test to see
if x is positive: A test to see if y equals z:
9. What are the two possible values of a boolean variable?
False and true
10. Write a test that will return a true if a is not equal to b. Assume a and b are integers.
Store the result in boolean kDog.
Boolean kDog=a!=b
1. Write the answer to #10 another way. (?? L’ho scritto io, da verificare )
if (a!=b) {
kDog = true
}
else {
kDog = false
}
2. What is the Java operator for boolean AND-ing?
||
3. What is the Java operator for boolean OR-ing?
4. System.out.println( (true && false) | | ( (true && true) | | false ) );
5. System.out.println(true && true || false);
6. System.out.println(true || true && false);
7. System.out.println(false || true && false);
8. System.out.println(false && true || false);
Exercise on Lesson 9
Use the following code for problems 1 – 10 and give the value of true_false for each: int i = 10, j = 3;
boolean true_false;
1. true_false = (j > i);
2. true_false = (i > j);
3. true_false = (i= = j);
4. true_false=((j<=i)||(j>=i) );
5. true_false=( (i>j)&&(j==0) );
6. true_false=( (j<50)||(j!=33) );
7. true_false=( !(j>=0)||(i<=50) );
8. true_false = ( !(! (!true)) );
9. true_false = (5 < = 5);
10. true_false = (j != i);
11. Write a statement that will store a true in boolean b if the value in the variable m is 44 or less.
12. Write a statement that will store a false in boolean b if the value in r is greater than 17.
13. What is returned by the following expression? (Recall that the precedence order of logical
operators is !, &&, and finally | |.)
!( (2>3) | | (5= =5) && (7>1) && (4<15) | | (35<=36) && (89!=34) )
In problem 14 – 16 what is the output?
14. String s1 = “school BUS”;
if ( s1.equals(“school bus”) )
System.out.println(“Equal”); else
System.out.println(“Not equal”);
15. String s1 = “school BUS”;
if ( s1.equalsIgnoreCase(“school bus”) ) System.out.println(“Equal”);
else
System.out.println(“Not equal”);
9-3
16. int j = 19, m = 200; if (j= =18)
m++;
j++; System.out.println(m);
System.out.println(j);
17. Write a statement that will store a false in boolean b if the value in g is not equal to 34.
18. Write a statement that will store a true in boolean b if integer k is even, false if it is odd.
19. Write a program that inputs a String from the keyboard after the prompt, “Enter your
password”. If it’s entered exactly as “XRay”, printout “Password entered successfully.”;
otherwise, have it printout “Incorrect password.”
20. What is output by the following “nested ifs” code? int k = 79;
if (k>50) {
if (k<60) {System.out.println(“One”);} else
{ System.out.println(“Two”);}
} else {
if (k>30) System.out.println(“Three”);
else
System.out.println(“Four”);