Question: 1.Describe the difference between s = 0; if (x > 0) { s++; // same as s=s+1; } if (y > 0) { s++; //

1.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;} }

2.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++; }

3.What does each of these code fragments print?

int n = 1; int m = -1; if (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) else { System.out.print(y); }

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?

4.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.

5.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.

6.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

7.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? 8.Each square on a chess board can be described by a letter and number, such as g5 in this example:  1.Describe the difference between s = 0; if (x > 0) The following pseudocode describes an algorithm that determines whether a square with a given letter and number is dark (black) or light (white).

If the letter is an a, c, e, or g: If the number is odd: color = "black" Else: color = "white" Else: If the number is even: color = "black" Else: color = "white"

Trace this algorithm by making a table such that:

The table headers are the variables (one column per variable)

The first row shows the initial (assumed) values of the variables

Each following row shows a value change in a variable

9.Give a set of four test cases for the algorithm from question 8 that covers all branches.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!