Chapter 5

20608
Write an expression  that evaluates  to true  if and only if the integer  x is greater than  the integer  y .
So, if the value of x was 15 and the value of y was 11, then the value of your expression would be true.

On the other hand, if the value of x was 145 and the value of y was 211, then the value of your expression would be false.
ANSWER
x > y
--------------------------------------------------------------------------------------------------------
20522
Write an expression  that evaluates  to true  if and only if the value  of the integer  variable  x is equal  to zero.
So if the value of x were 3 the expression would be false. Only if the value of x were 0 would the expression be true.

ANSWER
x==0

--------------------------------------------------------------------------------------------------------
20533
Write an expression  that evaluates  to true  if and only if the variables  profits and losses are exactly equal .

ANSWER
profits==losses

--------------------------------------------------------------------------------------------------------
20554
Given  the char variable  c , write an expression  that is true  if and only if the value  of c is not the space character .

ANSWER
c!=' '

--------------------------------------------------------------------------------------------------------
20555
Write an expression  that evaluates  to true  if the value  of index is greater than  the value  of lastIndex .

ANSWER
index>lastIndex

---------------------------------------------------------------------------------------------------------
20556
Working overtime is defined as having worked more than 40 hours during the week. Given  the variable  hoursWorked , write an expression  that evaluates  to true  if the employee worked overtime.

ANSWER
hoursWorked>40

---------------------------------------------------------------------------------------------------------
20557
Write an expression  that evaluates  to true  if the value  x is greater than  or equal  to y .

ANSWER
x>=y

---------------------------------------------------------------------------------------------------------
20558
Given  the variables  numberOfMen and numberOfWomen , write an expression  that evaluates  to true  if the number of men is greater than  or equal  to the number of women.

ANSWER
numberOfMen>=numberOfWomen

---------------------------------------------------------------------------------------------------------
20559
Given  a double variable  called average , write an expression  that is true  if and only if the variable 's value  is less than  60.0 .

ANSWER
average<60
---------------------------------------------------------------------------------------------------------
20560

Given  an int variable   grossPay , write an expression  that evaluates  to true  if and only if the value  of  grossPay is less than   10,000 .

ANSWER
grossPay<10000

----------------------------------------------------------------------------------------------------------
20666

Write an expression  that evaluates  to true  if the value  of the integer  variable   widthOfBox is not divisible by the value  of the integer  variable   widthOfBook . Assume  that  widthOfBook is not zero. ("Not divisible" means has a remainder.)


ANSWER
(widthOfBox%widthOfBook!=0)

----------------------------------------------------------------------------------------------------------
20667
Write an expression  that evaluates  to true  if the integer  variable   x contains an evenvalue , and false  if it contains an oddvalue

          For instance, 4, 10, and 256 are even numbers. If x had any of these values your expression
          should be true.

          On the other hand, 9, 13, 121 are odd. If x had any of these values your expression should be false.

ANSWER
(x%2==0)

----------------------------------------------------------------------------------------------------------
20665
 Write an expression  that evaluates  to true  if the value  of the integer  variable   numberOfPrizes is divisible (with no remainder) by the integer  variable   numberOfParticipants . Assume  that  numberOfParticipants is not zero.

ANSWER
numberOfPrizes%numberOfParticipants==0

----------------------------------------------------------------------------------------------------------
20922

Assume  that a boolean  variable   workedOvertime has been declared , and that another variable ,  hoursWorked has been declared  and initialized . Write a statement  that assigns  the value  true  to  workedOvertime if  hoursWorked is greater than  40 and false  otherwise.

ANSWER
if
     (hoursWorked>40)
      workedOvertime=true;
else
      workedOvertime=false;

------------------------------------------------------------------------------------------------------------
20921
Assume  that a boolean  variable  isQuadrilateral has been declared , and that another variable , numberOfSides has been declared  and initialized . Write a statement  that assigns  the value  true  if numberOfSides is exactly 4 and false  otherwise.

ANSWER
if
     (numberOfSides ==4)
      isQuadrilateral = true;
else
      isQuadrilateral = false;

------------------------------------------------------------------------------------------------------------
20561
Given  that the variables   x and  y have already  been declared  and assigned  values , write an expression  that evaluates  to true  if  x is non-negative and y is negative.


       So, if the value of x were 4 and the value of y were -3 then the expression would be true.

       On the other hand, if the value of x were 4 and the value of y were 1 then the value of the
       expression would be false.

ANSWER
x >= 0 && y < 0

------------------------------------------------------------------------------------------------------------
20610
Given  the variables   temperature and  humidity , write an expression  that evaluates  to true  if and only if the temperature is greater than   90 and the humidity is less than   10 .

ANSWER
temperature>90 && humidity<10  == true

------------------------------------------------------------------------------------------------------------
20562

Given  that the variables   x and  y have already  been declared  and assigned  values , write an expression  that evaluates  to true  if  x is positive (including zero) or y is negative.

ANSWER
x>=0||y<0==true

-----------------------------------------------------------------------------------------------------------
20613
Write an expression  that evaluates  to true  if and only if value  of the boolean variable  isAMember is false .
ANSWER
isAMember==false

-----------------------------------------------------------------------------------------------------------
20563
Given  two variables ,  isEmpty of type  boolean  , indicating whether  a class  roster is empty or not, and  numberOfCredits of type  int  , containing the number of credits for a class , write an expression  that evaluates  to true  if the class  roster is empty or the class  is exactly three credits. 

ANSWER
isEmpty || numberOfCredits == 3

-----------------------------------------------------------------------------------------------------------
20564
Given  two variables ,  isEmpty of type  boolean  , indicating whether  a class  roster is empty or not, and  numberOfCredits of type  int  , containing the number of credits for a class , write an expression  that evaluates  to true  if the class  roster is not empty and the class  is more than two credits. 

ANSWER
!isEmpty && numberOfCredits > 2

----------------------------------------------------------------------------------------------------------
20565
Given  two variables ,  isEmpty of type  boolean  , indicating whether  a class  roster is empty or not, and  numberOfCredits of type  integer  , containing the number of credits for a class , write an expression  that evaluates  to true  if the class  roster is not empty and the class  is one or three credits.

        So, if the value of isEmpty were false and the value of numberOfCredits were 3 then the
        expression would be true. 

        On the other hand, if the value of isEmpty were true or the value of numberOfCredits
        were 2 then the value of the expression would be false.
ANSWER
!isEmpty && (numberOfCredits == 1 || numberOfCredits == 3)

--------------------------------------------------------------------------------------------------------
20611
Given  the integer variables   yearsWithCompany and  department , write an expression  that evaluates  to true  if  yearsWithCompany is less than   5 and  department is notequal  to  99 . 

ANSWER
yearsWithCompany < 5 && department != 99

-------------------------------------------------------------------------------------------------------
20612
Given  the variables   isFullTimeStudent and  age , write an expression  that evaluates  to true  if  age is less than   19 or  isFullTimeStudent is true  . 

ANSWER
age < 19 || isFullTimeStudent == true

-------------------------------------------------------------------------------------------------------
20609
Write an expression  that evaluates  to true  if and only if the value  of the boolean variable   workedOvertime is true  . 

ANSWER
workedOvertime == true

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

Do not use an if statement  in this exercise!

ANSWER
recalled=(modelYear>=2001 && modelYear<=2006);

------------------------------------------------------------------------------------------------------
20946
Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. A boolean variable  named   norecall has been declared . Given  a variable   modelYear write a statement  that assigns  true  to norecall if the value  of  modelYear does NOT within the recall range and assigns  false  otherwise.

Do not use an if statement  in this exercise!

ANSWER
norecall=!(modelYear >= 2001 && modelYear <= 2006);

------------------------------------------------------------------------------------------------------
20941
 Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. A boolean variable  named  recalled has been declared . Given  a variable   modelYear write a statement  that assigns  true  to recalled if the value  of modelYear falls within the two recall ranges and assigns  false  otherwise.

Do not use an if statement  in this exercise!

ANSWER
recalled = (modelYear >= 1995 && modelYear <= 1998) || (modelYear >= 2004 && modelYear <= 2006);

------------------------------------------------------------------------------------------------------
20942

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. A boolean variable  named  recalled has been declared . Given  a variable   modelYear write a statement  that assigns  true  to norecall if the value  of modelYear does NOT fall within the two recall ranges and assigns  false  otherwise.

Do not use an if statement  in this exercise!

ANSWER
norecall=!((modelYear>=1995 && modelYear <=1998) || (modelYear>=2004 && modelYear<=2006));

-------------------------------------------------------------------------------------------------------
20673

Write a statement  that toggles the value  of  onOffSwitch . That is, if  onOffSwitch is false , its value  is changed to true ; if  onOffSwitch is true , its value  is changed to false .

ANSWER

if
   (onOffSwitch==false)
{
   onOffSwitch=true;
}
else
{
   onOffSwitch=false;
}

-----------------------------------------------------------------------------------------------------
20756
Assign to the boolean  variable  'possibleCandidate' the value  false  if the int  variable  'n' is even and greater than  2, or if the variable  'n' is less than  or equal  to 0; otherwise, assign  true  to 'possibleCandidate'. 

Assume  'possibleCandidate' and 'n' are already  declared  and 'n' assigned  a value .

ANSWER
possibleCandidate = !(n % 2 == 0 && n > 2 || n <= 0);

-----------------------------------------------------------------------------------------------------
20760
Write a class  definition of a class  named  'Value ' with the following: 
a boolean  instance variable  named  'modified', initialized  to false  
an integer  instance variable  named  'val' 
a constructor  accepting a single parameter  whose value  is assigned  to the instance variable  'val' 
a method  'getVal' that returns the current value  of the instance variable  'val' 
a method  'setVal' that accepts a single parameter , assigns  its value  to 'val', and sets the 'modified' instance variable  to true , and 
a boolean  method , 'wasModified' that returns true  if setVal was ever called. 

ANSWER
public class Value
{
private boolean modified = false;
private int val;

public Value(int val) 
{
this.val=val;
}
public int getVal()
{
return val;
}
public void setVal(int val)
{
this.val = val;
modified=true;
}
public boolean wasModified()
{
return modified;
}
}

----------------------------------------------------------------------------------------------------

20772

Write a class  definition of a class  named  'Value ' with the following:
a constructor  accepting a single integer  parameter
a constructor  with no parameters
a method  'setVal' that accepts a single parameter ,
a boolean  method , 'wasModified' that returns true  if setVal was ever called for the object .
a method  'getVal' that returns an integer  value  as follows: if setVal has ever been called, it getVal returns the last value  passed to setVal. Otherwise if the "single int  parameter " constructor  was used to create the object , getVal returns the value  passed to that constructor . Otherwise getVal returns 0.

ANSWER

public class Value
{
private boolean modified = false;
public int val;
public Value(int v)
{
val=v;
}
public int getVal()
{
return val;
}
public void setVal(int st)
{
val=st;
modified=true;
}
public boolean wasModified()
{
return modified;
}
}

-----------------------------------------------------------------------------------------------------
20614
Write a conditional  that assigns  the boolean value  true  to the variable   fever if the variable   temperature is greater than   98.6


      So if temperature has the value 99.5, after your code executes fever would have the value true.

      On the other hand, if temperature has the value 96.3, your code will not change the value of fever at all.



ANSWER
if ( temperature > 98.6 ) fever = true ;

---------------------------------------------------------------------------------------------------------
20615

Write a conditional  that assigns   10,000 to the variable   bonus if the value  of the variable   goodsSold is greater than   500,000 .

ANSWER
if ( goodsSold > 500000 ) bonus = 10000 ;

--------------------------------------------------------------------------------------------------------
20616
 Write a conditional  that decreases  the variable   shelfLife by  4 if the variable   outsideTemperature is greater than   90 . 

ANSWER
if ( outsideTemperature > 90 ) shelfLife -= 4 ;

--------------------------------------------------------------------------------------------------------
20617
Write a conditional  that multiplies the value  of the variable   pay by one-and-a-half if the value  of the boolean variable   workedOvertime is true  . 

ANSWER
if ( workedOvertime == true ) pay *= 1.5 ;

-------------------------------------------------------------------------------------------------------









No comments:

Post a Comment