1 / 29

BIT115: Introduction to Programming

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

bran
Download Presentation

BIT115: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT115: Introduction to Programming Instructor: Craig Duckett Lecture 9 CALL ME

  2. Assignment2 Revision DUE Monday, August 4th Assignment3 DUE Wednesday, August 6th Assignment3 Revision DUE Wednesday, August 13th

  3. 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?

  4. And now... ... The Quiz

  5. Boolean Expressions and Logical Operators The test in if and whilestatements are Boolean expressions that give a yes or no, 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 the andand the oroperators, using && and ||

  6. Where is the pipe character | located on the keyboard?

  7. Logical Operators The double ampersand && and double pipe || are called short circuit logical operators ANDOperator OROperator && TRUE TRUE TRUE TRUE TRUE TRUE || && TRUE FALSE FALSE TRUE FALSE TRUE || && FALSE TRUE FALSE FALSE TRUE TRUE || || FALSE FALSEFALSE

  8. 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 a = 10; int b = 40; if( a == 7 || b > a ) { // This will be true. Even though the first operand evaluates to false, // the second will evaluate to true. } int a = 7; int b = 10; if( !(a == b) ) { // This will evaluate to true since a == b will be false, // and the NOT operator will reverse it }

  9. Non-Robotic Java Straight Java, No Chaser

  10. Non-Robotic Java An Illustrated Introduction to Return Values Up to now we’ve seen how to use void parameters… publicvoid moveMultiple(intnumberOfIntersections) { intcounter = 0; while( counter < numberOfIntersections) { this.move(); counter = counter + 1; } } 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. rob.moveMultiple(5);

  11. Non-Robotic Java The meaning of “void” Up to now we’ve seen how to use void parameters. 5 ( ) publicvoidmoveMultiple(intnumberOfIntersections) { intcounter = 0; while( counter < numberOfIntersections) { this.move(); counter = counter + 1; } } moveMultiple 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. …and down in main: rob.moveMultiple(5);

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

  13. Cooking Eggs Analogy egg.overEasy() Alas, nothing is returned. Hungry customer not happy.

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

  15. egg.sunnySideUp() egg.overEasy() egg.overMedium() egg.overHard() egg.scrambled() egg.poached() egg.hardboiled() egg.softBoiled() Cooking Eggs Analogy plate = egg.overEasy() Hooray, overEasy is returned and put in the plate! Plate is now used to transport egg to happy customer!

  16. A Happy Scenario Breakfast is served!

  17. 3 Two Types of “Return” Method 1 Method 2 Nothing goes in, something comes out Something goes in, something comes out moveAndComment(); moveMultiple(3);

  18. Return Values int will be returned public intcountMoves( ) int will be returned chugga-chugga public intaddSum(intnum) true or falsewill be returned chugga-chugga return counter; public booleanisNorth(intnum) return sum; int chugga-chugga int return true; The Return Value is a way to send the information back into the program so the program can use it in another way. true

  19. Cooking Eggs Analogy … Again plate = egg.overEasy(1) plate = egg.overEasy(2) plate = egg.overEasy(3)

  20. A Word About Static No, not these kinds of static

  21. publicclassFileName4extends Object{publicstaticvoid main(String[] args){System.out.println( “I printed!");}} Class … but no Object !What the hay? 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…

  22. This will not compile (there is no object to do the action) publicclassStaticDemo{intmy_member_variable = 99999; publicstaticvoid main (String args[]) {// Access a non-static member from static methodSystem.out.println("This generates a compiler error :-( " + my_member_variable); }} This will compile (there is an object called demo to do the action) publicclassNonStaticDemo{intmy_member_variable = 99999;publicstaticvoid main (String args[]) {NonStaticDemo demo = newNonStaticDemo();// Access member variable of demoSystem.out.println ("This WON'T generate an error! --> " + demo.my_member_variable); }} See Examples: StaticDemo1.java – StaticDemo2.java – StaticDemo3.java

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

  24. publicclassFileName3extends Object{publicstatic intprintNum() {System.out.println("Going to print, some number of times!");inthowManyPrints = 0;while(howManyPrints < 2) {System.out.println("Printing!");howManyPrints++; // This is a basic counter }returnhowManyPrints; }publicstatic void main(String[] args) {intnum = 0;num= printNum(); // <-- Notice this method call has no objectSystem.out.println( "The method printed " + num + " times!"); } } Class Method Method is called in main and a value is returned and put into num

  25. Public Method A public methoddoes need an object to call it. It can notcall itself! Therefore, in order to use a public method down in main you need to createan instance of an objectfrom the classthat contains the method. I’m a Method! Glad you Called! I’m an Object!

  26. classPrintHelperextends Object{publicintprintNum() {System.out.println("Going to print, some number of times!");inthowManyPrints = 0;while(howManyPrints< 2) {System.out.println("Printing!");howManyPrints++; // This is a basic counter }returnhowManyPrints; }} publicclassFileNameextends Object{publicstaticvoid main(String[] args) {PrintHelperGutenberg= newPrintHelper();intnum;num = Gutenberg.printNum(); // This method is called by an objectSystem.out.println( "The method printed " + num + " times!"); }}

  27. Class “Idea / Attributes” classPrintHelperextends Object{publicintprintNum() {System.out.println("Going to print, some number of times!");inthowManyPrints = 0;while(howManyPrints< 2) {System.out.println("Printing!");howManyPrints++; // This is a basic counter }returnhowManyPrints; }} publicclassFileNameextends Object{publicstaticvoid main(String[] args) {PrintHelperGutenberg= newPrintHelper();intnum;num = Gutenberg.printNum();System.out.println( "The method printed " + num + " times!"); }} Class Object (Instance of Class)

  28. classPrintHelperextends Object{publicintprintNum() {System.out.println("Going to print, some number of times!");inthowManyPrints = 0;while(howManyPrints< 2) {System.out.println("Printing!");howManyPrints++; // This is a basic counter }returnhowManyPrints; }}publicclassFileNameextends Object{publicstaticvoid main(String[] args) {PrintHelper Gutenberg= newPrintHelper();intnum = 0;num = Gutenberg.printNum();System.out.println( "The method printed " + num + " times!"); }} howManyParts num

  29. classPrintHelperextends Object{publicintprintNum() {System.out.println("Going to print, some number of times!");inthowManyPrints = 0;while(howManyPrints< 2) {System.out.println("Printing!");howManyPrints++; // This is a basic counter }returnhowManyPrints; }} 2 howManyParts publicclassFileNameextends Object{publicstaticvoid main(String[] args) {PrintHelper Gutenberg= newPrintHelper();intnum; num = Gutenberg.printNum(); System.out.println( "The method printed " + num + " times!"); }} 2 2 howManyParts num 2

More Related