Object Oriented Programming
Download
Report
Transcript Object Oriented Programming
Object Oriented Programming
… and other things you need to
program in java.
Compiling a Program
You write your source code (or just
code) in an editor (JCreator)
You click compile and the computer
translates the code into bytecode (.class
file) that the Java interpreter can
understand
If you break the rules of the language,
the compiler will give you errors.
Compiling & Running Process
Library files
Hi h;
h=new Hi();
h.hello();
h.bye();
Editor
Interpreter
Compiler
Class files
Source Code
Java Bytecode
Program
Hello World!
Bye!
Be careful
If you compile and there are errors,
Java will not create a new .class file
So if you click on execute after you
have errors, Java might run an older
version of your file
Different Types of Errors
Compile time errors – the compiler will
tell you that the language of your file is
wrong.
Run time errors – your file compiles,
but it does the wrong thing.
Using comments
You can add comments in your code to
separate things out or to explain what you
are doing (the compiler will ignore the
comments):
Single line comments
//here is where draw the roof of the house
marker.move(86,86); …
Block comments – multiple lines
/* marker.forward(300);
marker.forward(100);*/
import
If I was making a new car would I
make tires from scratch or buy them
from a tire company?
Software developers are lazy
import gpdraw.*;
Importing in a package or already
written code
Methods (behaviors)
modifiers return_type method_name ( parameters )
{
method_body
}
Example:
public void drawCircle(int radius)
{
//draws circle
…
}
Modifiers
Example:
public void drawCircle(int radius)
{
//draws circle
…
}
public (everyone can see and use)
private (only this class can see it)
Modifiers Example
public
someone.getName();
private
someone.getWeight();
Return Type
Example:
public void drawCircle(int radius)
{
//draws circle
…
}
What information comes back from the
method
String (text)
int (integer)
double (decimal number)
void (nothing)
Return Type Examples
What’s the return type?
someone.raiseHand();
someone.getAge();
Parameters
Example:
public void drawCircle(int radius)
{
//draws circle
…
}
Information that must be given
someone.waveRightHand();
someone.call(String name);
Method Body
Example:
public void drawCircle(int radius)
{
//draws circle
…
}
How to do the method.
Your methods were full of forward, move
and turnRight calls.
Declaring an Object
DrawingTool pencil;
I have a DrawingTool called pencil
pencil is an instance of a DrawingTool
Person alicia;
I have a Person called alicia
alicia is an instance of a Person
Instantiating An Object
Create and initialize an object
pencil = new DrawingTool(poster);
Calls the constructor (special method) sending the
parameter (argument) poster
Person alicia = new Person(“Alicia”);
The Difference Between
Classes and Objects
Classes are a blueprints for objects
A Person will have 2 arms and a name etc.
An Arena will have seats, etc
Objects are specific instances of a class
The specific pe rson with the name Alicia
The Staples Center is a specific instance of
an Arena