Week 3 Homework

Please read carefully the questions that follow. Do your research, write up your responses (on your own computer) to at least one question from 1~5 AND at least one question from 6~9. Support your answers with examples, and post them into the current conference.

2. (a) Why doesn't the statement if (a <= b <= c)work?

Because of the left associativity of relational operators makes the statement evaluate incorrectly in Java.  It will not compile.  The expression should be re-written as if (a <= b) && (b <=c).

(b) Define an expression that evaluates to true when i lies in the interval 6...9.

I'm not sure if you wanted the numbers that lie BETWEEN 6-9 or to include 6 and 9.  Below takes numbers that lie between 6 and 9.  Expression to select numbers between and including 6 and 9 -- if ((x=>6) && (x<=9))

if ((x>6) && (x<9))

another way is

if (( x == 7) || (x == 8))

7.  Switch statements are shorthands for a certain kind of if statement. It is not uncommon to see a stack of if statements all relate to the same quantity like this:

if (x == 0) doSomething0();
else if (x == 1) doSomething1();
else if (x == 2) doSomething2();
else if (x == 3) doSomething3();
else if (x == 4) doSomething4();
else doSomethingElse();

Re-write above using a switch-case statement.

switch (x) {

     case ‘0’:

        dosomething0();

        break;

    case ‘1’:

        dosomething1();

        break;

case ‘2’:

        dosomething2();

        break;

     case ‘3’:

        dosomething3();

        break;

     case ‘4’:

        dosomething4();

        break;

default:

        doSomethingElse();

}

Send me email

Visit my website

 


 

Home | Photos | WaterMark Multimedia | Online Shopping Outlet

Matt's Bio | UMUC | Feedback | Guest book | E-mail me

 

FaulknerWeb.com is designed and managed by Matt Faulkner.

Domain Name service provided by Namesecure

Web Service by eDataRack
Copyright © 1998 - 2011 [FaulknerWeb.com]. All rights reserved.
Updated: October 15, 2011 21:53 .