* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Slides_12
Swift (programming language) wikipedia , lookup
Java syntax wikipedia , lookup
Scala (programming language) wikipedia , lookup
Java (programming language) wikipedia , lookup
Java performance wikipedia , lookup
Logic programming wikipedia , lookup
Programming language wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Functional programming wikipedia , lookup
Name mangling wikipedia , lookup
Java ConcurrentMap wikipedia , lookup
Design Patterns wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Go (programming language) wikipedia , lookup
Reactive programming wikipedia , lookup
Structured programming wikipedia , lookup
C Sharp syntax wikipedia , lookup
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 BIT 115: Introduction To Programming Quiz 12 •https://catalysttools.washi ngton.edu/webq/survey/b abaw/55235 • Due to Java Code Critic by 11:15 AM! BIT 115: Introduction To Programming 2 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! BIT 115: Introduction To Programming • 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! BIT 115: Introduction To Programming 4 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 BIT 115: Introduction To Programming 5 Today • Chapter 3.6 – public, private, protected, package-private • Programs in multiple files • Non-Robot software • Unit Testing – Using JUnit with Eclipse 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 BIT 115: Introduction To Programming 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; } BIT 115: Introduction To Programming 8 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.”); • } BIT 115: Introduction To Programming 9 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() BIT 115: Introduction To Programming 10 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 BIT 115: Introduction To Programming 11 Access Levels Modifier Class Package Subclass World public Yes Yes Yes Yes protected Yes Yes Yes No no modifier (packageprivate) Yes Yes No No private Yes No No No • See: http://java.sun.com/docs/books/tutorial/java BIT 115: Introduction To Programming 12 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) BIT 115: Introduction To Programming 13 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. BIT 115: Introduction To Programming 14 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 BIT 115: Introduction To Programming 15 coating or membrane. ICE 12 Part 1: public, private, protected • About 5 minutes, must finish by 11:50 AM BIT 115: Introduction To Programming 16 ICE12 Part 2: Putting Classes in Separate Files • About 5 minutes, must finish by Noon BIT 115: Introduction To Programming 17 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 BIT 115: Introduction To Programming 18 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 EAST BIT 115: Introduction To Programming 19 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. BIT 115: Introduction To Programming 20 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! BIT 115: Introduction To Programming 21 A03 Groups • Who doesn’t have a partner? BIT 115: Introduction To Programming 22