Lecture 9a Instructor: Craig Duckett CALL ME. Assignment 2 Revision DUE THIS Wednesday, August 5 th Assignment 3 DUE Monday, August 10 th Assignment 3.

Slides:



Advertisements
Similar presentations
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Advertisements

Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
Lecture 13 Instructor: Craig Duckett CALL ME. Assignment 3 DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of a team.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture 14/15 Instructor: Craig Duckett. 2 ANNOUNCEMENTS For various reasons, the Cascadia Faculty web server occasionally goes down, making it impossible.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
BIT115: Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Content Programming Overview The JVM A brief look at Structure
BIT 115: Introduction To Programming
Building Java Programs
Introduction to Computer Science / Procedural – 67130
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Debugging and Random Numbers
Primitive Data, Variables, Loops (Maybe)
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
BIT115: Introduction to Programming
Lesson 2: Building Blocks of Programming
Building Java Programs Chapter 2
Conditions and Ifs BIS1523 – Lecture 8.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Review of Previous Lesson
Building Java Programs
LCC 6310 Computation as an Expressive Medium
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 9a Instructor: Craig Duckett CALL ME

Assignment 2 Revision DUE THIS Wednesday, August 5 th Assignment 3 DUE Monday, August 10 th Assignment 3 Revision DUE Monday, August 17 th DUE Monday, August 24 th (NO REVISION) Assignment 4

Assignment 1 Assignment 1 GRADED Assignment 2 Assignment 2 GRADED Assignment 1 Revision Assignment 1 Revision GRADED Assignment 2 Revision Assignment 2 Revision DUE Lecture 10, Wednesday, August 5 th, by midnight Assignment 3 Assignment 3 DUE Lecture 11, Monday, August 10 th, by midnight Assignment 3 Revision Assignment 3 Revision DUE Lecture 13, Monday, August 17 th, by midnight Assignment 4 Assignment 4 DUE Lecture 15, Monday, August 24 th, by midnight NO REVISION AVAILABLE Extra Credit 01 Extra Credit 01 DUE Lecture 15, Wednesday, August 26 th, by midnight 3 Assignment Announcements

And Now No Quiz Instead …

Mid-Term Exam: Post Mortem Best Class Yet! Class Average was 125 (84%)!

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?

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 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. OperatorOperation < 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 ||).

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.

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.

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. ! && || 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

Logical Operator: Examples int 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 }

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(); } 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”* 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).

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” 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”

Two Types of “void” Nothing goes in, nothing comes out Something goes in, nothing comes out move(); moveMultiple(5); 5 Method 1 Method 2

Cooking Eggs Analogy Return: By Way of A 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 class diningRoom class

WTF? * * Where’s the food? A Sad Scenario

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()

A Happy Scenario Breakfast is served!

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 true int will be returned true or false will be returned chugga-chugga

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”

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

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

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

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 howManyPrints 2

Example Code: Return Let’s look at some more examples using return… NumberTest.java ReturnValues_Demo.java ReturnValues_Demo2.java

A Word About Static No, not these kinds of static

public class FileName4 extends Object { public static void main(String[] args) { System.out.println( “I printed!"); } } Class Main is going to run whether it contains an instance (object) of a class or not. It is declared static for just such this reason—static means that main doesn’t need an instance (object) of the class that contains it to be created in order for main to run, because main is set up to be its own instance. In this way, main acts like the starter on a car, it doesn’t need a starter to start the starter…it is the starter. Interesting note: you can compile your Java program without a main method, you just can’t run it! (just like you can build a car without a starter, but you can’t start it without the starter). Now, if you are wanting to use other classes (their “ideas” and “attributes”) down in main, then you do need to create an instance of those classes ( a named object) that can actually do the something (whatever that something is) that you want done. For instance (pun intended) when we are using the Becker classes, then we need to create a named instance of the Robot class (e.g., a Robot object called “Karel” OR “Jo” or “Mary”) if we want to see any activities and actions done (move, pickThing, putThing, etc). Without the Object doing the actions, then they will only remain ideas… Main is going to run whether it contains an instance (object) of a class or not. It is declared static for just such this reason—static means that main doesn’t need an instance (object) of the class that contains it to be created in order for main to run, because main is set up to be its own instance. In this way, main acts like the starter on a car, it doesn’t need a starter to start the starter…it is the starter. Interesting note: you can compile your Java program without a main method, you just can’t run it! (just like you can build a car without a starter, but you can’t start it without the starter). Now, if you are wanting to use other classes (their “ideas” and “attributes”) down in main, then you do need to create an instance of those classes ( a named object) that can actually do the something (whatever that something is) that you want done. For instance (pun intended) when we are using the Becker classes, then we need to create a named instance of the Robot class (e.g., a Robot object called “Karel” OR “Jo” or “Mary”) if we want to see any activities and actions done (move, pickThing, putThing, etc). Without the Object doing the actions, then they will only remain ideas… … but no Object ! What the hay?

public class StaticDemo { int my_member_variable = 99999; public static void main (String args[]) { // Access a non-static member from static method System.out.println ("This generates a compiler error :-( " + my_member_variable); } } This will not compile (there is no object to do the action) public class NonStaticDemo { int my_member_variable = 99999; public static void main (String args[]) { NonStaticDemo demo = new NonStaticDemo(); // Access member variable of demo System.out.println ("This WON'T generate an error! --> " + demo.my_member_variable ); } } This will compile (there is an object called demo to do the action) See Examples: StaticDemo1.java – StaticDemo2.java – StaticDemo3.java

Static Method A static method does not need an object to call it. It can call itself ! You there? I’m here!

public class FileName3 extends Object { public static 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 static void main(String[] args) { int num = 0; num = printNum(); // <-- Notice this method call has no object System.out.println( "The method printed " + num + " times!"); } } Class Method Method is called in main and a value is returned and put into num

Public Method A public method does need an object to call it. It can not call itself! Therefore, in order to use a public method down in main you need to create an instance of an object from the class that contains the method. I’m an Object! I’m a Method! Glad you Called!

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 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

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!"); } } howManyParts num

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!"); } } howManyParts num 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; } } howManyParts 2

43 LECTURE 9: ICE Part 1: