17
Object Oriented Programming … and other things you need to program in java.

Object Oriented Programming … and other things you need to program in java

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming … and other things you need to program in java

Object Oriented Programming

… and other things you need to program in java.

Page 2: 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 understandIf you break the rules of the language, the compiler will give you errors.

Page 3: Object Oriented Programming … and other things you need to program in java

Compiling & Running Process

Source Code

Editor

Hi h; h=new Hi();

h.hello(); h.bye();

Hi h; h=new Hi();

h.hello(); h.bye();

Compiler

Class files

Library files

InterpreterHello World!Bye!

Java BytecodeProgram

Page 4: Object Oriented Programming … and other things you need to program in java

Be careful

If you compile and there are errors, Java will not create a new .class fileSo if you click on execute after you have errors, Java might run an older version of your file

Page 5: Object Oriented Programming … and other things you need to program in java

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.

Page 6: Object Oriented Programming … and other things you need to program in java

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);*/

Page 7: Object Oriented Programming … and other things you need to program in java

import

If I was making a new car would I make tires from scratch or buy them from a tire company? Software developers are lazyimport gpdraw.*; Importing in a package or already written code

Page 8: Object Oriented Programming … and other things you need to program in java

Methods (behaviors)

modifiers return_type method_name ( parameters ){ method_body}

Example:public void drawCircle(int radius){ //draws circle

…}

Page 9: Object Oriented Programming … and other things you need to program in java

Modifiers

Example:public void drawCircle(int radius){ //draws circle

…}

public (everyone can see and use)private (only this class can see it)

Page 10: Object Oriented Programming … and other things you need to program in java

Modifiers Example

public someone.getName();

private someone.getWeight();

Page 11: Object Oriented Programming … and other things you need to program in java

Return Type

Example:public void drawCircle(int radius){ //draws circle

…}

What information comes back from the methodString (text)int (integer)double (decimal number)void (nothing)

Page 12: Object Oriented Programming … and other things you need to program in java

Return Type Examples

What’s the return type? someone.raiseHand(); someone.getAge();

Page 13: Object Oriented Programming … and other things you need to program in java

Parameters

Example:public void drawCircle(int radius){ //draws circle

…}

Information that must be given someone.waveRightHand(); someone.call(String name);

Page 14: Object Oriented Programming … and other things you need to program in java

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.

Page 15: Object Oriented Programming … and other things you need to program in java

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

Page 16: Object Oriented Programming … and other things you need to program in java

Instantiating An Object

Create and initialize an objectpencil = new DrawingTool(poster); Calls the constructor (special method)

sending the parameter (argument) posterPerson alicia = new Person(“Alicia”);

Page 17: Object Oriented Programming … and other things you need to program in java

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