• No results found

Exercises: Conditionals

N/A
N/A
Protected

Academic year: 2021

Share "Exercises: Conditionals"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

Code Reading

Write out the truth tables for the following statements.

1. P || !Q && P Solution: P Q !Q !Q && P P || !Q && P 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 1 0 0 1 2. !(P && Q) || R Solution:

P Q R P && Q !(P && Q) !(P && Q) || R

0 0 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1

(2)

3 } 4 if ( x < 15) { 5 S y s t e m . out . p r i n t l n ( x + " < 15 ") ; 6 } 7 if ( x < 20) { 8 S y s t e m . out . p r i n t l n ( x + " < 20 ") ; 9 } 10 11 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 12 // x = 17 // x = 25 12 < 15 17 < 20 Done ! 12 < 20 Done ! Done !

(3)

4. What is the output from the following code when x = 12? When x = 18? When x = 22? Also draw out the control ow diagram for this code to help your understand how it works.

1 if ( x < 10) { 2 S y s t e m . out . p r i n t l n ( x + " < 10 ") ; 3 } else { 4 S y s t e m . out . p r i n t l n ( x + " >= 10 ") ; 5 } 6 if ( x < 15) { 7 S y s t e m . out . p r i n t l n ( x + " < 15 ") ; 8 } else { 9 S y s t e m . out . p r i n t l n ( x + " >= 15 ") ; 10 } 11 if ( x < 20) { 12 S y s t e m . out . p r i n t l n ( x + " < 20 ") ; 13 } else { 14 S y s t e m . out . p r i n t l n ( x + " >= 20 ") ; 15 } 16 17 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 12 // x = 17 // x = 22 12 >= 10 17 >= 10 22 >= 10 12 < 15 17 >= 15 22 >= 15 12 < 20 17 < 20 22 >= 20

(4)

3 4 } else if ( x < 15) { 5 S y s t e m . out . p r i n t l n ( x + " < 15 ") ; 6 7 } else if ( x < 20) { 8 S y s t e m . out . p r i n t l n ( x + " < 20 ") ; 9 10 } else { 11 S y s t e m . out . p r i n t l n ( x + " >= 20 ") ; 12 } 13 14 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 12 // x = 15 // x = 22 12 < 15 15 < 20 22 >= 20

(5)

6. What is the output from the following code when x = 7? When x = 12? When x = 18? Also draw out the control ow diagram for this code to help your understand how it works.

1 if ( x < 10) { 2 S y s t e m . out . p r i n t l n ( x + " < 10 ") ; 3 4 if ( x < 15) { 5 S y s t e m . out . p r i n t l n ( x + " < 15 ") ; 6 7 if ( x < 20) { 8 S y s t e m . out . p r i n t l n ( x + " < 20 ") ; 9 } 10 } 11 } 12 13 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 7 // x = 12 // x = 18 7 < 10 Done ! Done ! 7 < 15 7 < 20 Done !

(6)

3 4 if ( x > 15) { 5 S y s t e m . out . p r i n t l n ( x + " > 15 ") ; 6 7 if ( x < 20) { 8 S y s t e m . out . p r i n t l n ( x + " < 20 ") ; 9 } 10 } 11 } 12 13 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 7 // x = 16 // x = 23 Done ! 16 > 10 23 > 10 16 > 15 23 > 15 16 < 20 Done ! Done !

(7)

8. What is the output from the following code when x = 7? When x = 18? When x = 10? Also draw out the control ow diagram for this code to help your understand how it works.

1 if ( x > 10) { 2 S y s t e m . out . p r i n t l n (" May be ... ") ; 3 4 if ( x > 15) { 5 S y s t e m . out . p r i n t l n (" H u r r a y ! ") ; 6 } else { 7 S y s t e m . out . p r i n t l n (" No dice . ") ; 8 } 9 10 } else if ( x < 9) { 11 S y s t e m . out . p r i n t l n (" S h u c k s ... ") ; 12 } 13 14 S y s t e m . out . p r i n t l n (" Done ! ") ; Solution: // x = 7 // x = 18 // x = 10 S h u c k s ... May be ... Done ! Done ! H u r r a y ! Done !

(8)

3 4 y = 13 % 5; // y = 3 5 6 z = z * y + 4; // z = 10.6 7 8 if ( x <= z ) { // false 9 if ( y % 2 == 0 ) { 10 x = y / 5 + 2; 11 } 12 13 if ( y % 2 == 1 ) { 14 x = y + 5; 15 } else { 16 x = 7 * y ; 17 } 18 19 z = 1.1 + (int) 3.1 * y ; 20 21 x ++; 22 23 } else { 24 if ( x % 2 != 0 ) { // false 25 y = x / 5 + 7; 26 } 27 28 if ( x % 3 == 1 ) { // true 29 y = y + x + 5; // y = 24 30 } 31 32 x = (int) ( z * 3.0) ; // x = 31 33 34 y ++; // y = 25 35 } Solution: x = 31 y = 25 z = 10.6

(9)

Code Writing

10. Write code to determine whether or not a given integer is divisible by 3. Print the result (e.g., Divisible by 3. or Not divisible by 3.).

Solution:

i m p o r t java . util . S c a n n e r ;

i m p o r t java . util . D e c i m a l F o r m a t ;

p u b l i c cla ss D i v B y T h r e e {

p u b l i c s t a t i c void main ( S t r i n g [] args ) {

int num = ...; // a s s u m e this has been i n i t i a l i z e d to some va lue

if ( num \% 3 == 0) { S y s t e m . out . p r i n t l n (" D i v i s i b l e by 3. ") ; } else { S y s t e m . out . p r i n t l n (" Not d i v i s i b l e by 3. ") ; } } }

11. Write code that, for a given number, prints Fizz if the number is divisible by 3, Buzz if the number is divisible by 5, and Fizz Buzz if the number is divisible by both. There are many possible answers to this problem, so be sure to type yours up and try it out in Eclipse! Be sure to pay careful attention to the ordering of your conditions.

Solution:

i m p o r t java . util . S c a n n e r ;

i m p o r t java . util . D e c i m a l F o r m a t ;

p u b l i c cla ss F i z z B u z z {

p u b l i c s t a t i c void main ( S t r i n g [] args ) {

int num = ...; // a s s u m e this has been i n i t i a l i z e d to some va lue

if ( num \% 15 == 0) {

S y s t e m . out . p r i n t l n (" Fizz Buzz ") ; } else if ( num \% 3 == 0) { S y s t e m . out . p r i n t l n (" Fizz ") ; } else if ( num \% 5 == 0) { S y s t e m . out . p r i n t l n (" Buzz ") ; } } }

References

Related documents

Full vat or the delivery time for shipping options and what does dhl express in lincoln, warehouse and track courier tracking number?. Find it sometimes does usps package

40 is divisible by the prime numbers 2 and 5. 75 is divisible by the prime numbers 3 and 5. 81 is divisible by the prime number 3.. Draw a factor tree for each number.. Use a

General Information: The Taxable Mortgage Program (My First Texas Home) provides the homebuyer with a 30-year fixed interest rate mortgage loan and 5% assistance to

• If you are claiming for damage to the buildings please contact our claims department. We will usually require two estimates for repair, although we may appoint a loss

Based on their behaviour in simple examples and their success in numerical simulations, it is believed that the maximal and CMC slicing conditions will in general produce

Preliminary Structural Design Preliminary Structural Design Based on Vertical Loads Plus an Based on Vertical Loads Plus an allowance for Lateral Loads. allowance for Lateral Loads

Jeroen Frijters — An Introduction to .NET for the J2EE Programmer Page 10 Managed Code Verifiably Type Safe Managed Code... Java

The difference in potential energy between two positions of a particle is defined as the work done by applied force on the particle in moving it from the first position to