1 / 22

The World of JAVA: Class and Object Review

This lecture provides a review on the concepts of class and object in the world of JAVA. It explains how objects are generated from classes, and introduces the attributes and methods that classes contain. The lecture also covers the process of writing and executing Java programs, including the compilation and execution steps. Additionally, it includes a practical exercise on writing methods for a Turtle class.

bspruill
Download Presentation

The World of JAVA: Class and Object Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3

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

  3. classes The world of JAVA Review (What is Class and Object ?) • Objects are generated from

  4. Honda Ford TOYOTA Review (What is Class and Object ?) Class Car …

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

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

  7. Attributes and Methods • Class will contain • a set of attributes and methods • Attribute • State of object generated from the class E.g. name, color, size, etc … • Methods • Behavior of object generated from the class E.g. moveTo(int x, int y), turnLeft(), etc …

  8. Honda Ford TOYOTA Example (Attributes and Methods) Class Car Attribute color, size, name, etc Methods move, turn, stop, etc …

  9. Java Source Code (source file) public class SimpleTurtle { ///// Field (Attributes) ///// … /////// Method /////// … } • Each class is programmed in source file named ClassName.java • For example, SimpleTurtle.java

  10. How to run the program? How do computers understand source codeswritten in Java programming language ? • Basically, there are 3 steps • Edit source code • Compile source code • Execute the compiled code Repeat!

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

  12. 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 MachineCode

  13. Level of programming language Java source code • Human readable language • High-level programming language Java bytecode • Human readable notationof machine code Machine code • Binary string (0 and 1) Example 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$ --> %r3 add %r2, %r3, %r2 ! $x+y$ --> %r2set x, %r1 st %r2, [%r1] ! $x+y$ --> x end: ta 0 10110000 01100001 00110011 1001110001001011 10101001 11100100 00011111 11000010 10011001 11111100 10011010 10101010 00101010 00110100 11010010 10000010 10101010 01010110 00100101 (omit…)

  14. Java bytecode Java compiler Java interpreter Bytecodecomplier Machine code Summary .java file Source code .class file * We can test(in Interaction panel) Execute!

  15. Practice • Let’s write our own method!!!

  16. Edit source code - Open Turtle.java We gonna write Methods Here!!! After this line

  17. Write a method, named drawOne ////////// method ////////// public void drawOne() { }

  18. Write a method, named drawOne ////////// method ////////// public void drawOne() { forward(100); turn(225); forward(40); }

  19. Save source code • Save Turtle.java Note: • File name should be the same as Class name • Extension of the file is .java

  20. Compile it • Right click on the source file Success?

  21. Let’s Test the compiled code • In an interaction panel, create a turtle in the world, then try to call the method drawOne()

  22. Challenge!!! • Write a method for Turtle • The method name is drawSquare • The method draws the one square with size 50

More Related