Variables are buckets that store data. Variable declarations looks like this:
<datatype> <variable name>; // Initializing <variable name>
<variable name> = <value>; // Assigning <variable name>// Is equivalant to:
<datatype> <variable name> = <value>; // Initializing and assigning <variable_name>
Here are some examples of variable declarations.
int myAge = 23; // <datatype> is int (integer)double pi = 3.14; // <datatype> is double (decimal)boolean programmingIsAwesome;
programmingIsAwesome = true; // <datatype> is boolean (true/false)char bestLetter = 'J'; // <datatype> is char (a letter)
Questions before practice?
Practice with variables! (5 minutes)
Declare a variable called firstLetter with the first letter of your name.
Declare a variable hadGoodSleep that is true if you had good sleep, and false otherwise.
Declare a variable minimumGPA that is the minimum GPA for your program.
Declare a variable mealsPerDay with the number of meals you eat per day.
Hint: You'll need to use int, double, boolean, and char only once in some order.
What can you do with variables? (Operations)
For integers int and decimals double:
Add, subtract, multiply, divide (+, -, *, /).
Add, subtract, multiply, divide and assign (+=, -=, *=, /=)
Increment, i.e. add one, or decrement, i.e. subtract one (++, --).
For booleans bool:
Negation, pronounced not in code.
boolean havingAGoodTime = true;
boolean notHavingAGoodTime = !havingAGoodTime; // Not having a good time.
There are more with String too, but this is what we'll deal with.
Questions before practice?
Practice with operations! (10 minutes)
Tracking the number of people in Zoom.
Declare a variable peopleInZoom that is equal to the number of people in this Zoom call.
On the next line, increment peopleInZoom by one using ++.
On the next line, add 7 to the variable peopleInZoom using +=.
Negation practice.
Declare a boolean called drankWater if you have drank water today.
Reassign drankWater with the opposite value of drankWater using !.
What are conditional expressions and statements?
Let's start off with expressions, conditional expressions are like questions that Java will evaluate and return true or false.
For == are the things on the left and right equal?
For > (>=) is the left value greater than the right value? Same goes with < and <= in the reverse.
Conditional expressions go into conditional statements (otherwise known as if statements).
We'll consider an example on the next page.
Consider below.
double mealCost = 3.75 + 8.66; // Another example of an operation.
mealCost *= 1.08; // Using the *= to multiple, and then assign back to mealCost.boolean mealIsExpensive = mealCost > 10.0; // Is this an expression or statement?if (mealIsExpensive) { // Is this an expression or statement?
System.out.println("I'll put that on my business card!");
}
// The above is the same as.if (mealCost > 10.0) {
System.out.println("I'll put that on my business card!");
}
Practice with conditional expressions and statements! (10 minutes)
Declare a variable myAge that contains your age.
Declare a variable called canBuyAlcohol that is true when myAge is greater or equal to 21 and false otherwise.
Write a conditional statement that prints "Get out of my store!" if canBuyAlcohol is false. Reminder that ! is the negation operator.
Add an else statement to the if statement that prints "What can I help you with?". When will this print?
Reminder
If-else statements look like.
if (<expression>) {
<code to execute if expression is true>;
} else {
<code to be execute if otherwise>;
}
Final, final questions about today's content? Anything!
if (!questions) {
System.out.println("Have an awesome day!");
System.out.println("Be sure to mark attendance if that matters, " +
"leave some feedback!");
}
char firstLetter = 'J'; // Note the single quotation.boolean hadGoodSleep = true; // Note that true and false are lowercased!double minimumGPA = 3.0;
int mealsPerDay = 3; // Okay, maybe more than that.
Practice with operations! (Solution)
Tracking the number of people in Zoom.
int peopleInZoom = 13;
peopleInZoom++;
peopleInZoom += 7; // Notice that I don't need int here again.
Negation practice.
boolean drankWater = true;
drankWater = !drankWater; // drankWater is now false.
Questions?
Practice with conditional expressions and statements! (Solution)
int myAge = 23;
boolean canBuyAlcohol = myAge >= 21;
if (!canBuyAlcohol) { // "If cannot buy alcohol."
System.out.println("Get out of my store!");
} else {
System.out.println("What can I help you with?");
}