270 likes | 438 Views
BIT 115 : Introduction To Programming LECTURE 3a. Instructor: Craig Duckett c duckett@cascadia.edu. Lecture 3 Announcements. By now everyone should be up and running with Java, jGRASP , and the Becker Robots on their home or personal computers. Any Problems?
E N D
BIT 115: Introduction To ProgrammingLECTURE 3a Instructor: Craig Duckett cduckett@cascadia.edu
Lecture 3 Announcements • By now everyone should be up and running with Java, jGRASP, and the Becker Robots on their home or personal computers. • Any Problems? • Has everyone had a chance to work with the Java programs and the Becker Robots? Reading Assignment for Today • Appendix F.1 – Extending a Class • Chapter 2.1, 2.2 – Extending Robot Class • Chapter 2.4 – Coding Style BIT 115: Introduction To Programming
HOMEWORK Assignment 1is due LECTURE 4 • Monday, July 14th • It’s posted on the website under Assignments menu • It will be due by midnight • If unsure how to upload to StudentTracker, then bring your work to class, in electronic form, and we will go over how to hand in the homework: Student Tracker How to Use Student Tracker • If you’re stuck, seek help • Talk to the Instructor or a classmate • Email me BIT 115: Introduction To Programming
An Upcoming Reminder • Assignment 1 Due Lecture 4by MIDNIGHT • Monday, July 14th I will double dog tryto have Assignment 1 graded and back by Wednesday, July 16th • Assignment 2Due Lecture 7 by midnight • Wednesday, July 23rd • Assignment 1 Revisiondue Lecture 8 - Monday, July 28th(if you are wanting to “improve” your grade) BIT 115: Introduction To Programming
And Now…. The First Quiz! • You each get a hand-out: Put your name on it • When your program works, raise your hand • 5 minute limit BIT 115: Introduction To Programming
Lecture 3Buckle up! This could really zoom-zoom-zoom! • Extending a Class : Creating a new type of Robot (Predicting What A Program Will Do BEFORE you Run It) • Styleand Java Coding Conventions (If time) BIT 115: Introduction To Programming
Appendix F.1, Chapter 2.1, 2.2 Extending a Class • Extension (B extends A) • Extending the Robot Class • Superclass and Subclass • Constructor • Adding a Service • turnAround(); • turnRight(); • The This Keyword (Implicit Parameter) • Putting It All Together BIT 115: Introduction To Programming
Constructor Here, when we create a new instance (an object) of the Robot class, a ‘hidden’ default constructor works in the background to make sure that Kelseyinherits all the attributes and methods available to Robots, including its placement on a particular Street and Avenue and Direction in a particular City, and that it can use all of the actions (methods) available to the Robot class (including move(), pickThing(), turnLeft(), putThing(), frontIsClear(), etc.) http://faculty.cascadia.edu/cduckett/bit115/Becker/Documentation/index.html BIT 115: Introduction To Programming
Constructor Constructors have one purpose in life. To create an instance of a class. This can also be called creating an object, as in: The purpose of a method, by contrast is much more general. The purpose of a method is to executeJavacode, to allow the object to dosomething. BIT 115: Introduction To Programming
Now … what if these is an action that you might want Kelsey to do that isn’t found in the Robot class? For instance, instead of invoking the theturnLeft() method three times, you could just call up a turnRight() ? The problem is, the Robot class does not have a turnRight() command (method). The Robot class has been finalized. You cannot add to it. The good news is, you can create a new methodlike turnRight() that will do what you want the robot to do! But in order to make this happen, you need to extend the Robotclass … BIT 115: Introduction To Programming
Extending a Class:Where ClassBextendsClassA In Plain Ol’ English: Where ClassB “inherits” the attributes and actions of ClassA … then adds new functionality to them. When we’re not interested in extending a class because we’re happy with the methods that come with that class just the way they are, then we declare our class the ‘normal’ default way: public class Example extends Object Object Object is the top class of all class hierarchies. When a new instance of anything is made in Java, then it inheritsall the attributes and actions of the Object class. You can’t get a new object withoutObject. Class Hierarchy However, if we want to add new functionality (methods) to the Robot class (like turnRight) then we need to extend the Robot class (which is itself an extension of Object) public class MrRobotoextends Robot BIT 115: Introduction To Programming
Extending a Class:Where ClassBextendsClassA Instance vs. Extension? Instance creates a new object from a class, but extension extends a new class from a class through inheritance, allowing for an improved class that might offer additional attributes and services (methods) not available in the original class … BIT 115: Introduction To Programming
Extending the Robot Class public class MrRoboto extends Robot MrRoboto inherits MrRoboto “inherits” all of the Robot attributes and services and then can have additional attributes and services of its own (i.e., those not shared by Robot). extends Robot BIT 115: Introduction To Programming
Superclass and Subclass Robot Superclass MrRoboto Subclass BIT 115: Introduction To Programming
Constructor importbecker.robots.*;public class MrRobotoextends Robot{ publicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } //New service or services go here } BIT 115: Introduction To Programming
Constructor importbecker.robots.*;public class MrRobotoextends Robot{ // This declares the parameters used by Robot “inside” of MrRobotopublicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) // This passes on information received by the parameters used by Robot ‘inside” of MrRoboto { super(theCity, street, avenue, aDirection); //Instead of Robot here, Java uses the keyword super} //New service or services go here } Constructors fulfill a special roll. They are responsible for ensuring an object is set up properly when it is created, and that it can be immediatelyused once it is created. This construction process is known as initialization. Two other details about constructors: they must have the same name as the class and they do not have a return type, not even a void. NOTE: We will talk briefly about return types in just a few minutes, and go over them in greater detail in an upcoming lecture. BIT 115: Introduction To Programming
Constructor public class MrRobotoextends Robot{ publicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } MrRoboto Robot BIT 115: Introduction To Programming
Constructor public class MrRobotoextends Robot{ publicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } MrRoboto Robot super BIT 115: Introduction To Programming
Constructor public class MrRobotoextends Robot{ publicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } MrRoboto imagine a conduit … super BIT 115: Introduction To Programming
Constructor public class MrRobotoextends Robot{ publicMrRoboto(City theCity, intstreet, intavenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } bothell, 3, 2, Direction.SOUTH Since MrRoboto is inheriting the Robot parameters, the Robot still needs those parameters in order for MrRoboto to inherit them. This is why it appears as if there are two sets of parameters: one set to pass through MrRoboto, a second set for Robot to receive them, where Robot sends them back to MrRoboto by extension. MrRoboto super BIT 115: Introduction To Programming
Adding New Services public void turnAround() { this.turnLeft();this.turnLeft(); }public void move3() { this.move();this.move();this.move(); }.public void turnRight() { this.turnAround();this.turnLeft(); }
The this keyword The new Java feature in the new services we created is the use of the this keyword. The keyword this is useful when you need to refer to an instance of the class from its method, but without having to refer to it by a specific name. Why? Because when you create the new method, you don’t know the name of the particular robot that is going to use it, so ‘this’ is a kind of placeholder name. The this keyword helps us to avoid name conflicts, and also creates a shortcut to having to invent a unique name for each field in the different methods. public void turnAround() { this.turnLeft();this.turnLeft(); }
Putting It All Together Two Ways of Doing the Same Thing Version 1: One Class MrRoboto.java Version 2: Two Classes MrRobotoMain.java
Putting It All Together MrRoboto2.java MrRoboto.java MrRobotoTest2.java All on One File On Two Separate Files
Chapter 2.4 – Coding Conventions (Style) http://www.oracle.com/technetwork/java/codeconv-138413.html BIT 115: Introduction To Programming
Lecture 3 ICE: Creating a New Type of Robot • ICE_03_Demo_1.javaDEMO • In-Class Exercise Directions (ICE 10, 11, 12) • ICE_03_01_Trace.java • ICE_03_02_CompileErrors.java • ICE_03_03_WalkDownWalls.java BIT 115: Introduction To Programming