|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
As a review in if-else, let's complete the following method, to make estimated grades for the registrar's office at midk-semester (or, any time during the semester). We'll use a rather stupid algorithm for the moment (although we know how to extend it, using multiple else-if clauses):
/* If somebody's score is 85% or more of the points possible, * we'll return "A" * otherwise we return "B". */ String estimateGrade( int ptsEarned, int ptsPossible ) { // ...?? } |
So, our code incorporated into a web page that students can visit whenever they want, and the latest letter-grade-estimate gets incorporated into the page. This works great … until the first day of a (new) semester, when suddenly all my students report that the web page crashes! I look at the error log and see “division by zero” errors! Rats — if only my test cases would have included the edge-case of ptsPossible == 0, I would have averted a bunch of disgruntled students!
How to fix? First, we need to decide what the correct answer should be: While it might be nice to return a String like "N/A", the web-page software which calls our method might require us to always return a letter grade. What does MLB do? At the start of a season, everybody's batting average is . We'll do the same: at the start of the semester, we want everybody's grade to be . That is, we want to return "A" if ptsPossible == 0 or the student's average is at least 85%: lect08a-soln.html
In general, && looks at the boolean expressions on each side, and the entire && will evaluate to true if (and only if) the left side and the right side evaluate to true:
expr1 | expr2 | (expr1&&expr2) |
false | false | false |
false | true | false |
true | false | false |
true | true | true |
expr1 | expr2 | (expr1||expr2) |
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Exercise: What is the value of:
( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) ) |
It's worth a mention that in English, “or” is used in two different ways: The inclusive-or, which is what || means, means “one or the other or both”. For example, if the question on the tax form asks “are you over 18, or had income of more than $20,000”, and you meet both criteria, then the answer is true. (People might use “and/or” to emphasize they include the “and” part.) However, sometimes English uses “or” to mean one or the other but not both. For instance, “You can eat veal, or you can be morally responsible” is implying that one option precludes the other. (You may or may not agree with that presumption, of course.) Similarly, if you are told “Keep dating your ex, or date me!”, don't presume that the option of both is included in the “or”!
While we're on the topic, there is one last boolean operator, ! (“not”):
expr | !expr |
false | true |
true | false |
Exercise: What is
We see from BlueJ's codepad, that answers have the type boolean. We can use this type wherever we use any type in Java: declare variables which hold a boolean, write methods which return a boolean, write methods with boolean parameters (inputs).
/** Return a message to be incorporated into the waiting-area display screen. * @param planeCapacity The number of passenger seats on the plane. * @param ticketsSold The number of tickets sold on this flight. * @return a message to be incorporated into the waiting-area display screen. */ String boardingRoomMessage( int ticketsSold, int planeCapacity ) { boolean areEnoughSeats; areEnoughSeats = (numTickets <= planeCapacity); roomIsOkay if (roomIsOkay == true) { return "Please board whan called."; } else { return "The flight is overbooked; panic!"; } } |
Reached here, 9am section
1Just like we think of + and * as functions which take in two numbers, and return a number. ↩
2If you look at the tables and squint, thinking of false,true being like 0,1 and ||,&& being like +,* then these tables above really do look fairly similar to arithmetic. ↩
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Mar.16 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |