Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)"— Presentation transcript:

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 http://edutek.net/cascadia/bit115

2 BIT 115: Introduction To Programming 2 Quiz 12 https://catalysttools.washi ngton.edu/webq/survey/b abaw/55235https://catalysttools.washi ngton.edu/webq/survey/b abaw/55235 Due to Java Code Critic by 11:15 AM! 2

3 BIT 115: Introduction To Programming Quiz More “write me some code” type quizzes This sort of thing will be on the final …and in real life, of course! –we have to be fast developers, so we can't always wait for the computer to catch every little mistake –Little mistakes can lead to big confusion! –we must be able to try out several designs in our head before typing any one of them up How to practice doing this: –Write parts of your homework on paper first –Then type it in, see what you did wrong!

4 BIT 115: Introduction To Programming 4 We’ve sometimes included this for instructional purposes public void turnAround() { this.turnLeft(); // You almost never need "this" for a method call. It's just clutter. You can just use: public void turnAround() { turnLeft(); // this will call the closest method in the inheritance hierarchy Avoid infinite recursion! public void turnAround() { some.parent.turnAround(); // this is fine turnAround(); // Avoid calling your own method! 4

5 BIT 115: Introduction To Programming 5 Convert MS Excel Files If you have turned in an.xlsx file, convert it to.xls and email it to me. So far I can convert.docx files to.rtf 5

6 BIT 115: Introduction To Programming Today Chapter 3.6 –public, private, protected, package-private Programs in multiple files Non-Robot software Unit Testing –Using JUnit with Eclipse

7 BIT 115: Introduction To Programming Next Lecture Ch 5.4.1-5.4.3 = Complex boolean expressions Ch 5.5.1-5.5.4 = For loops, Do...while loops Ch 2.4.3-2.4.4 = Documentation; JavaDoc

8 BIT 115: Introduction To Programming 8 Predicate Methods & Return OK public boolean isRobotOnAvenueFive() { if (this.getAvenue() == 5) { return true; } else { return false; } BEST (simpler)! public boolean isRobotOnAvenueFive() { return this.getAvenue() == 5; }

9 BIT 115: Introduction To Programming 9 Return 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.”); } 9

10 BIT 115: Introduction To Programming 10 Access Modifiers for Classes, Methods, & Objects public : anybody can call this method –Good for methods and constants that we want to make widely available private : nobody (except other methods in the same class) can access this –Good for helper methods (see pattern 3.8.1 p. 155-156) –HOWEVER, private methods aren't made available to classes that inherit from this class, either (subclasses) protected : like private, except that subclasses can call the method (see Robot.breakRobot() JavaDoc)Robot.breakRobot() JavaDoc 10

11 BIT 115: Introduction To Programming 11 Access Modifiers for Classes, Methods, & Objects no modifier (default/package-private): nobody (except other methods in the same package) can access this –Packages are named groups of related classes –Not in textbook: see link on next slide 11

12 BIT 115: Introduction To Programming 12 Access Levels See: http://java.sun.com/docs/books/tutorial/java /javaOO/accesscontrol.html http://java.sun.com/docs/books/tutorial/java /javaOO/accesscontrol.html 12 ModifierClassPackageSubclassWorld public Yes protected Yes No no modifier (package- private) Yes No private YesNo

13 BIT 115: Introduction To Programming 13 Access Modifiers for Classes, Methods, & Objects In general declare everything private –UNLESS you want to make it available to anybody public –UNLESS you want to make it available to subclasses protected –UNLESS you want to make it available to its package no modifier (package-private) 13

14 BIT 115: Introduction To Programming 14 Why hide (encapsulate)? Why would you want something that's private? –Other parts of the program can't manipulate. –Fewer bugs, but isn't any more secure. The main advantage is that other code won't accidentally change your object's internal data. –This idea is called encapsulation: the internal, private, state of an object isn't accessible to other parts of the program, but the public interface is available. –Public code limits you to a particular implementation and limits your flexibility to change your code later.

15 BIT 115: Introduction To Programming 15 encapsulate |en ˈ kaps(y)ə ˌ lāt| verb [ trans. ] enclose (something) in or as if in a capsule. express the essential features of (someone or something) succinctly : the conclusion is encapsulated in one sentence. Computing enclose (a message or signal) in a set of codes that allow use by or transfer through different computer systems or networks. Computing provide an interface for (a piece of software or hardware) to allow or simplify access for the user. [as adj. ] ( encapsulated) enclosed by a protective coating or membrane. 15

16 BIT 115: Introduction To Programming 16 ICE 12 Part 1: public, private, protected About 5 minutes, must finish by 11:50 AM 16

17 BIT 115: Introduction To Programming 17 ICE12 Part 2: Putting Classes in Separate Files About 5 minutes, must finish by Noon 17

18 BIT 115: Introduction To Programming 18 ICE 12 Part 3: Non-robotic Software static classes/methods/objects: Always have the same value within a Java Virtual Machine (Java VM) Open the ICE_12.java. Note that the main method is always a static method: It can be called on a class without instantiating an object first –Can only call static methods or make objects and call their non-static methods –Useful for instantiating objects including those of it’s own class, often done to get things going in a program 18

19 BIT 115: Introduction To Programming 19 final and static keywords final keyword means the value never changes once it is set – final classes can’t have any subclasses final static objects thus never change and are called constants –constants’ names should be in all caps – public static final Direction EASTDirection 19

20 BIT 115: Introduction To Programming 20 ICE_12.java: main & println Does not import any robot classes Has a main method so it can be run as Java application –Uses default system output (using System.out.println(“…”) ). This is fine because the System.out object is static and its println method is public. –However the ICE_12.countdown() method is not static, so it is only callable from the static main method if it is being called on an existing object. So an ICE_12 object called runningObject is instantiated so that runningObject.countdown() can be called.

21 BIT 115: Introduction To Programming 21 ICE 12 Part 4: Using println for debugging, using JUnit for testing –Test the ICE_12.countdown() method using JUnit 4 –Fix the ICE_12.countdown() method and see how JUnit confirms that it is fixed –Writing test cases is crucial for applications people will use Allows software developers to change an application without fear of missing the reoccurrence of old bugs! 21

22 BIT 115: Introduction To Programming 22 A03 Groups Who doesn’t have a partner? 22


Download ppt "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)"

Similar presentations


Ads by Google