Part Zero - Review
Write a program that calculates the number of pizzas to order for 16 people if each person will have 3 slices (0.375 pizzas) each. Try to write out the main method yourself but if you forgot just click here
Your output should look like this:
Can I have ""number of pies"" to Hancock 0005?
Hint: use Math.ceil() to round up to the nearest whole pizza.
Part One - Boolean Logic, AND, OR and NOT
AND | OR | NOT | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
You should be fairly familiar with the following truth tables from high school.
In java we have operators for each of these boolean expressions.
We also have a type just for holding true and false values.
The type is boolean and the operators are:
AND | && |
OR | || |
NOT | ! |
Let's make a sample program to use boolean and our boolean operators...
boolean x = true; boolean y = false; boolean z = x || y; System.out.println(z);
Part Two
Writing true and false aren't the only way to get boolean values. We can also compare ints doubles and Strings.
System.out.println( 2 == 2 ); // equals System.out.println( 2 != 2 ); // not equal System.out.println( 2 < 3 ); // less than System.out.println( 3 > 2 ); // greater than System.out.println( 2 <= 3 ); // less than or equal to System.out.println( 3 >= 2 ); // greater than or equal to String x = "yes"; System.out.println( x == "yes" ); //incorect way to compare strings System.out.println( x.equals("yes") ); //correct way to compare strings
Part Three
The if is one of the most important controll structures in programming. It lets the computer make decisions based on a boolean statement.
boolean hasBrain = true; if ( hasBrain ){ System.out.println("would while away the hours conversin\' with the flowers"); }
Part Four
You can also provide something for the computer to do if it fails the test
String storedPassword = "password123"; String inputPassword = "password123"; if ( storedPassword.equals(inputPassword) ){ System.out.println("You are logged in"); } else { System.out.println("Sorry incorrect poassword"); }
Part Four
If you need to test more than one condition use else if
double bodyTemperature = 98.6; if ( bodyTemperature > 98.6 ){ System.out.println("You have a fever"); } else if ( bodyTemperature < 98.6 ) { System.out.println("You have a cold"); } else { System.out.println("You are healthy"); }
Home Work
Make a program that tells you what score you got on a hole in golf.
Use two variables, one for par and one for your score on the hole.
If your score is equal to par print : You got par!
If your score is one less than par print : You got a birdie!
If your score is one more than par print : You got a bogey!
Otherwise print : You got a ""your score""