Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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: image text in transcribed 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 9 that covers all branches.

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

Using the table described in question 8, trace this algorithm with each of the following:

an appointment from 1012 and one from 1113

an appointment from 1011 and one from 1213

11.Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean predicates (variables) p, q, and r. image text in transcribed

12.True or false? A && B is the same as B && A for any boolean expressions A and B.

13.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?

a.b && x == 0

b.b || x == 0

c.!b && x == 0

d.!b || x == 0

e.b && x != 0

f.b || x != 0

g.!b && x != 0

h.!b || x != 0

14.Simplify the following expressions. Assume b is a variable of type boolean.

a.b == true

b.b == false

c.b != true

d.b != false

15.Simplify the following statements. Here, b is a variable of type boolean and n is a variable of type int.

a.if (n == 0) { b = true; } else { b = false; } (Hint: What is the value of n == 0?)

b.if (n == 0) { b = false; } else { b = true; }

c.b = false; if (n > 1) { if (n }

d.if (n else { b = n > 2; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions

Question

Describe the effect of automation on the process accounting system.

Answered: 1 week ago