Friday, February 14, 2014

Section 4.1: Relational and Logical Operators - Ranges




41117:  Clunker Motors Inc. is recalling all vehicles from model years 2001-2006.  A bool variable named norecall has been declared.  Given an int variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the recall range and assigns false otherwise.  Do not use an if statement in this exercise!

norecall = (modelYear < 2001 Or modelYear > 2006)

41067:  Clunker Motors Inc. is recalling all vehicles from model years 2001-2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to recalled if the value of modelYear falls within the recall range and assigns false otherwise.

recalled = (modelYear >= 2001) And (modelYear <= 2006)

41068:  Clunker Motors Inc is recalling all vehicles from model years 1995-1998 and 2004-2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to recalled if the value of modelYear falls within the two recall ranges (the first range being 1995-1998, the second range being 2004-2006) and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = ((modelYear >= 1995) And ((modelYear <= 1998)) Or (modelYear >=2004) And (modelYear <=2006))

41118:  Clunker Motors Inc is recalling all vehicles from model years 1995 - 1998 and 2004 - 2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the two recall ranges (2001-2006) and assigns false otherwise.  Do not use an if statement in this exercise!

norecall = (modelYear < 1995 Or modelYear > 1998) And (modelYear < 2004 Or modelYear > 2006)

41101:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an upper-case letter.

(x >= "A") And (x <= "Z")

41102:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a lower-case letter.

(x >= "a") And (x <= "z")

41103:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a decimal digit (0-9).

(x >= "0") And (x <= "9")




41104:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an octal (Base 8) digit (0-7).

(x >= "0") And (x <= "7")

41115:  Assume that s is a string.  Write an expression whose value is true if and only if the value of s would come between "mortgage" and "mortuary" in the dictionary.

(s >= "mortgage") And (s <= "mortuary")

41105:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a letter.

(x >= "A") And (x <= "Z") Or (x >= "a") And (x <= "z")

41106:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is alphanumeric, that is either a letter or a decimal digit.

((x >= "A") And (x <= "Z")) Or ((x >= "a") And (x <= "z")) Or ((x >= "0") And (x <= "9"))

41107:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an hexadecimal (Base 16) digit (0-9 plus A-F or a-f).

((x >= "A") And (x <= "F")) Or ((x >= "a") And (x <= "f")) Or ((x >= "0") And (x <= "9"))

41113:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is NOT un upper-case letter.

((x < "A") Or (x > "Z"))

41114:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is NOT a letter.

((x < "A") Or (x > "Z")) And ((x < "a") Or (x > "z"))

41072:  Clunker Motors, Inc is recalling all vehicles in its Extravagant line from model years 1999 - 2002 as well as all vehicles in its Guzzler line from model years 2004 - 2007.  A bool variable named recalled has been declared.  Given an int variable modelYear and a string modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = (modelYear >= 2004) And (ModelYear <= 2007) And (modelName = "Guzzler") Or (modelYear >= 1999) And (ModelYear <= 2002) And (modelName = "Extravagant")




41070:  Clunker Motos, Inc is recalling all vehicles in its Extragagant line from model years 1999-2002.  A bool variable named recalled has been declared.  Given an int variable modelYear and a string modelName write a statement that assigns true to recalled if hte values of modelYear and modelName match the recall details and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = (modelYear >= 1999) And (ModelYear <= 2002) And (modelName = "Extravagant")

2 comments: