Question
1. Describe why it is more difficult to compare floating-point numbers than integers. Write Java code fragments to test whether an integer n equals 10
1. Describe why it is more difficult to compare floating-point numbers than integers.
Write Java code fragments to test
whether an integer n equals 10 and
whether a floating-point number x is approximately equal to 10
2. It is easy to confuse the = and == operators.
Write a test program containing the statement
if (floor = 13)
What error message do you get?
Write another test program containing the statement
count == 0;
What does your compiler do when you compile the program?
3. Give a set of four test cases for the algorithm from question 9 that covers all branches.
4. In a scheduling program, we want to check whether two appointments overlap.
For simplicity, appointments start at a full hour, and we use military time (with hours 024).
The following pseudocode describes an algorithm that determines whether the appointment with start time start1 and end time end1 overlaps with the appointment with start time start2 and end time end2.
If start1 > start2:
s = start1 Else:
s = start2 If end1 < end2:
e = endl Else:
e = end2 If s < e:
The appointments overlap. Else:
The appointments dont overlap.
Using the table described in question 9, trace this algorithm with each of the following:
an appointment from 1012 and one from 1113
an appointment from 1011 and one from 1213
5. Suppose the value of the boolean variable b is false and the value of the int variable x is 0.
What is the value of each of the following expressions?
b && x == 0
b || x == 0
!b && x == 0
!b || x == 0
b && x != 0
b || x != 0
!b && x != 0
!b || x != 0
6. Simplify the following expressions.
Assume b is a variable of type boolean.
b == true
b == false
b != true
b != false
7. Simplify the following statements.
Here, b is a variable of type boolean and n is a variable of type int.
if (n == 0) { b = true; }
else { b = false; }
(Hint: What is the value of n == 0?)
if (n == 0) { b = false; }
else { b = true; }
b = false;
if (n > 1)
{
if (n < 2) { b = true; }
}
if (n < 1) { b = true; }
else { b = n > 2; }
8. Describe the difference between
s = 0; if (x > 0) { s++; // same as s=s+1;} if (y > 0) { s++; // same as s=s+1;}
and
s = 0; if (x > 0) { s++; // same as s=s+1;} else { if (y > 0) { s++; // same as s=s+1;} }
9. Find the errors in the following if statements:
if x > 0 then System.out.print(x);
if (1 + x > Math.pow(x, Math.sqrt(2)) { y = y + x; }
if (x = 1) { y++; }
10. What does each of these code fragments print?
int n = 1;
int m = -1;
if (n < -m) { System.out.print(n); }
else { System.out.print(m); }
int n = 1;
int m = -1;
if (-n >= m) { System.out.print(n); }
else { System.out.print(m); }
double x = 0;
double y = 1;
if (Math.abs(x - y) < 1) { System.out.print(x); }
else { System.out.print(y); }
d. double x = Math.sqrt(2);
double y = 2;
if (x * x == y) { System.out.print(x); }
else { System.out.print(y); } // For part (d), write down an answer based on the logic. // Then, type (or copy-paste) the code into the main method // of a Java program and compile and execute. // What result do you get? Why do you think this happens?
11. Suppose x and y are variables of type double.
Write a Java code fragment that sets y to x if x is positive and to 0 otherwise.
12. Suppose x and y are variables of type double.
Write a code fragment that sets y to the absolute value of x without calling the Math.abs function.
Use an if statement.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started