Lecture 3. Review (What is Class and Object ?) The world of JAVA contains objects The world of JAVA.

22
Lecture 3
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Lecture 3. Review (What is Class and Object ?) The world of JAVA contains objects The world of JAVA.

Lecture 3

Review (What is Class and Object ?)

• The world of JAVA contains objects

The world of JAVA

Review (What is Class and Object ?)

• Objects are generated from

The world of JAVA

classes

Class Car

Review (What is Class and Object ?)

Honda

Ford

TOYOTA

Today

• Let’s write a method in the class Turtle

– We are going to write our own methods

– So far, we just used methods written by other programmers (e.g. forward(), turnLeft(), etc)

Writing Java program

• Application (or Software) is written in programming language– Java application is constructed by Java programs written

in Java programming language– Generally, when we say that “writing java program”, it

means that “writing classes”– i.e. we are writing blueprints for applications

Attributes and Methods

• Class will contain– a set of attributes and methods

• Attribute–State of object generated from the classE.g. name, color, size, etc …

• Methods–Behavior of object generated from the

classE.g. moveTo(int x, int y), turnLeft(), etc …

Class Car

Honda

Ford

TOYOTA

Example (Attributes and Methods)

Attribute

color, size, name, etc

Methods

move, turn, stop, etc

Java Source Code (source file)

public class SimpleTurtle{

///// Field (Attributes) ///// …

/////// Method /////// …

}

• Each class is programmed in source filenamed ClassName.java

For example, SimpleTurtle.java

How to run the program?

How do computers understand source codeswritten in Java programming language ?

• Basically, there are 3 steps

1. Edit source code

2. Compile source code

3. Execute the compiled code

Repeat!

Compile

• Once we write source codes (i.e. .java file),we have to translate it to the codes which computers can understand– The process of translation is called “compile”

• Java compiler performs this task• The compiled code is called java Bytecode

Source code(.java)

Bytecode(.class)

JavaCompiler

Execute (Run)

• In order to execute bytecodes– bytecode compiler will translate the codes into

machine codes (sequences of 0 and 1)

• Finally, computers understand what we write in Java programming language– i.e. computers understand what we want

Bytecode(.class)

BytecodeCompiler Machine

Code

Level of programming language

Java source code– Human readable language– High-level programming

language

Java bytecode – Human readable notation

of machine code

Machine code– Binary string (0 and 1)

x = x + y

.data x:

.word 0x42 y:

.word 0x43

.text start:set x, %r1ld [%r1], %r2 ! $x$ --> %r2set y, %r1 ld [%r1], %r3 ! $y$ --> %r3add %r2, %r3, %r2 ! $x+y$ --> %r2set x, %r1 st %r2, [%r1] ! $x+y$ --> x end: ta 0

10110000 01100001 00110011 1001110001001011 10101001 11100100 0001111111000010 10011001 11111100 1001101010101010 00101010 00110100 1101001010000010 10101010 01010110 00100101(omit…)

Example

Source code

Java compiler Java bytecode

Java interpreterBytecodecomplier

Machine code

.java file

.class file

Execute!

Summary

* We can test(in Interaction panel)

Practice

– Let’s write our own method!!!

• Edit source code

- Open Turtle.java

We gonna write Methods Here!!!

After this line

• Write a method, named drawOne

////////// method //////////

public void drawOne(){

}

• Write a method, named drawOne

////////// method //////////

public void drawOne(){

forward(100);turn(225);forward(40);

}

• Save source code

– Save Turtle.java

Note:• File name should be the same as Class name• Extension of the file is .java

• Compile it– Right click on the source file

Success?

• Let’s Test the compiled code

– In an interaction panel, create a turtle in the world, then try to call the method drawOne()

Challenge!!!

• Write a method for Turtle

– The method name is drawSquare

– The method draws the one square with size 50