BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett [email protected].

30
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett c [email protected]

Transcript of BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett [email protected].

Page 1: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

LECTURE 3

Instructor: Craig [email protected]

Page 2: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

2

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

Page 3: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

3

What? Tinnitus! That’s What!

Page 4: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

4

Assignment 1 is Due LECTURE 5Zipped & Uploaded to StudentTracker by MIDNIGHT

NEXT Tuesday, October 13, ONE WEEK FROM TODAYIt’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

• FYI: You should have enough information after today’s lecture to successfully complete Assignment 1

HOMEWORK

Page 5: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

5

• Assignment 1 (LECTURE 5) Tuesday, October 13 in StudentTracker by midnight

• Assignment 2 (LECTURE 8) Thursday, October 22

• Assignment 1 Revision (LECTURE 10) Tuesday, November 3

• Assignment 2 Revision (LECTURE 12) Tuesday, November 10

• Assignment 3 (LECTURE 13) Thursday, November 12

• Assignment 3 Revision (LECTURE 16) Tuesday, November 24

• Assignment 4 (LECTURE 20) Tuesday, December 8

• Assignment 4 Revision (LECTURE 21) Thursday, December 10

Assignment Dates (By Due Date)

Page 6: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Going forward, always leave your completed quiz beside your throughout the Lecture (and the Quarter) because

your Instructor will use these to learn your names

6

• You each get a Quiz hand-out: Put your name on it• When you think your drawing is complete, raise your hand

– 5 minute limit

And Now…. The First Quiz!

Page 7: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

IMPORTANT! CityFrame – An older version of becker.jar file contained a class called CityFrame which when used looked something like this:

CityFrame City = CityFrame(someCity);

Please ignore any reference to this. It will, however, rear its ugly head in the ICEs and Assignments on purpose as an example of old 'legacy' code that should either be "commented out" with // or deleted altogether since it will not allow the program to run correctly!

7

Please Note! CityFrame!

// CityFrame City = CityFrame(someCity);

Page 8: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Lecture 3Buckle up! This could really

8

• Extending a Class : Creating a new type of Robot• Style and Java Coding Conventions

Page 9: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Appendix F.1, Chapter 2.1, 2.2 Extending a Class

BIT 115: Introduction To Programming

9

• Extension (B extends A)• Extending the Robot Class• Superclass and Subclass• Constructor• Adding a Method (Service)

• turnAround();• turnRight();

• The This Keyword (Implicit Parameter)• Putting It All Together

Page 10: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructors

BIT 115: Introduction To Programming

10

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/

Page 11: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

11

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 act, to allow the object to do something.

Constructors

Page 12: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

12

Page 13: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

BIT 115: Introduction To Programming

13

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 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 …

In other words, you’re going to make a new class from the Robot class so you can add new methods to it like turnRight()

Page 14: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Extending a Class:Where Class B extends Class A

14

In Plain Old English: Where Class B “inherits” all the attributes and actions of Class A and then adds it’s own functionality by creating new methods

public class Example extends Object

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:

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 ofObject)

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 Object class. You can’t get a new object without Object.

Class Hierarchy

Object

Page 15: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Extending a Class:Where ClassB extends ClassA

BIT 115: Introduction To Programming

15

Instantiation (Instance) vs. Extension?

Instantiation 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 …

Page 16: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

16

Think of it as adding an extension to a house.

You still get to use all of the original house, butyou also get to use the new section you addedto the house

Page 17: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Extending the Robot Class

BIT 115: Introduction To Programming

17

public class MrRoboto extends Robot

Robot

MrRobotoMrRoboto “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

Page 18: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Superclass and Subclass

BIT 115: Introduction To Programming

18

Robot

MrRoboto

Superclass

Subclass

Page 19: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

19

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

}

Page 20: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

20

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.

Page 21: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

21

public class MrRoboto extends Robot{ public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); }}

Robot

MrRoboto

Page 22: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

22

public class MrRoboto extends Robot{ public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); }}

Robot super

MrRoboto

Page 23: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

23

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 …

Page 24: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Constructor

BIT 115: Introduction To Programming

24

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.

Page 25: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Adding New Methods (Actions)

25

public void turnAround() { this.turnLeft(); this.turnLeft(); }

public void move3() { this.move(); this.move(); this.move(); }. public void turnRight() { this.turnAround(); this.turnLeft(); }

Page 26: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

The this keyword

26

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(); }

Page 27: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Putting It All Together

27

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 ClassMrRoboto.java

Version 2: Two ClassesMrRobotoMain.java

Page 28: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Putting It All Together

28

MrRoboto2.java

MrRobotoTest2.java

MrRoboto.java

All on One File On Two Separate Files

Page 29: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Chapter 2.4 – Coding Conventions (Style)

BIT 115: Introduction To Programming

29

http://javascript.crockford.com/javacodeconventions.pdf

http://geosoft.no/development/javastyle.html

These coding conventions are not only good for Java, but for other languages as well, including C, C++, C#, JavaScript, Perl, Python, etc, to name a few.

Page 30: BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu.

Lecture 3 ICE: Creating a New Type of Robot

BIT 115: Introduction To Programming

30