Title: ObjectOriented Programming in Java
1Object-Oriented Programmingin Java
- UW/ACSE
- CS Summer Institute
- Sandy Graham
2Your First Java Program
public static void main(String
args) System.out.println(Hello World.)
Follow a pattern to start! Dont worry about what
this all means for now.
3Objects First Approach
- Using Karel the Robot
- not an original idea - has been used to introduce
programming concepts in Pascal - used to introduce C
- UW starts a pilot section of Java in the Fall
1998
4Karel/Becker the Robot
5Karel the Robot
http//www.student.math.uwaterloo.ca/cs132/Doc/Ka
rel/
6Using UML in Design
- every class has a box
- class name
- attributes/instance variables
- behaviours/methods
- Arrows indicate the relationships among classes
- indicates a 0-many relationship
- other symbols not shown here
7Topics in CS133 using Karel
- Week 1 what is an object, behaviours, attributes
- Week 2extending objects/selection statements
- Week 3method parameters/ loops
- Week 4 instance variables/ constructors
- Week 10 polymorphism
8Other Java Topics in CS133
- Strings (built in library)
- I/O (use a homemade library)
- Arrays
- GUIs (fun stuff)
- OO-Design
- Project management
9Missing/Minor Topics in CS133
- exceptions
- interfaces
- applets
- threads
- many others you cant do everything
10A First Program Using Karel
Import any libraries.
Create a class.
Include main method.
Create some objects.
Make the objects do something.
11Second Program Using KarelMultiple Instances of
Robots
12Karel Problem 3
13Making a Smarter Robot(Inheritance)
- add functionality through new methods
- stepwise refinement
- inherit all instance variables and methods from
the super class - new functionality
- turnRight()
- putThingOnNextCorner()
- pickAllThings()
Robot
14More Complex Methods
- parameters
- always passed by value
- sometimes the value is a reference
- all method calls include the implicit parameter
(this) - two major types of variables primitive and
object - overloading methods
- unique method signatures
- overriding methods
- inheritance hierarchy
- objects always know what they are
15Overloading Overriding
import becker.robots. public class ShowSpin
extends Object public static void main
(String args) City aCity new
City() KarelFrame frame new
KarelFrame(aCity) SpinningRobot sandy
new SpinningRobot (aCity, 1, 5,
Directions.EAST, 0) for (int i 0 i lt
3 i) sandy.move()
sandy.turnLeft() sandy.move(3)
16Passing Objects as Parameters
import becker.robots. public class ObjParam
extends Object public static void
main(String args) City myCity new
City("Slave.txt") Mover karel new
Mover(myCity, 1, 4, Directions.NORTH, 0)
Robot tubman new Robot(myCity, 2, 4,
Directions.NORTH, 0) KarelFrame f new
KarelFrame(myCity, 4, 5)
karel.move(tubman, 4) karel.move(tubman,
4)
public class Mover extends Robot ... public
void move(Robot slave, int dist) while(dist
gt 0) slave.move() dist--
17Modifiers
- public -gt visible to everyone
- used for class definition, methods which are part
of the interface of the object - private -gt visible only within the class file
- used for instance variables and helper methods
- also protected and blank visibility modifiers
- static - class-wide (not instance)
- final - constant
18Making More Complex Objects
- A CourierRobot will deliver packages to various
locations. It has a home base, and will be
supplied with the address and package (some
number of things) for delivery. The robot must
calculate the cost of delivery base rate
distancecost/step weightcost/kg. -
Create a new kind of robot to handle this task.
Develop a UML diagram, and implement the class.
19Partial UML for CourierRobot
20Constructors
- a special method for creating an instance of the
class - no return type
- has the same name as the class
- may be overloaded
- major task is to initialize the instance variables
21Polymorphism
- has already been used in parameter passing
- used to allow many different kinds of objects in
the same variable - collections of objects
- everything automatically extends (big O) Object
in Java
22Using Vectors
- Vector is a class in the Java util library
- include java.util.Vector
- essentially a partially filled array
- methods include addElement(Object obj),
insertElementAt(Object obj, int index), Object
elementAt(int index), boolean remove(Object
obj), int size(), - everything is stored as an Object
- need to cast when the values are retrieved