bit115: introduction to programming
DESCRIPTION
BIT115: Introduction to Programming. Instructor: Craig Duckett. Lecture 9. CALL ME. Assignment 2 Revision. DUE Monday, August 4 th. Assignment 3. DUE Wednesday, August 6 th. Assignment 3 Revision. DUE Wednesday, August 13 th. Lecture 9 Announcements CONTINUED. Today - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/1.jpg)
BIT115: Introduction to ProgrammingLecture 13
Instructor: Craig Duckett
![Page 2: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/2.jpg)
Assignment 3
DUE TONIGHT!Uploaded to StudentTracker by midnightIf you are submitting as part of a team make sure that all team member names are on both the .java program and .doc summary files. Also only one member of the team has to submit, but I will return the graded assignment to all members of the team.
Assignment 3 RevisionDUE Wednesday, March 2nd
![Page 3: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/3.jpg)
3
Assignment 2 Revision GRADED! RETURNED!
Assignment 3 (LECTURE 13) DUE TONIGHTMonday, February 22
Assignment 3 Revision (LECTURE 16) Wednesday, March 2
Assignment 4 (LECTURE 19) Monday, March 14 NO REVISION
Extra Credit 01 (LECTURE 20) Wednesday, March 16
Assignment Dates (By Due Date)
![Page 4: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/4.jpg)
Today’s Topics• Boolean Expressions and Logical Operators
&& , || , !• Our First Look at Non-Robotic Code
(Straight Java, No Chaser)• Return Values: What Good Are They and How
Do They Work?
![Page 5: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/5.jpg)
And Now:... The “Warm-Up” Quiz
It’s Easy-Peasy!
![Page 6: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/6.jpg)
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.
Topic:
![Page 7: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/7.jpg)
Boolean Expressions and Comparison Operators
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.
Operator Operation< Less than<= Less than or equal> Greater than>= Greater than or equal== Equal!= Not equal
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 ||).
![Page 8: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/8.jpg)
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” && Double Ampersand
“Or” || 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
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.
![Page 9: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/9.jpg)
Where is the pipe character | located on the keyboard?
Before We Continue: What Pipe Character ?
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.
![Page 10: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/10.jpg)
Logical OperatorsLogical Operators AND A && B are true only if both A and B are true &&ORA || 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.
!
&&
&&
&&
||
||
||
||
TRUE TRUE TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE FALSE TRUE
FALSE TRUE FALSE FALSE TRUE TRUE
FALSE FALSE FALSE
AND Operator OR Operator
The double ampersand && and double pipe || are called “short circuit” logical operators
![Page 11: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/11.jpg)
Logical Operator: Examplesint a = 7;int b = 10;
if( a > 4 && b < 20 ) { // This will be true since both operands to the && operator will evaluate to true}
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.}
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}
![Page 12: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/12.jpg)
Logical Operator: Gotchasint 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");}
![Page 13: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/13.jpg)
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)
![Page 14: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/14.jpg)
Logical Operator: Robotic Examplesif(this.countThingsInBackpack() > 0 && this.frontIsClear()) {
this.putThing();this.move();
}
if((this.isFacingEast() || this.isFacingWest()) && this.frontIsClear()){
this.move();}
if( !(this.frontIsClear()) ) {
this.turnLeft();}
Example: LogicalOperatorRobot.java
![Page 15: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/15.jpg)
“Non-Robotic” Java Programming
Writing Java programs without using the Becker Robot class or any robots in the code is now deemed “non-robotic.”
Topic:
![Page 16: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/16.jpg)
Start Looking for “Non-Robotic Java”*
Straight Java, No Chaser
* 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…
Example: LogicalOperatorsExample.java
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).
![Page 17: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/17.jpg)
The Return Statement and Return Values
Topic:
![Page 18: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/18.jpg)
Return ValuesAn Illustrated Introduction to Return ValuesFirst, 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.
![Page 19: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/19.jpg)
Void: Means “Nothing is Returned”
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;
} } 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.
moveMultiple
5
…and down in main:
rob.moveMultiple(5);
The meaning of “void”
![Page 20: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/20.jpg)
Two Types of “void”
Nothing goes in,nothing comes out
Something goes in,nothing comes out
move(); moveMultiple(5);
5
Method 1 Method 2
![Page 21: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/21.jpg)
Return: By Way ofA Cooking Eggs Analogy
customer.overEasy()Alas, nothing is returned.
Hungry customer not happy.
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…
kitchen classdiningRoom class
![Page 22: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/22.jpg)
WTF? *
* Where’s the food?
A Sad Scenario
![Page 23: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/23.jpg)
Cooking Eggs Analogy
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!
egg.sunnySideUp()egg.overEasy()egg.overMedium()egg.overHard()egg.scrambled()egg.poached()egg.hardboiled()egg.softBoiled()
![Page 24: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/24.jpg)
A Happy ScenarioBreakfast is
served!
![Page 25: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/25.jpg)
Return: The Return Type Must Be Specified
The Return Value is a way to send the information back into the program so the program can use it in another way.
public int addSum(int num)
public int countMoves( )
return counter;
return sum;
public boolean isNorth(int num)
return true;
int
int
true
int will be returned
int will be returned
true or false will be returned
chugga-chugga
chugga-chugga
chugga-chugga
![Page 26: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/26.jpg)
Nothing goes in,something comes out
Something goes in,something comes out
FileName.java FileName2.java
3
Method 1 Method 2
Two Types of “Return”
![Page 27: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/27.jpg)
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!"); }}
Walkthrough: FileName.java
![Page 28: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/28.jpg)
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(); System.out.println( "The method printed " + num + " times!"); }}
Class“Idea / Attributes”
Object (Instance of Class)
Class
![Page 29: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/29.jpg)
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 = 0; num = Gutenberg.printNum();
System.out.println( "The method printed " + num + " times!"); }}
howManyPrints
num
![Page 30: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/30.jpg)
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();
System.out.println( "The method printed " + num + " times!"); }}
howManyPrints
num
2
2
2
howManyPrints
2
![Page 31: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/31.jpg)
Example Code: ReturnLet’s look at some more examples using return…
• NumberTest.java• ReturnValues_Demo.java• ReturnValues_Demo2.java
![Page 32: BIT115: Introduction to Programming](https://reader035.vdocument.in/reader035/viewer/2022062521/56816690550346895dda67d4/html5/thumbnails/32.jpg)
ICE 14: Logical Operators and Boolean Returns
Before starting today’s ICE, you may want to look at the Boolean return examples:
• BooleanReturns1.java• BooleanReturns2.java• BooleanReturns3.java