180 likes | 301 Views
BIT 115: Introduction To Programming. Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah) bkweusijana@cascadia.edu http://edutek.net/cascadia/bit115. Quiz 11. https://catalysttools.washington.edu/webq/survey/babaw/55000. 2. Review of Quiz 10 Results.
E N D
BIT 115: Introduction To Programming • Professor: Dr. Baba Kofi Weusijana • (say Doc-tor Way-oo-see-jah-nah, • Doc-tor, or Bah-bah) • bkweusijana@cascadia.edu • http://edutek.net/cascadia/bit115
Quiz 11 • https://catalysttools.washington.edu/webq/survey/babaw/55000 2
Today • Finish polymorphism example • Predicate (Helper) methods • the return keyword • Nested while loops • Next class: • Non-robotic software • Modifiers (public, private, protected) • Debugging & unit testing • Programs stored in multiple files
Homework Assignments & Journals • J#3 due today • J#4 & A03 due Mon 5/19
Polymorphism benefit • Can refer to the superclass as the type of an object of an extended class • Change the type of joe in ICE_10_Demo_1.java from SpinningRobot to Robot but don’t change new SpinningRobot(...) • This will be very helpful when we start putting objects into other objects such as arrays and collections 6
Nested Loops • Suppose we want to place Things in a row, we can do that with a while loop and a row integer variable • Notice : only 1 dimensional output • What if you wanted to also place Things across columns (2D output)? • inside the row while loop, have a column while loop • We call this nesting • you can nest loops, if statements, select cases, etc. 7
ICE Part 2: Nested Loops • With Eclipse set breakpoint on line 12 of NestedWhile.java • Trace the code in the debugger and watch the changes • If we wanted to, we could nest 3 loops inside each other, etc 8
Queries, Predicates, Integers in tests • A query method is a question asking method • Used to figure out something about the current state of the program • Queries SHOULDN'T have side effects: shouldn’t change the state of the object • for example, when a robots’ query method ends, the robot should be in the same situation it was when the method was called • Example: public int countThingsInBackpack() in the becker.robots.Robot class 9
Predicates • Predicate methods are true/false (yes/no) queries • Return a boolean value (true or false) • Usually have “is” in their name • For example in becker.robots.Robot: • frontIsClear • isBesideThing 10
Return • return can be used to indicate what answer the method wants to give • It will immediately stop the method from going any further, and instead return to whatever method called the current method • Example: • public boolean isTrue() • { • return true; • } 11
Return • Normally, you'd combine return with some other logic to create a more useful bit of functionality: • public boolean isRobotOnAvenueFive() • { • if (this.getAvenue() == 5) • { • return true; • } • else • { • return false; • } • } 12
Return • You can also directly return anything that will be true/false when the program is run: • public boolean isRobotOnAvenueFive() • { • return (this.getAvenue() == 5); • } • Return can also be used in void methods to indicate that you simply want to stop the method & return to the caller • public void doNothing() • { • return; • System.out.println(“Never say this.”); • } 13
Using return just to stop a method • /** This will put down as many as five things, in a row. The method will stop if the robot doesn't have 5 Things to place */ • public void placeFiveThings() • { • int counter = 0; • while(counter < 5) • { • if(this.countThingsinBackPack() > 0) • { • this.putThing(); • } • else • { • // This will simply stop the current method from running • return; • } • } • } 14
ICE Part 1, with the sample test code • FileName.java 15
Integer tests • These queries return int numbers: • int getAvenue(); • int getStreet(); • Direction getDirection(); //one of Directions.NORTH, Directions.SOUTH, etc • int getSpeed(); • Note that these are all public methods • Integer tests are not very useful unless we can compare them to other things & do some simple math on them 16
ICE Part 3: Variable Design • Discuss: • What are good answers? • TYPO: • Students DO NOT need to turn in a MS Word doc, but good quiz practice 17
ICE Parts 4 & 5 • Note: You must fix class naming problem by making packages • Can turn in as late as by class on Monday 5/12 to the Java Code Critic 18