Lecture 13 PowerPoint

advertisement
Lecture 9a
Instructor: Craig Duckett
Mirror Site
Occasionally the Cascadia servers will “go down”
making it impossible to access my faculty course
websites. Knowing this, I have “mirrored” a backup of
my BIT websites at:
programajama.com/courses
Assignment 2 Revision
DUE NEXT Monday, August 8th
Assignment 3
DUE NEXT Wednesday, August 10th
Assignment 3 Revision
DUE Wednesday, August 17th
Assignment 4
DUE Wednesday, August 24th (NO REVISION)
Assignment Dates (By Due Date)
• Assignment 2 Revision (LECTURE 10)
Monday, August
8th
The Fickle
Finger of Fate
• Assignment 3 (LECTURE 11)
Wednesday, August 10th
• Assignment 3 Revision (LECTURE 13)
Wednesday, August 17th
• Assignment 4 (LECTURE 15)
Wednesday, August 24th
• EXTRA CREDIT (LECTURE 25)
Wednesday, August 24th
4
And Now...
... No Quiz
Instead …
Mid-Term Exam: Post Mortem
Best Class Yet! Class Average was 130 (87%)!
Lecture 9 Announcements CONTINUED
Today
“A little bit of this, a little bit of that…”
•
•
•
•
•
Boolean Expressions and Logical Operators && , || , !
Our First Look at Non-Robotic Code (Straight Java, No Chaser)
A Word About Static
Static Methods vs Public Methods
Return Values: What Good Are They and How Do They Work?
The World Wide Web is your GREATEST
Programming Ally!
“Google it!”
Boolean Expressions and Logical Operators
A Boolean Expression is an expression
that results in a Boolean value, that is, in
a value that is either true or false.
More complex Boolean expressions can
be built out of simpler expressions, using
the Boolean Logical Operators.
Boolean Expressions and Comparison Operators
Operator
Operation
<
Less than
<=
Less than or equal
>
Greater than
>=
Greater than or equal
==
Equal
!=
Not equal
Using Comparison Operators, the test in if and while statements are Boolean expressions
that give a true or false answer to a question. So far, our questions have been simple. As
our programming skills grow, however, we will want to ask more complex questions,
which will need more complex Boolean expressions to answer.
Just like the mathematic operators (+ - / *) can be combined to form more complex
expressions like
s = (( x + y) * (w – z)) / 2
so too can Boolean expressions be combined to create more complex and utility
expressions using and (represented by &&) and or (represented by ||).
Logical Operators: && (“And”) and II (“Or”)
In many programming languages — like Java, JavaScript, C++, and C# — the logical operators
‘and’ and ‘or’ are represented in the code by a double ampersand && and double pipe ||
“And”
“Or”
&&
||
Double Ampersand
Double Pipe
There is also the ‘not’ operator represented by a single exclamation point ! which I’ll talk
about in a moment.
“Not”
!
Single Exclamation Point
NOTE: Java also has a single & and single | characters, called bitwise operators, but for the
purposes of this course we won’t be discussing them. These will come a bit later (pun
intended) along your journey in learning programming languages.
Before We Continue: What Pipe Character ?
Where is the pipe character | located on the keyboard?
AN INTERESTING NOTE: The “pipe” character goes by several different names,
depending on the particular group using it: besides being called the pipe, it can also be
called the pipeline, vertical bar, Sheffer stroke, polon, verti-bar, vbar, stick, vertical line,
vertical slash, bar, or glidus.
Logical Operators
Logical Operators
AND
A && B are true only if both A and B are true
OR
A || B are true if A is true or B is true or they're both true
&&
NOT
!A is the opposite of A.
If A is true, then !A is false.
If A is false, !A is true.
!
||
The double ampersand && and double pipe || are called “short circuit” logical operators
AND Operator
OR Operator
TRUE
&& TRUE
TRUE
TRUE
&&
FALSE
FALSE
TRUE ||
FALSE
TRUE
FALSE
&&
TRUE
FALSE
FALSE ||
TRUE
TRUE
|| FALSE
FALSE
TRUE
FALSE
|| TRUE
TRUE
Logical Operator: Examples
AND
int a = 7;
int b = 10;
if( a > 4 && b < 20 )
{
// This will be true since both operands of the && operator evaluate to true
}
OR
int c = 10;
int d = 40;
if( c == 7 || d > c )
{
// This will be true. Even though the first operand evaluates to false,
// the second will evaluate to true.
}
NOT
int e = 7;
int f = 10;
if( !(e == f) )
{
// This will evaluate to true since e == f will be false,
// and the NOT operator will reverse it
}
Example: LogicalOperatorsExample.java
Logical Operator: Gotchas
int x = 5;
if( 0 < x < 11) // Testing to see if x is in range of 1 to 10
{
System.out.println("X is in range");
}
int x = 5;
if( x > 0 && < 11) // Testing to see if x is in range of 1 to 10
{
System.out.println("X is in range");
}
Logical Operator: Gotchas
int x = 5;
if( x > 0 && x < 11) // Testing to see if x is in range of 1 to 10
{
System.out.println("X is in range");
}
So, remember: you need whatever you are comparing—whether
variables or methods— listed on both sides of the logical operators.
if( getStreet() > 0 && getStreet() < 11)
Logical Operator: Robotic Examples
if(this.countThingsInBackpack() > 0 && this.frontIsClear())
{
this.putThing();
this.move();
}
// Notice the use of the wrapping parenthesis
if((this.isFacingEast() || this.isFacingWest()) && this.frontIsClear())
{
this.move();
}
if( !(this.frontIsClear()) )
{
this.turnLeft();
}
Example: LogicalOperatorRobot.java
“Non-Robotic” Java Programming
Writing Java programs without using the
Becker Robot class or any robots in the
code is now deemed “non-robotic.”
Start Looking for “Non-Robotic Java”*
* Of course any new topics we learn under the heading ‘Non-Robotic Java’ can still be used when working with
Robots, like Return Values and Static Methods, as we will see…
Straight Java, No Chaser
Starting today, we will begin
introducing “Non-Robotic Java”
into the mix, which will find its
way into the PowerPoint slides,
some of the example code,
several of the ICEs, and even
one of the Assignments,
Assignment 4 (Advanced).
Example: LogicalOperatorsExample.java
The Return Statement and Return Values
Return Values
An Illustrated Introduction to Return Values
First, a look at non-return…
Up to now we’ve seen how to use void methods…
public
{
void
moveMultiple(int numberOfIntersections)
int counter = 0;
while( counter < numberOfIntersections)
{
this.move();
counter = counter + 1;
}
}
rob.moveMultiple(5);
You can pass an
argument to a void
method and it will do
whatever it is meant to
do, but nothing is
returned as a separate
value that the program
might use later on.
Void: Means “Nothing is Returned”
The meaning of “void”
5
Up to now we’ve seen how to use void methods.
public
{
void
moveMultiple(int numberOfIntersections)
moveMultiple
int counter = 0;
while( counter < numberOfIntersections)
{
this.move();
counter = counter + 1;
}
}
…and down in main:
rob.moveMultiple(5);
void
means that nothing is
returned back to the program;
the method does something, it
just doesn’t return something
back into the program once it’s
finished doing whatever it is it’s
been doing. In other words, it is a
closed method.
5
Two Types of “void”
Method 1
Method 2
Nothing goes in,
nothing comes out
Something goes in,
nothing comes out
move();
moveMultiple(5);
Return: By Way of
A Cooking Eggs Analogy
The overEasy() method does exactly what it is supposed to do—it cooks the egg
over easy, but that’s as far as it goes…
customer.overEasy()
Alas, nothing is returned.
Hungry customer not happy.
kitchen class
diningRoom class
A Sad Scenario
WTF? *
* Where’s the food?
Cooking Eggs Analogy
egg.sunnySideUp()
egg.overEasy()
egg.overMedium()
egg.overHard()
egg.scrambled()
egg.poached()
egg.hardboiled()
egg.softBoiled()
plate = customer.overEasy()
Hooray, overEasy() returns the cooked egg and puts it in the plate!
Plate is now used to transport egg to happy customer!
A Happy Scenario
Breakfast is
served!
Return: The Return Type Must Be Specified
int will be returned
public
int
countMoves( )
int will be returned
chugga-chugga
public
int
addSum(int num)
true or false
will be returned
chugga-chugga
return counter;
public
int
boolean
isNorth(int num)
return sum;
chugga-chugga
int
The Return Value is a way to send the
information back into the program so
the program can use it in another way.
return true;
true
3
Two Types of “Return”
Method 1
Nothing goes in,
something comes out
FileName.java
Method 2
Something goes in,
something comes out
FileName2.java
We’ll visit these two code examples after the next slide…
Example: Nothing Goes In, Something Comes Out
// Example of a method returning a value
////////////////////////////////////////
class returnIt extends Object
{
public int returnYear()
{
int year = 2016;
return year;
}
}
public class methodReturn extends Object
{
public static void main(String[] args)
{
returnIt bernie = new returnIt();
int num;
num = bernie.returnYear();
System.out.println(num);
}
}
methodReturn.java
Example: Nothing Goes In, Something Comes Out
// Example of a method returning a value
////////////////////////////////////////
class returnIt extends Object
{
public int returnYear()
{
int year = 2016;
return year;
}

}
public class methodReturn extends Object
{
public static void main(String[] args)
{
returnIt bernie = new returnIt();
int num;

}
}

System.out.println(num); 
num = bernie.returnYear();
Anatomy of a Method Return
1) The returnYear() method
is called by the bernie
object
2) The returnYear() method
returns the value of year
3) Value of year shoots of
out the returnYear()
method and deposited in
the num variable
container
4) The value of num is
printed out.
Example: Something Goes In, Something Comes Out
// EXAMPLE: A number goes in, a revised number comes out
class SumNum extends Object
{
public int Tally(int num)
{
int sum = 0;
sum = num + 5;
return sum;
}
}
public class NumberTest extends Object
{
public static void main(String[] args)
{
SumNum sn = new SumNum();
int number;
number = sn.Tally(3);
System.out.println(number);
}
}
NumberTest.java
Walkthrough: FileName.java
class PrintHelper extends Object
{
public int printNum()
{
System.out.println("Going to print, some number of times!");
int howManyPrints = 0;
while(howManyPrints < 2)
{
System.out.println("Printing!");
howManyPrints++; // This is a basic counter
}
return howManyPrints;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper Gutenberg = new PrintHelper();
int num;
num = Gutenberg.printNum(); // This method is called by an object
System.out.println( "The method printed " + num + " times!");
}
}
Class
class PrintHelper extends Object
“Idea / Attributes”
{
public int printNum()
{
System.out.println("Going to print, some number of times!");
int howManyPrints = 0;
while(howManyPrints < 2)
{
System.out.println("Printing!");
howManyPrints++; // This is a basic counter
}
return howManyPrints;
}
}
Class
public class FileName extends Object
{
public static void main(String[] args)
{
Object (Instance of Class)
PrintHelper Gutenberg = new PrintHelper();
int num;
num = Gutenberg.printNum();
System.out.println( "The method printed " + num + " times!");
}
}
class PrintHelper extends Object
{
public int printNum()
{
System.out.println("Going to print, some number of times!");
int howManyPrints = 0;
while(howManyPrints < 2)
{
System.out.println("Printing!");
howManyPrints++; // This is a basic counter
}
return howManyPrints;
}
howManyPrints
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper Gutenberg = new PrintHelper();
int num = 0;
num
num = Gutenberg.printNum();
System.out.println( "The method printed " + num + " times!");
}
}
class PrintHelper extends Object
{
public int printNum()
{
System.out.println("Going to print, some number of times!");
int howManyPrints = 0;
while(howManyPrints < 2)
{
System.out.println("Printing!");
howManyPrints++; // This is a basic counter
}
return howManyPrints;
}
}
howManyPrints
2
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper Gutenberg = new PrintHelper();
int num;
num = Gutenberg.printNum();
2
2
num
howManyPrints
System.out.println( "The method printed " + num + " times!");
}
}
2
Example Code: Return
Let’s look at some more examples using return…
•
•
•
•
NumberTest.java
SumOfTwoNumbers.java
AverageOfTwoNumbers.java
RetrunValues_Demo.java
Assignment 3: Maze Reminder
This is what you turn in
This is what I add to grade it
•
I will also be testing with only 25
things in the backpack to start to
make sure the Robot successfully
completes the Maze even if the
backpack is runs out of Things to
put down.
LECTURE 9:
ICE Part 1:
39
Download