Tuesday, October 4, 2011

Java Programming Helper


Conditionals
Conditions
Simple-2

1.  Write an expression that evaluates to true if and only if the integer x is greater than the integer y.
Example:  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.

x > y

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

workedOvertime == true

Compound-2

1.  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.

temperature > 90 && humidity < 10 == true

2.  Given the integer variables yearsWithCompany and department, write an expression that evaluates to true if yearsWithCompany is less than 5 and  department is not equal to 99.

yearsWithCompany < 5 && department != 99

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

age < 19 || isFullTimeStudent == true

4.  Write an expression that evaluates to true if and only if value of the boolean variable isAMember is  false.

isAMember == false

Statements

Simple_if

1.  Write a conditional that assigns the boolean value true to the variable fever if the variable temperature is greater than 98.6.
Example:  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.

if ( temperature > 98.6 ) fever = true ;

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

if ( goodsSold > 500000 ) bonus = 10000 ;

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

if ( outsideTemperature > 90 ) shelfLife -= 4 ;

4.  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.

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

Simple_if_else

1.  Write an if/else statement that compares the variable age with 65 , adds 1 to the variable seniorCitizens if  age is greater than or equal to 65 , and adds 1 to the variable nonSeniors otherwise.
Example:  So if age has the value 73, seniorCitizens has the value 450, and nonSeniors has the value 1066, then after your code executes seniorCitizens would then have the value 451 but nonSeniors would still be 1066.
On the other hand, if age has the value 21, then after your code executes seniorCitizens would still have the value 450 but nonSeniors would then be 1067.

if ( age >= 65 ) seniorCitizens += 1 ; else nonSeniors += 1 ;

2.  Write an if/else statement that compares the value of the variables soldYesterday and  soldToday, and based upon that comparison assigns salesTrend the value -1 or 1.
-1 represents the case where soldYesterday is greater than soldToday; 1 represents the case where soldYesterday is not greater than soldToday.

if ( soldYesterday > soldToday ) salesTrend = -1 ; else salesTrend = 1 ;

3.  Write an if/else statement that assigns  true to the variable fever if the variable temperature is greater than 98.6; otherwise it assigns false to fever.

if ( temperature > 98.6 ) fever = true ; else fever = false ;

Cascaded

1.  Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if  age is 18 through 64 and adds 1 to the variable seniors if age is 65 or older.

if (age < 18) minors ++ ; else if (age >= 18 && age <= 64) adults ++ ; else seniors ++ ;

2.  Write an if/else statement that compares the double variable  pH with 7.0 and makes the following assignments to the int variables neutral , base, and acid:
0,0,1 if pH is less than 7
0,1,0 if pH is greater than 7
1,0,0 if pH is equal to 7

if (pH == 7.0)
{
neutral = 1;
base = 0;
acid = 0;
}
else if (pH < 7)
{
neutral = 0;
base = 0;
acid = 1;
}
else
{
acid = 0;
base = 1;
neutral = 0;
}

Switch

1.  Write a switch statement that tests the value of the char variable response and performs the following actions:
if response is y, the message               Your request is being processed is printed
if response is n, the message               Thank you anyway for your consideration is printed
if response is h, the message               Sorry, no help is currently available is printed
for any other value of  response , the message           Invalid entry; please try again is printed

switch (response)
{
case 'y':
System.out.println("Your request is being processed");
break;
case 'n':
System.out.println("Thank you anyway for your consideration");
break;
case 'h':
System.out.println("Sorry, no help is currently available");
break;
default:
System.out.println("Invalid entry; please try again");
break;
}

The Conditional Expression

1.  Write an expression using the conditional operator (? :) that compares the value of the variable x to 5 and results in:
x if  x is greater than or equal to  5
-x if  x is less than  5
Example:  If the value of x is 15, then the value of the expression you write should be 15.
On the other hand, if the value of x is 3, then the value of the expression you write should be -3.

( x >= 5 ) ? x : - x

2.  Write an expression using the conditional operator (? :) that compares the values of the variables x and y.  The result (that is the value) of this expression should be the value of in the larger of the two variables.

( x > y ) ? x : y


1 comment: