This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.
Learning Objectives
- Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
- Learn about arithmetic, relational, and logical operators
- Understand how to obtain input from the user of our programs
Intended Audience
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
Prerequisites
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
So, what have we learned so far about operators?
Well, we can classify them by what they do. So, we have arithmetic operators and relational operators and are about to explore logical operators. We can also classify operators by how many operands they take. If an operator takes two operands, it is a binary operator. And if it takes one, it is a unary operator. With the logical operators, we have these things called truth tables. And each truth table indicates what the values of combined Boolean values or expressions end with. So, applying Logical AND, two ampersand signs, you can see that the operands P and Q, both must be true to result in a combined true value. So, the two ampersands must be used to indicate that you are using Logical AND. For Logical OR, if either operand is true, the combined result is true. The two vertical bars are used to indicate use of Logical OR. On a standard US and British keyboard, the vertical bar is found on the same key as the backslash, and it's just above the Enter or Return key on the right side of the keyboard. Those two are binary operators. And now, we have a unary operator, Logical NOT. For this operator, the truth value of its operand is just flipped.
So if the operand is true, the result of applying Logical NOT on it is false. If the operand is false, applying the Logical NOT on it results in true, and that's it. Two of the logical operators again are binary, the Logical AND, and Logical OR. And one of them is unary, Logical NOT. I encourage all of my students to memorize these truth tables and be able to reproduce them whenever asked to. It may sound a little silly but more than one person has told me they were asked to do so during a job interview and I've certainly asked my students to do so during a test. So, that's pretty cool. How might we use these operators in code? Let's create a file called logical fun and take a look at how we might use them. So, we'll go over here, and we will right click 'Source', 'New java class', LogicalFun and public static void main (String[ ] args) and my little comment there. Okay, so with this one, we're going to work with two Boolean variables, actually more than that, but two are going to be the foundation. We have Boolean is Raining and we'll start that with true and then we'll have Boolean is Warm and set that to true as well.
Often Boolean variables are given names like, is this or is that, or do this do that, kind of like they're an action or a verb or a verb phrase. So Boolean combined, this would be an instance when it does not. So if we do this is Raining and isWarm. So in this case these two variables are acting like the p's and the Q's from the truth table that we saw earlier, and the P and the Q in the truth table, just note that they could be variables, they could be literal values like true or false, they could also be a method call or anything else that results in a Boolean value. Now let's print this out. System.out.print will say it is raining or is it rather, "is it raining and warm?" And we'll put the combined result, it'll say either true or false. Now we'll do combined, we're going to reuse this Boolean variable, you'll notice I'm not redeclaring it like I did here, but I'm reassigning it a different value. It's not a constant, so it's totally fine. So isRaining or isWarm. And then system.out.println(), we will say, ("is it raining or warm?") So as long as one of these is true, then the whole statement is true. For and, you need both of the statements, is Raining and isWarm, you need both of these in this case variables to be true. So let's see what happens with combined and then we will do this combined equals not is Raining. This time
we're using the unary Logical NOT operator. ''Is it not raining outside?" and we could of course use this with the, is Warm as well, if we wanted to. So there you go and it's kind of a misnomer at this case because it's not combined, it's just taking the Boolean value and flipping it but I didn't want to declare a completely separate variable just for semantics sake. For an example, now if you were doing this in industrial code, you might need to do something like that. But for our example and our purposes, this is okay. So let's run it of course. I'm going to right click ''LogicalFun'' and then go down to Run'LogicalFun.main()' and here we go. Is it raining and warm? Since both of them are true, the result is of course true. And then it says, is it raining or warm? Well it's also the case that one of them at least one of them is true so, the whole thing is true.
Now if we ask, is it not raining outside? What that does is, it flips the truth value. The Boolean value of in this case raining, so not raining is false because it is raining is true. Nice. So the code is fairly straightforward. I'm going to repeat things about the logical operators so that you can really ingrain them into your head. For Logical AND, to result in true, both operands isRaining isWarm in this case, must be true. For logical OR to result in true, at least one must be true. And finally the Logical NOT operator just flips the truth value. So as a simple challenge for you, I would like you to just keep modifying the values for is Raining and isWarm, to try every permutation, rerunning the application to see the results of the logical operators each time. We've already run the code with both set to true. So now try the different permutations. As a hint there are three more possibilities remaining. So, pause the video and come back when you're done or if you need some help.
So how did that go for you? Were you able to complete this little challenge? I hope so. This one didn't require a ton of modifications. So let's do it together. So we're going to try the different permutations that are left. So we have true and true. Let's try true for isRaining and false for is Warm and see what our results are Rerun it. And in this case, you can see that, is it raining and warm? Is now false, Instead of true. Because both of them have to be true for the whole thing to be true. Now for, is it raining or warm? That's still true because at least one of them is true, isRaining is. What about is it not raining outside? Well we haven't changed isRaining, so that means that the true is flipped to false again. Now the next permutation we will try is false and true. So, I flipped those two. If I run this one you can see again. Is it raining and warm? Since one of them is false the whole expression is false. So, that's false now. What about, is it raining or warm? Well, that's still true, because one of them is still true. Right? And what about, is it not raining outside? Is it true that it is not raining? So, since is raining is false, when you flip is raining value. If you flip the false, that becomes true.
Now, the final permutation of these two values is false and false. Now, this will give us some more interesting results, Hopefully. Let's right click and run it. You'll see now that the end is of course still false because if even one of these is false the whole thing is false, but now or is false, because neither of them are true. You need at least one of them to be true for the or to combine them into a true value. Is it not raining outside is true, because it is raining is false and we flipped that value and then that is true. Nice work, everyone. So now we have a pretty strong handle on the vast majority of the operators you'll work within your day to day software development. Up next, we'll look at how to obtain input from the user. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.