Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”"— Presentation transcript:

1 BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

2 Names Put your name on your computer using a card/sticky note Don’t sit alone Log into Canvas https://cascadia.instructure.com BIT 115: Introduction To Programming2

3 Adding and Keeping Up There are a few spots left so I will sign add forms for those on wait list But everyone must be caught-up with all current assignments by Thursday! BIT 115: Introduction To Programming3

4 4 Today Announcements & Q + A on prior topics (<= 5 minutes) Quiz #3 (5 minutes) Making New Types of Robots Creating new commands/methods (like a ‘turnRight’ command)

5 Lecture 3 Announcements By now everyone should be up and running with Java, jGRASP/Eclipse, 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 5

6 Staying Organized Keep up by checking email daily and reviewing Canvas Calendar and Tentative Course Schedule at the bottom of Syllabus Let’s look at it now BIT 115: Introduction To Programming 6

7 Asking For Help Start Homeworks As Soon As Possible! Unless it’s about your grades, or something specific to a homework assignment, post to the relevant Canvas Discussions first BE SPECIFIC What were you trying to do List what you did up to that point State clearly what you expected to happen State clearly what actually happened You must make at least 2 Canvas Discussion posts before the midterm (& at least 2 more afterwards) Read the associated Canvas Discussion Contributions assignments and Rubric BIT 115: Introduction To Programming 7

8 Asking For Help: Open Lab Tuesdays and Thursdays: 11am – 1pm: Brian, in room CC1-211 except for the following days & times: The room is in use for all of Tuesday, December 15 th If Continuing Education happens to need the room they may pre-empt us, possibly with short notice. 1:15 to 3:15: Elijah, in room CC1-221 except for the following days & times: We will need to be out of the room by 3pm on Thursday, October 15th, October 19th, & December 17th, since someone else needs the room then There's a lab monitor who can help, if you ask nicely BIT 115: Introduction To Programming 8

9 9 Homework A1 is due next class, at the start of class Hand-in will be done via Canvas; upload.doc files to the Canvas assignment, submit.java files to the relevant Java Code Critic Module assignments Let look at those now Try submitting something tonight If you run into trouble, get help! Contact your professor immediately! How? See the Canvas Announcement titled “How to Report Problems” A2 has been posted online Skim through it, and work on parts as we cover them in class.

10 BIT 115: Introduction To Programming 10 Quiz03 under Quizzes in Canvas Access Code:think 5 minute limit The First Quiz And Now…. The First Quiz!

11 BIT 115: Introduction To Programming 11 Make Your Own Robot Commands So far we’ve done simple stuff We’re (eventually) going to be doing more complicated stuff We need ways to make the program seem more simple, so we don’t get (as) lost. Today: Creating a new type of robot! By extending a class Predicting What A Program Will Do BEFORE you Run It

12 Appendix F.1, Chapter 2.1, 2.2 Extending a Class BIT 115: Introduction To Programming 12 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

13 Calling a Constructor BIT 115: Introduction To Programming 13 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 Kelsey inherits 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://www.learningwithrobots.com/doc/

14 Calling a Constructor BIT 115: Introduction To Programming 14 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 execute Java code, to allow the object to do something.

15 BIT 115: Introduction To Programming15

16 BIT 115: Introduction To Programming16 N ow … 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 the turnLeft() 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 method like turnRight() that will do what you want the robot to do! But in order to make this happen, you need to extend the Robot class …

17 Extending a Class: Where Class B extends Class A BIT 115: Introduction To Programming 17 In Plain Ol’ English: Where Class B “inherits” the attributes and actions of Class A … then adds new functionality to them. public class Example extends Object When we’re not interested in extending a class because we’re happy with the methods that come with the java.lang.Object class just the way they are, then we declare our class the ‘normal’ default way: 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 MrRoboto extends Robot Object is the top class of all class hierarchies. When a new instance of anything is made in Java, then it inherits all the attributes and actions of the java.lang.Object class. You can’t get a new object without the Object class. Class Hierarchy Object

18 Extending a Class: Where ClassB extends ClassA BIT 115: Introduction To Programming 18 Instance vs. Extension? An instance is 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 …

19 Extending the Robot Class BIT 115: Introduction To Programming 19 public class MrRoboto extends Robot Robot MrRoboto 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/Inherits from

20 Superclass and Subclass BIT 115: Introduction To Programming 20 Robot MrRoboto Superclass/Parent Subclass/Child

21 Writing a Constructor BIT 115: Introduction To Programming 21 import becker.robots.*; public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } //New service or services go here }

22 Writing a Constructor BIT 115: Introduction To Programming 22 import becker.robots.*; public class MrRoboto extends Robot { // This declares the parameters used by Robot “inside” of MrRoboto public MrRoboto(City theCity, int street, int avenue, 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 immediately used 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.

23 Constructor BIT 115: Introduction To Programming23 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } Robot MrRoboto

24 Constructor BIT 115: Introduction To Programming24 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } Robot super MrRoboto

25 Constructor BIT 115: Introduction To Programming25 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } super MrRoboto  imagine a conduit …

26 Constructor BIT 115: Introduction To Programming26 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } super MrRoboto 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.

27 Adding New Methods/Services 27 public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); }. public void turnRight() { this.turnAround(); this.turnLeft(); }

28 The this keyword 28 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(); }

29 Putting It All Together 29 Two Ways of Doing the Same Thing, however: THE CLASS THAT CONTAINS MAIN HAS TO BE THE SAME NAME AS THE FILE Version 1: One Class MrRoboto.java Version 2: Two Classes MrRobotoMain.java

30 Putting It All Together 30 MrRoboto2.java MrRobotoTest2.java MrRoboto.java All on One FileOn Two Separate Files

31 Canvas Discussions For most questions, try there, first Do NOT post questions about your grades Do NOT post questions about doing Assignment 1 You need to do this on your own DO post questions about stuff like “How do I install Java/jGRASP?” “Where is homework assignment 1?” “I’m having trouble handing in A1”, “I have a question about something we did in class”, etc. BIT 115: Introduction To Programming 31

32 ICE03 In our course’s Canvas Assignments Upload.doc files to the Canvas ICE03 assignment Paste in.java file to Java Code Critic Module (ICE03 assignment) If you finish early, help your classmates then work on A1 & then A2 If you finish late, make sure you turn all pieces in before our next class BIT 115: Introduction To Programming32


Download ppt "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”"

Similar presentations


Ads by Google