Road to Object Oriented Programming Advanced programming ce244 Sharif University of Technology Feb 2007 Sunday, February 18, 2007 Taha Abachi 1 Outline Deadlines Objects Object Oriented Languages Editing your first java program Compiling, running and debugging Homework 6/28/2016 Taha Abachi 2 Deadlines By the end of the week you should have : The Java CD Java installed on your machines Edited, run, debugged your first Java programs without any trouble Examined Java IDEs 6/28/2016 Taha Abachi 3 Objects Abstraction Programming languages 2. Problem space 1. Definition State : internal data 2. Behavior : methods 3. Identity : each object can be uniquely distinguished from every other object 1. 6/28/2016 Taha Abachi 4 Features of an Object An object has an interface An object provides services The hidden implementation Data encapsulation Reusing the implementation Inheritance:reusing the interface 6/28/2016 Taha Abachi 5 Features of an Object (cont’d) Sample object Date : Day : 1..31 Month : 1..12 Year : 1300..1500 Problems Inconsistent data: no dependency to other fields Inflexible representation 6/28/2016 Day := 32 Date : longint Date := 28351 days passed after a specific date Taha Abachi 6 Features of an Object (cont’d) Clients of the code Yourself Teammates Customers An infant programmer Solution : Separating implementation form interface 6/28/2016 More Secure and flexible Taha Abachi 7 Object Oriented Languages 1. 2. 3. 4. 5. Everything is an object. A program is a bunch of objects telling each other what to do by sending messages. Each object has its own memory made up of other objects. Every object has a type. All objects of a particular type can receive the same messages. 6/28/2016 Taha Abachi 8 Installing Java Install JDK Install Java Documentation API(Application programming Interface) Set Environment variables Choose 6/28/2016 a text editor Taha Abachi 9 Java Characteristics Portability : compile programs to a intermediary format called JBC C:\>javac HelloWorld.java to produce HelloWorld.class Interpreting : JVM 6/28/2016 Taha Abachi 10 Portability 6/28/2016 Taha Abachi 11 Java is Object Oriented Every thing in Java is an Object. Objects in OOP mirror the properties and behavior of the real world objects. They have their own set of data and functions (called method) that operate on these data. 6/28/2016 Taha Abachi 12 First Program in Java Use a text editor Write down your program Java is case sensitive Compile your program Run 6/28/2016 Taha Abachi 13 HelloWorld public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } 6/28/2016 Taha Abachi 14 Java program files Each class is saved in a file of the same name with java extension. Note java is case sensitive language, even as far as the class file names are concerned. The Hello class should be saved in a file called HelloWorld.java class . 6/28/2016 Taha Abachi 15 Syntax Highlighting public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } 6/28/2016 Taha Abachi 16 The method main ( ) The main() method is the point where a Java program starts running. Any executable Java class must have the main() method in this specific format: public static void main(String[ ] args){ ………….. ………….. } 6/28/2016 Taha Abachi 17 Time to compile Use javac command to compile your java programms : C:\> javac HelloWorld.java HelloWorld.class (JBC file) is created You may face some errors Correct erroneous parts and compile again 6/28/2016 Taha Abachi 18 Run The Java command is used to run the class (JBC) file: c:\> Java HelloWorld 6/28/2016 Taha Abachi 19 Congratulations You are a Java programmer NOW! 6/28/2016 Taha Abachi 20 Java Development Environments Sun's JDK is not an environment. Sun's Java Workshop Microsoft's Visual J++ Symantec's Cafe Borland's J Builder Metrowerks' Code Warrior Tek-Tools' Kawa. 6/28/2016 Taha Abachi 21 Java Development Environments My candidates : IntelliJ IDEA Eclipse Textpad 6/28/2016 Taha Abachi 22 Homework Install Java properly Write and Test you first Java program Design an object oriented environment for manipulating geometrical shapes : Different subtypes (square, circle, etc.) Common characteristics (fields & methods) Particular characteristics Use UML (Unified Modeling Language) notation 6/28/2016 Taha Abachi 23 Questions? 6/28/2016 Taha Abachi 24