bit 115: introduction to programming professor: dr. baba kofi weusijana (say doc-tor...

18
BIT 115: Introduction To Programming BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah) [email protected] http://edutek.net/cascadia/bit115

Upload: russell-mitchell

Post on 14-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

BIT 115: Introduction To Programming

BIT 115: Introduction To Programming

Professor: Dr. Baba Kofi Weusijana

(say Doc-tor Way-oo-see-jah-nah,

Doc-tor, or Bah-bah)

[email protected]

http://edutek.net/cascadia/bit115

Page 2: 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

BIT 115: Introduction To Programming 2

Quiz 11

•https://catalysttools.washington.edu/webq/survey/babaw/55000

2

Page 3: 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

BIT 115: Introduction To Programming 3

Review of Quiz 10 Results

3

Page 4: 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

BIT 115: Introduction To Programming

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

Page 5: 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

BIT 115: Introduction To Programming

Homework Assignments & Journals

• J#3 due today• J#4 & A03 due Mon 5/19

Page 6: 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

BIT 115: Introduction To Programming 6

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

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

BIT 115: Introduction To Programming 7

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

Page 8: 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

BIT 115: Introduction To Programming 8

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

Page 9: 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

BIT 115: Introduction To Programming 9

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

Page 10: 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

BIT 115: Introduction To Programming 10

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

Page 11: 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

BIT 115: Introduction To Programming 11

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

Page 12: 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

BIT 115: Introduction To Programming 12

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

Page 13: 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

BIT 115: Introduction To Programming 13

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 callerpublic void doNothing()

{return;System.out.println(“Never say this.”);} 1

3

Page 14: 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

BIT 115: Introduction To Programming 14

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 runningreturn;}}} 1

4

Page 15: 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

BIT 115: Introduction To Programming 15

ICE Part 1, with the sample test code

• FileName.java

15

Page 16: 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

BIT 115: Introduction To Programming 16

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

Page 17: 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

BIT 115: Introduction To Programming 17

• 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

Page 18: 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

BIT 115: Introduction To Programming 18

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