Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object Oriented Programming … and other things you need to program in java."— Presentation transcript:

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

2 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.

3 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 Interpreter Hello World! Bye! Java Bytecode Program

4 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

5 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.

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

7 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

8 Methods (behaviors) modifiers return_type method_name ( parameters ) { method_body } Example: public void drawCircle(int radius) { //draws circle … }

9 Modifiers Example: public void drawCircle(int radius) { //draws circle … } public (everyone can see and use) private (only this class can see it)

10 Modifiers Example public someone.getName(); private someone.getWeight();

11 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)

12 Return Type Examples What’s the return type? someone.raiseHand(); someone.getAge();

13 Parameters Example: public void drawCircle(int radius) { //draws circle … } Information that must be given someone.waveRightHand(); someone.call(String name);

14 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.

15 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

16 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”);

17 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


Download ppt "Object Oriented Programming … and other things you need to program in java."

Similar presentations


Ads by Google