Topics Discussed: Using what we’ve learned so far to calculate how many seconds are in a user-defined amount of time

Source Code Available Here

In this lesson I show you guys the power of using compound operators, as well as including a good amount of variables in a single print statement.
Compound operators
Throughout this lesson I combine a lot of statements in order to avoid using a ‘total’ variable, here’s the example:


       hours += (days*24); //line 1
       minutes += (hours*60);// line 2
       seconds += (minutes*60); // line 3

These three lines do the following things:
Line 1: Starts by taking the current value of hours and adds the value of days * 24 (meaning that days * 24 = number of hours in a day). So in one statement we count the number of hours in a day, and add that value to days
Line 2: Like the above, we are converting “downward”. We multiply hours by 60 and add the total to minutes. For reference this adds the amount of hours in days, and the amount of hours we had to begin with
Line 3: Similarly to the lines above, this takes minutes and converts / adds them to seconds.


Without compound statements the code would look something like this:


       days = days * 24;
       hours= hours + days;
       hours = hours *24;
       minutes = hours + minutes;
       minutes = minutes * 60;
       seconds = seconds + minutes;


Maximum combination
As you can see the above doesn’t do anything to help us out. There is another way we could have done this… but it’s a bit more contrived. With a little bit of our calculator we can learn that there are 86,400 seconds in a day, 3600 seconds in an hour, and 60 seconds in a minute. Thus we can get rid of everything but the println statement.


System.out.println("The number of seconds in " + days + " days " + hours + " hours " + minutes + " minutes and " 
+ seconds + " seconds is: " + ((days*86400)+(hours*3600)+(minutes*60)+seconds));

This sort of coding is technically proper, but can be confusing when you’re using more complex variables, so it’s generally avoided.


Using a total variable
Although I avoided using one here, there are times where it will be inappropriate to change the values of variables so we’ll declare a total variable to hold all of our variables. The code would to use a total variable in place of our existing variables would look like this:


       total += (days*86400);
       total += (hours*3600);
       total += (minutes*60);
       total += seconds;

You’ll notice that instead of using our existing variables we use total to hold the value of each converted variable.


Topics Discussed: If/Else logic and structure, if “conditionals”

Source Code Available Here

If statements
An if statement has two essential elements, the condition under which it will run (the conditional) and the payload that it will run when the condition is true. Let’s examine each part in a bit more depth:

If conditional: The if conditional is the part of the if statement where we tell the if under which conditions it will be operating. Here’s an example of an if conditional:


     if ( a ==7 ){
          //do stuff here
     }

In this case the contents of the brackets will be executed under the condition that the variable a is equal to 7. In the event that it’s not, nothing will happen here, however if you added the code:

     else{
          //do stuff
     }

You would have the option to add something to happen when a did NOT equal 9.


Boolean conditionals
The basic idea behind a statement evaluating to either ‘true’ or ‘false’ like the above conditions is known as ‘boolean’ logic. With that being said, there are things called ‘boolean operators’ that allow us to compare our variables to other constructs. The main boolean operators we will be using throughout these videos are as follows:


<     // Less Than
>     // Greater than
<=    // Less than or equal to
>=    // Greater than or equal to
!     // Not or is not
!=    // Is not equal to (very similar to above)
==    // is equal to
||    // Or
&&    // And
true  // will evaluate to true
false // will evaluate to false

Using the above statements we can make complex comparisons with relative ease, like this:

if (a < =6 && a > b || a < c && a > x){
     //do stuff
}
else{
     //do other stuff
}

In the next lesson we talk about boolean variables which always evaluate to either true or false. But what is true or false, and how to if statements actually resolve themselves?


If resolution
One of the more misunderstood areas of if statements is how they work internally, and the basic idea is something like this. If a statement evaluates to 0, then it is false, if a statement evaluates to 1, it is true. Let me show you an example where you can overlook this, then one where it’s in your face.


int a = 1;
if ( a ==1 ){ //this evaluates to 1, or true
     //do stuff
}

In this case, the contents being if (1) doesn’t really matter all that much. But in the following example, it might make sense why I’m teaching this at all:

int a =6, b=7, c=8;

if (c> b > a){ if 6 < 7< 8
     System.out.println("Expected output");
}
else{
     System.out.println("Unexpected output");
}

In this case the ‘unexpected output’ would appear on our screen, but why? Remember how a few lessons ago I mentioned that arithmetic evaluates from left – to – right in cases where items have the same precedence? That’s exactly what’s happening here. We are comparing c(8) to b(7) and seeing if c is > b. In this case c > bis true, so c > b becomes “1” (true). The remaining statement is 1 > a(where a is equal to 6), therefore the entire thing evaluates to 0, because 1 is less than 6. The proper way to write the above statement would be something like this:

if (c > b && c >a){
   //do stuff
}

*Note*: My apologies for the volume being somewhat screwed up, Starcraft II turned up microphone boost on its own. This gets fixed in a few videos.

Topics Discussed: The structure behind using nested if statements effectively and how easy it is to get them confused!

Source Code Available Here

Nested if structure
In this video I give you a fairly simple example as to why we should avoid nested if statements whenever we possibly can. The primary reason behind this is because they syntax is overwhelming, and it simply becomes very difficult to read / understand.

The syntax of a nested if is exactly the same as a normal if / else statement, except that it takes place in the ‘payload’ of an existing if / else statement.


There isn’t really too much to write up about this lesson, as it built off of what we covered in the lesson before now. Just watch your syntax and you should be able to grasp this almost immediately.

Apologies about this being a two-part video, AND for my mic volume being weird, in future videos this is fixed. I’ll consider re-recording this later


Topics Discussed: Using math functions with if-based logic to choose the output of a program

Source Code Available Here

8-2 Here


Topics discussed: Same as above

Source Code Available Here

In these videos I talk a little bit about java.lang.Math; An include that allows us to do some simple mathematical calculations in our code in conjunction with the if-statements we’ve been talking about.


java.lang.Math functions
There are a LOT of different constructs in the java.lang.Math library, but the ones we use in this lesson seem to be the most common of them. Raising a number to a power, and taking the square root of a number. The code for each of these is as follows: (assuming you have the import in your code)


Math.pow(numberOrVariable, powerRaisingTo);
Example:
Math.pow(x, 3); // raises x to the 3rd power.

Math.sqrt(x); //Takes the square root of x.


Topics Discussed: Boolean Logic / Operators

Source Code Available Here

HomeWork Available Here


Boolean operators
Boolean operators are variables which have two states, “true” or “false” (default is false if you use a simple declaration). These can be used in a variety of ways, but are most typically seen when you need to confirm that something happened in an if statement or a loop (in later lessons). Here’s an example:


int empPay1=0;
boolean isChanged;//ischanged defaults to false
System.out.println("Enter the income for employee 1 that is greater than 0.");
empPay1= input.nextInt(); // Enter pay

if (empPay1 > 0){// if the value is > 0, meaning it's been changed
     isChanged=true;
}

if(isChanged){//if the value has been changed (by the if above)
     System.out.println("empPay has been changed");
}
//You could just use an else here, but instead you could use the following:

if(!isChanged){
     System.out.println("empPay has not been changed");
}


In the above example I show that booleans can be used in place of the standard if conditionals and used as a simple true / false that is set as part of another conditional. At present these must seem convoluted, but in the future when our variables change around a bit more it will make more sense to use these than other pieces of code.