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.
SOLUTION
x > y
————————————————————————————————————
20618
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.
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.
SOLUTION
if ( age >= 65 ) seniorCitizens += 1 ; else nonSeniors += 1 ;
————————————————————————————————————
20619
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 .
SOLUTION
if ( soldYesterday > soldToday ) salesTrend = -1 ; else salesTrend = 1 ;
————————————————————————————————————
20620
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 .
SOLUTION
if ( temperature > 98.6 ) fever = true ; else fever = false ;
————————————————————————————————————
20621
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.
SOLUTION
if (age < 18) minors ++ ; else if (age >= 18 && age <= 64) adults ++ ; else seniors ++ ;
————————————————————————————————————
20622
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
SOLUTION
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;
}
————————————————————————————————————
20651
Write the definition of a method printDottedLine , which has no parameters and doesn’t return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods.
SOLUTION
public static void printDottedLine ()
{
System.out.println(“…..”);
}
————————————————————————————————————
20652
Write the definition of a method printGrade , which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character ) to standard output . Don’t forget to put a new line character at the end of your line. Thus if the value of the parameter is ‘A’, the method prints out
Grade: A
SOLUTION
public static void printGrade
( char x ){
System.out.println(“Grade: ” + x);}
————————————————————————————————————
20655
Write the definition of a method twice , which receives an integer parameter and returns an integer that is twice the value of the parameter .
SOLUTION
public int twice (int x)
{
return 2*x;
}
————————————————————————————————————
20656
Write the definition of a method add , which receives two integer parameters and returns their sum .
SOLUTION
public int add (int x,int y)
{
return x+y;
}
————————————————————————————————————
20660
Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min .
Assume that all the variables have already been declared and that x , y , and z have been assigned values .
So if the value of x is 19 the value of y is 13, and the value of z is 23, then after your code executes,
the value of min would be 13.
SOLUTION
min = Math.min(Math.min(x,y),z);