Java Assignment

profilejpFlute
java_homework.docx

Part A

Perform the following problems in Exercises, Problems and Projects

Complete 1-3, 39 and 44

1. Given the following code declaring and initializing two int variables a and b with respective values 3 and 5, indicate whether the value of each expression is true or false.

img

   Expression

true

false

img a < b

____

____

img a != b

____

____

img a == 4

____

____

img ( b − a )<= 1

____

____

img Math.abs( a − b ) >= 2

____

____

img (b % 2 == 1 )

____

____

img b <= 5

____

____

  2.   Given the following code declaring and initializing three boolean variables a, b, and c, with respective values true, true, and false, indicate whether the value of each expression is true or false.

img

   Expression

true

false

img !a

____

____

img a && b

____

____

img a && c

____

____

img a || c

____

____

img ! ( a || b )

____

____

img !a||b

____

____

img ! ( ! ( a && c ) )

____

____

img a && ! ( b || c )

____

____

  3.   Given two boolean variables a and b, are the following expressions equivalent?

img ! ( !a )

img a

39.   You coded the following in class Test.java:

img

At compile time, you get the following error:

img

Explain what the problem is and how to fix it.

44. Write a program that calculates the area of the following figures:

a square of side 0.666666667

a rectangle of sides and 4

Test the two calculated areas for equality; discuss your result.

Part B

Perform the following problems in Exercises, Problems and Projects

Complete 1-3, 5, and 8.

  1    How do you discover that you have an infinite loop in your code?

imgThe code does not compile.

imgThe code compiles and runs but gives the wrong result.

imgThe code runs forever.

imgThe code compiles, but there is a run-time error.

  2    If you want to execute a loop body at least once, what type of loop would you use?

img for loop

img while loop

img do/while loop

imgnone of the above

  3    What best describes a for loop?

imgIt is a count-controlled loop.

imgIt is an event-controlled loop.

imgIt is a sentinel-controlled loop.

5.   What is the output of this code sequence? (The user successively enters 3, 5, and −1.)

img

  8.   What are the values of i and sum after this code sequence is executed?

img

Part A

P

erform the following problems in E

xercises, Problems and Projects

Complete 1

-

3, 39 and 4

4

1.

Given the following code declaring and initializing two

int

variables

a

and

b

with respective values

3 and 5, indicate whether the value of each expression is

true

or

false

.

Expression

true

false

a < b

____

____

a

!= b

____

____

a == 4

____

____

(

b

-

a

)<= 1

____

____

Math.abs(

a

-

b

) >= 2

____

____

(b % 2

== 1

)

____

____

b <= 5

____

____

2.

Given the following code declaring and initializing three

boolean

variables

a, b

, and

c

, with

respective values

true, true

, and

false

, indicate whether the value of each expression is

true

or

false

.

Expression

true

false

Part A

Perform the following problems in Exercises, Problems and Projects

Complete 1-3, 39 and 44

1. Given the following code declaring and initializing two int variables a and b with respective values

3 and 5, indicate whether the value of each expression is true or false.

Expression true false

a < b ____ ____

a != b ____ ____

a == 4 ____ ____

( b - a )<= 1 ____ ____

Math.abs( a - b ) >= 2 ____ ____

(b % 2 == 1 ) ____ ____

b <= 5 ____ ____

2. Given the following code declaring and initializing three boolean variables a, b, and c, with

respective values true, true, and false, indicate whether the value of each expression is true or false.

Expression true false