Chapter 3: Selections
Write an expression that evaluates to true if and only if value of the boolean variable isAMember is false.
!(isAMember)
Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression's value is "freshman"; 30-59 would be a "sophomore", 60-89 would be a "junior" and 90 or more would be a "senior"
((credits < 30) ? "freshman": ((credits < 60 && credits > 29)?"sophomore": ((credits >59 && credits < 90)?"junior": ((credits > 89)?"senior": "invalid"))))
Assume that month is an int variable whose value is 1 or 2 or 3 or 4 or 5 ... or 11 or 12. Write an expression whose value is "jan" or "feb" or "mar" or "apr" or "may" or "jun" or "aug" or "sep" or "oct" or "nov" or "dec" based on the value of month. (so, if the value of month were 4 then the value of the expression would be "apr").
((month==1)?"jan": ((month==2)?"feb": ((month==3)?"mar": ((month==4)?"apr": ((month==5)?"may": ((month==6)?"jun":((month==7)?"jul":((month==8)?"aug":((month==9)?"sep":((month==10)?"oct":((month==11)?"nov":((month==12)?"dec": "invalid"))))))))))))
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)? true:false
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 the larger of the two variables.
(x < y ? y : x)
Four int variables, x1, x2, y1, and y2, have been declared and been given values. Write an expression whose value is the difference between the larger of x1 and x2 and the smaller of y1 and y2.
(x1 < x2 ? x2 : x1)-(y1 < y2 ?y1 : y2)
Declare a variable hasPassedTest, and initialize it to true.
boolean hasPassedTest = true;
NOTE: In mathematics, division by zero is undefined. So, in java, division by zero is always an error. Given an int variable named callsReceived and another int variable named operatorsOnCall, write the necessary code to read values into callsReceived and operatorsOnCall, and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID". ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.
callsReceived = stdin.nextInt(); operatorsOnCall = stdin.nextInt(); if (operatorsOnCall > 0){ System.out.println(callsReceived / operatorsOncall); } else{ System.out.println("INVALID"); }
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.
grossPay < 10000
Clunker Motors Inc. is recalling all vehicles from model years 1995 - 1998 and 2004 - 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 fall within the two recall ranges and assigns false otherwise. Do not use an if statement in this exercise!
if ((modelYear > 1994 && modelYear < 1999 ) || (modelYear > 2003 && modelYear < 2007 )) { norecall = false; }else{ norecall = true; }
Assume that the variables gpa, deansList and studentName, have been declared and initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5
if (gpa > 3.5){ deansList = deansList + 1; System.out.println(studentName); }
Write a statement that toogles 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.
if (onOffSwitch){ onOffSwitch=false; }else{ onOffSwitch = true; }
Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the message "player1 wins" is printed to standard out. When score2 exceeds score1 the message "player2 wins" is printed to standard out. In each case, the variables player1Wins, player1Losses, player2Wins, and player2Losses, are incremented when appropriate. Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented.
if (score1 > score2){ System.out.println("player1 wins"); player1Wins += 1; player2Losses += 1; }else if (score2 > score1){ System.out.println("player2 wins"); player1Losses +=1; player2Wins +=1; }else{ System.out.println("tie"); tieCount +=1; }
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; }
Given the variables numberOfMen of numberOfWomen, write an expression that evaluates to true if the number of men is greater than or equal to the number of women.
numberOfMen >= numberOfWomen
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.
numberOfPrizes % numberOfParticipants == 0
Given an int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out "YES WAS RECORDED" if the value typed in is a 3 or 4 then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input
response = stdin.nextInt(); if (response == 1 || response == 4){ noCount++; System.out.println("NO WAS RECORDED"); }else System.out.println("INVALID");
HTTP is the protocol that governs communications between web servers and web clients (i.e browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: 200, OK (fulfilled) 403, forbidden 404, not found 500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
switch ( status ){ case 200: System.out.println("OK(fulfilled)");break; case 403: System.out.println("forbidden");break; case 404: System.out.println("not found");break; case 500: System.out.println("server error"); }
In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-9, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age.
switch(age){ case 1:System.out.println("ineligible");break; case 2:System.out.println("toddler");break; case 3:System.out.println("early childhood");break; case 4:System.out.println("young reader");break; case 5:System.out.println("elementary");break; case 6:System.out.println("middle");break; case 7:System.out.println("impossible");break; case 8:System.out.println("high school");break; case 9:System.out.println("scholar");break; case 10:System.out.println(" ineligible");break;
Write an expression that evaluates to true if and only id the value of the boolean variable workedOvertime is true
workedOvertime
Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value
x % 2 == 0
Write an expression that evaluates to true if and only if the integer x is greater than the integer y
x > y
Write an expression using 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 then 5
x >= 5 ? x : -x