Object-ive Orientation: The Use of Learning Objects for Support Services
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
-
date post
21-Dec-2015 -
Category
Documents
-
view
217 -
download
1
Transcript of Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
![Page 1: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/1.jpg)
Objects Interaction and Source Code
Week 2
![Page 2: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/2.jpg)
OBJECT ORIENTATIONBASICS
REVIEW
![Page 3: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/3.jpg)
• Object – Represents an actual thing!
• Class – ‘Blueprint’ for making an object
• Method – Things an object can do (behaviour)
• Parameter – input required by an object method
• Data type – what type of data is allowed
Object Model BasicsFUNDAMENTAL CONCEPTS
![Page 4: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/4.jpg)
• Many objects (instances) can be created from a single class.
• An object has attributes (fields), and those attributes have values assigned to them.
• The class defines what fields an object has, but each object stores its own set of values (state).
• Each class has source code (Java) associated with it that defines its fields and methods.
Object Model BasicsOTHER OBSERVATIONS
![Page 5: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/5.jpg)
OBJECT ORIENTATION
OBJECT INTERACTION
![Page 6: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/6.jpg)
Create the outside wall:
1. wall = new Square();
2. wall.moveVertical(80);
3. wall.changeSize(100);
4. wall.makeVisible();
How To Draw A HouseA SEQUENCE OF METHOD CALLS
![Page 7: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/7.jpg)
Create the window:
1. window = new Square();
2. window.changeColor(“black”);
3. window.moveHorizontal(20);
4. window.moveVertical(100);
5. window.makeVisible();
How To Draw A HouseA SEQUENCE OF METHOD CALLS
![Page 8: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/8.jpg)
Create the roof:
1. roof = new Triangle();
2. roof.changeSize(50, 140);
3. roof.moveHorizontal(60);
4. roof.moveVertical(70);
5. roof.makeVisible();
How To Draw A HouseA SEQUENCE OF METHOD CALLS
![Page 9: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/9.jpg)
Create the sun:
1. sun = new Circle();
2. sun.changeColor(“yellow”);
3. sun.moveHorizontal(180);
4. sun.moveVertical(-10);
5. sun.changeSize(60);
6. sun.makeVisible();
How To Draw A HouseA SEQUENCE OF METHOD CALLS
![Page 10: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/10.jpg)
How To Draw A HouseA SEQUENCE OF METHOD CALLS
Final result after a sequence of twenty methods
![Page 11: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/11.jpg)
• A house picture is composed of four different shape objects – representing a wall, window, roof and sun.
• The sequence of twenty method calls used to create and manipulate these objects will always be the same.
• It would be convenient to record the twenty method calls and replay them whenever we wished to draw a picture of the house.
Object InteractionOBSERVATIONS ON DRAWING THE HOUSE
![Page 12: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/12.jpg)
BlueJ DemonstrationA First Look at the Picture class
![Page 13: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/13.jpg)
/**
* This class represents a simple picture.
* You can draw the picture using the draw method…
*/
public class Picture
{ private Square wall; // create a reference to a Square object and call it wall.
private Square window; // create a reference to a Square object and call it window.
private Triangle roof; // create a reference to a Triangle object and call it roof.
private Circle sun; // create a reference to a Circle object and calls it sun.
Java Source CodePicture.java
![Page 14: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/14.jpg)
/** * Constructor for objects of class Picture
*/
public Picture()
{
// nothing to do... instance variables are automatically set to null
}
Java Source CodePicture.java
A constructor is a ‘special method’ that is called when the object is created and is used for object initialisation.
Constructors are defined like other methods but use the name of the
class.
![Page 15: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/15.jpg)
Creating a new object
wall = new Square();
variable of type Square assignment
creates an instance of the class
calls the constructor
![Page 16: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/16.jpg)
Sending messages to objects by invoking their methods
wall.moveVertical(80);
![Page 17: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/17.jpg)
public void draw(){ wall = new Square(); wall.moveVertical(80); wall.changeSize(100); wall.makeVisible(); window = new Square(); window.changeColor("black"); window.moveHorizontal(20); window.moveVertical(100); window.makeVisible();
Java Source CodePicture.java
![Page 18: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/18.jpg)
Java Source CodePicture.java
roof = new Triangle(); roof.changeSize(50, 140); roof.moveHorizontal(60); roof.moveVertical(70); roof.makeVisible();
sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(180); sun.moveVertical(-10); sun.changeSize(60); sun.makeVisible();}
![Page 19: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/19.jpg)
• A method of one class can create objects of other classes, and can invoke those object’s methods.
• An object’s method can only be called with its correct method signature – this will be detailed further in this week’s tutorials.
• Always put comments in your source code to ensure others can understand your program.
Object Interaction & Source CodeIMPORTANT POINTS TO REMEMBER
![Page 20: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/20.jpg)
Comments
• Comments are statements that are put into a program to help others understand what it does
• Comments are marked by:• // this is a single line comment• /* this is a multiline comment */• /** so is this */
![Page 21: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/21.jpg)
Variables• A variable is a location in the computer’s memory that is used
to store data• A variable has a name, a type and a value associated with it
int mark = 10;
mark – its name
int – its type – what we can store in it, in this case integers
value - 10 - what is currently stored in it
The statement can be broken down into two separate statements
int mark; // declaration
mark = 10; // assignment
![Page 22: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/22.jpg)
Variable Declarations
• Variables must be declared before they are used.
• To declare a variable we give it a name and a type i.e. what type of data can be stored in it e.g.
• int mark; • double price;• String name;
![Page 23: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/23.jpg)
Assignment
• To put a value in a variable we use the assignment operator =
• mark = 10;• price = 3.64;• name = “Fred”;
![Page 24: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/24.jpg)
Data types• Storage areas in memory must be of the
correct type• Main primitive data types:
– int whole numbers– e.g. 5, 110, -34, 3760– double numbers with a decimal point– e.g. 2.225, 365.2, 0.234, -8.65, 5.0– char keyboard character e.g. ‘q’, ‘N’, ‘2’, ‘(‘– boolean true or false
• N.B. type String is a class. Example String objects: “Hello”, “Meeting at 18:00”, “”
![Page 25: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/25.jpg)
Java syntax - blocks and semicolons
• Curly brackets { } mark the beginning and end of blocks of code including classes and methods
• Semicolons ; mark the end of statements
![Page 26: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/26.jpg)
COMPILING AND RUNNING JAVA PROGRAMS
Javacompiler
JVM Running program
Edit & debug
Libraries / Java API
Source code
Java byte code
Each Java class definition is held in a separate .java file, and the byte code is held in a .class file.
Java Source Code
Picture.java holds our source code, and Picture.class holds the detail the computer needs to create Picture objects.
![Page 27: Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.](https://reader030.fdocuments.us/reader030/viewer/2022032521/56649d595503460f94a396a4/html5/thumbnails/27.jpg)
Required ReadingObjects First With Java – A Practical Introduction using
BlueJ
Related reading for this lecture
• Chapter 1 pp1-17 (Objects and Classes)
Reading for next week’s lecture
• Chapter 2 pp18-37 (Constructors & Methods)