Lecture 3 PowerPoint

advertisement
BIT 115: Introduction To
Programming
LECTURE 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
2
Assignment Dates (By Due Date)
• Assignment 1 (LECTURE 4)
Monday, July
•
•
•
•
•
•
•
18th,
in StudentTracker by midnight
Assignment 2 (LECTURE 7)
Wednesday, July 27th
Assignment 1 Revision (LECTURE 8)
Monday, August 1st
Assignment 2 Revision (LECTURE 10)
Monday, August 8th
Assignment 3 (LECTURE 11)
Wednesday, August 10th
Assignment 3 Revision (LECTURE 13)
Wednesday, August 17th
Assignment 4 (LECTURE 15)
Wednesday, August 24th
EXTRA CREDIT (LECTURE 25)
Wednesday, August 24th
The Fickle
Finger of Fate
3
H Assignment 1 is due LECTURE 4
th
•
Monday,
July
18
O
M
– It’s posted on the website under Assignments menu
– It will be due by midnight
E
• If unsure how to upload to StudentTracker, then bring your
work to class, in electronic form, and we will go over how to
W
hand in the homework:
Student Tracker
O
How to Use Student Tracker
R • If you’re stuck, seek help
– Talk to the Instructor or a classmate
K
– Email me
BIT 115: Introduction To Programming
4
Lecture Setup Strategy
Some Lecture we will have one long lecture followed by one long ICE period, so
the class will basically only be divided into two parts: Lecture and ICE.
Other Lectures, like today’s lecture, will be divided into four (4) parts: Lecture,
ICE, Lecture, ICE. You can tell if it is going to be a four part class by seeing if
there are two lecture PowerPoints offered (for example , Lecture 03a and
Lecture 03b)
The schedule for four part classes will be as such (approximately):
• Lecture (a): 45 minutes – 11:30am-12:15pm
• ICE (a): 45 minutes – 12:15pm-1:00pm
• Lecture (b): 45 minutes – 1:00pm-1:45pm
• ICE (b) 45 minutes – 1:45pm-2:30pm
NOTE: Times may vary according to this scheme, although this is the working
plan on how I will try to divide the class when there are two lecture points.
5
In-Class Exercises (ICEs)
•
•
•
•
not
You do
have to submit ICEs, since I grade these in-class. If
you finish all your ICEs early and I sign you off on them, you are done and
you can leave the class early.
If you are working on the ICEs until the end of class, but did not finish
them, you will still get full credit for working on them, but it is HIGHLY
RECOMMENDED that you finish the ICEs on your own outside of class
since they have been designed to help you with the concepts that will be
used in the Assignments.
If you want, you can show me that you have completed the ICEs during
the next ICE-time in the next Lecture.
Starting with Lecture 4, I will start posting “Solutions” to most (but not
all) of the ICEs.
6
And Now….
The First Quiz!
• 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
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
7
Please Note! CityFrame!
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!
// CityFrame City = CityFrame(someCity);
8
Lecture 3
Buckle up! This could really
• Extending a Class : Creating a new type of Robot
• Style and Java Coding Conventions
9
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 Method (Service)
• turnAround();
• turnRight();
• The This Keyword (Implicit Parameter)
• Putting It All Together
BIT 115: Introduction To Programming
10
Constructors and
Extending a Class
Object  Class
Left
Right
City Bothell = new City();
Object
Class No Arguments
City Bothell = new City(8, 10);
Object
Class With Arguments
Robot Robbie = new Robot(Bothell, 3, 0, Direction.EAST, 0);
Object
Class
With Arguments
BIT 115: Introduction To Programming
12
Constructors
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/
BIT 115: Introduction To Programming
13
Constructors
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.
BIT 115: Introduction To Programming
14
BIT 115: Introduction To Programming
15
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()
BIT 115: Introduction To Programming
16
Extending a Class:
Where Class B extends Class A
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
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 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.
Object
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 MrRoboto extends Robot
17
Extending a Class:
Where ClassB extends ClassA
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 …
BIT 115: Introduction To Programming
18
Think of it as adding an extension to a house.
You still get to use all of the original house, but
you also get to use the new section you added
to the house 
19
Extending the Robot Class
public class MrRoboto extends Robot
MrRoboto
extends
inherits
Robot
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).
BIT 115: Introduction To Programming
20
Superclass and Subclass
Robot
Superclass
MrRoboto
Subclass
BIT 115: Introduction To Programming
21
Constructor
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
}
BIT 115: Introduction To Programming
22
Constructor
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.
BIT 115: Introduction To Programming
23
Constructor
public class MrRoboto extends Robot
{
public MrRoboto(City theCity, int street, int avenue, Direction aDirection)
{
super(theCity, street, avenue, aDirection);
}
}
MrRoboto
Robot
BIT 115: Introduction To Programming
24
Constructor
public class MrRoboto extends Robot
{
public MrRoboto(City theCity, int street, int avenue, Direction aDirection)
{
super(theCity, street, avenue, aDirection);
}
}
MrRoboto
Robot super
BIT 115: Introduction To Programming
25
Constructor
public class MrRoboto extends Robot
{
public MrRoboto(City theCity, int street, int avenue, Direction aDirection)
{
super(theCity, street, avenue, aDirection);
}
}
MrRoboto
 imagine a conduit …
super
BIT 115: Introduction To Programming
26
Constructor
public class MrRoboto extends Robot
{
public MrRoboto(City theCity, int street, int avenue, Direction aDirection)
{
super(theCity, street, avenue, aDirection);
}
}
bothell, 3, 2, Direction.SOUTH
MrRoboto
super
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.
BIT 115: Introduction To Programming
27
Adding New Methods (Actions)
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
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
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
MrRoboto2.java
MrRoboto.java
MrRobotoTest2.java
All on One File
On Two Separate Files
31
Chapter 2.4 – Coding Conventions (Style)
http://geosoft.no/development/javastyle.html
http://javascript.crockford.com/javacodeconventions.pdf
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.
BIT 115: Introduction To Programming
32
Lecture 3a ICE:
Creating a New Type of Robot
Approximate Time: 12:15 to 1:00pm
•ICE_03_Demo_1.java DEMO
•In-Class Exercise Directions
•ICE_03_01_Trace.java
•ICE_03_02_CompileErrors.java
•ICE_03_03_WalkDownWalls.java
BIT 115: Introduction To Programming
33
Download