ObjectOriented Software Development ObjectOriented models are composed of

Object-Oriented Software Development • Object-Oriented models are composed of objects. • Objects contain data and make computations. • The decomposition of a complex system is based on the structure of classes, objects, and the relationship among them. (divide-and-conquer). • When we divide our problems into sub-problems, we will try to design classes to solve these sub-problems. • We will use graphical notation to describe object-oriented analysis and design models. This notation is based on Unified Language Modelling (UML). Spring 2003 COP 3330 Object Oriented Programming 1

Classes and Objects • Objects and classes are two fundamental concepts in the object-oriented software development. • An object has a unique identity, a state, and behaviors. In the real life, an object is anything that can be distinctly identified. • A class characterizes the structure of states and behaviors that shared by all its instances. • The terms object and instance are often interchangeable. • The features of an object is the combination of the state and behaviors of that object. – The state of an object is composed of a set of attributes (fields) and their current values. – The behavior of an object is defined by a set of methods (operations, functions, procedures). • A class is a template for its instances. Instead of defining the features of objects, we define features of the classes to which these objects belong. Spring 2003 COP 3330 Object Oriented Programming 2

Classes in Java • A class in Java can be defined as follows: class Rectangle { int length, width; public int area() {………} public void change. Sizes(int x, int y) { ……… } • • The name of the class is: Rectangle Its attributes are: length width Its methods are: area change. Sizes This Rectangle class is a template for all rectangle objects. All instances of this class will have same structure. Spring 2003 COP 3330 Object Oriented Programming 3

Objects in Java • An object in Java is created from a class using new operator. Rectangle r 1 = new Rectangle(); length width r 1 Rectangle r 2 = new Rectangle(); length width Spring 2003 r 2 COP 3330 Object Oriented Programming 4

Graphical Representation of Classes Class. Name is the name of the class field 1 … field 2 Each field is [Visibility][Type] identifier [=initialvalue] method 1 … methodm Ex: Each method is [Visibility][Type] identifier ( [parameter-list] ) Rectangle int length int width • We may not give field, methods parts public int area () public void change. Sizes (int x, int y) Spring 2003 COP 3330 Object Oriented Programming 5

Graphical Representations of Objects object. Name: Class. Name field 1 = value 1 … fieldn =valuen r 1: Rectangle length = 20 width = 10 r 2: Rectangle length = 40 width = 30 Spring 2003 • We may omit Class. Name, and just use object. Name. In this case the class of the object is no interest for us. • We may omit object. Name, and just use : Class. Name. In this case, the object is an anonymous object. Rectangle r 1 = new Rectangle(); r 1. length = 20; r 1. width = 10; Rectangle r 2 = new Rectangle(); r 2. length = 40; r 2. width = 30; COP 3330 Object Oriented Programming 6

Java Execution Model • Java execution model compromises between conventional compilation and interpretation approaches. • Java programs are compiled into Java byte-codes (Machine Codes of Java Virtual Machine). These Java byte-codes are independent from machine codes of any architecture. But they are close to machine codes. • These generated Java byte-codes are interpreted by Java interpreters available in different platforms. • So, the generated byte-codes are portable among different systems. • The execution of Java programs are slower because we still use the interpretation approach. Spring 2003 COP 3330 Object Oriented Programming 7

Class versus Object • A class is an object template and factory • It contains the design of an object (class instance) • It contains a way to generate these instances • An object is an instance of a class • All objects that are identical except for their state during execution are grouped together in a class • All instances of a class have the same members but those members take on different values for each instance Spring 2003 COP 3330 Object Oriented Programming 8

Java Attributes • Run on many platforms – Unix, Windows, Mac • Develop on multiple platforms – The usual suspects • Reuse at many levels – Components – Classes (families of components) – Frameworks (collaborating components) Spring 2003 COP 3330 Object Oriented Programming 9

How is Java Portable? • Java source code (. java) is compiled for a JVM – JVM = Java virtual machine • Byte code files (. class) run on the JVM via – Machine emulation on local host – Just-in-time compilation from byte to native codes – An actual silicon implementation of the JVM Spring 2003 COP 3330 Object Oriented Programming 10

Applications versus Applets • Java applications run like any other program, with all the rights and privileges thereto appertaining • Applets run in a sandbox, Internet browser or applet viewer – The sand box provides services, but limits what applet can do • No reading or writing files on local machine (just from host) • No network connections, except back to host • No inter-applet communication except to others from same host • JVM enforces byte-code verification, security management • Java language does not allow pointer manipulation Spring 2003 COP 3330 Object Oriented Programming 11

Preparing a Java Program • We are going to use JDK environment of Sun Microsystems. • JDK environment is simple to use and free. • You can JDK environment for your own computer from: the sun website http: //java. sun. com/j 2 se/1. 3 • Editing: – Create a file containing a Java program. – You may use any text editor. – This file will be. java extension (Ex: Test 1. java ) • Compiling: – Use Java compiler to compile the Java program. ( javac Test 1. java ) – Java compiler will create a file with. class extension. This file will contain the Java bytecodes of your Java program ( Test 1. class ). You can run this file in different platforms. – The other compilers produce executable files. Spring 2003 COP 3330 Object Oriented Programming 12

Preparing a Java Program (cont. ) • Executing: – Execute the byte-codes of your Java program by a Java interpreter. – We will see that there are two types of Java programs: application, applet – If our program is an application, we will execute it as follows: java Test 1 (will interpret Test 1. class file ) – If our program is an applet: * First we will create a. html file containing a link to our. class file. * Then we will run our applet using appletviewer: appletviewer Test 1. html Spring 2003 COP 3330 Object Oriented Programming * We may run our applets under web-browsers too. 13

A Simple Console Application Program // Author: Ilyas Cicekli Date: October 9, 2001 // A simple application program which prints “Hello, World” public class Test 1 { public static void main(String args[]){ // print “Hello, World” System. out. println("Hello, World”); } // end of main } // end of class Spring 2003 COP 3330 Object Oriented Programming 14

Java data types • Primitive data types • integers (byte, short, int, long) • floating point numbers (float, double) • boolean (true, false) • char (any symbol encoded by a 16 -bit unicode) • Objects- everything else An object is defined by a class. A class is the data type of the object. You can define your own objects or use predefined classes from library. Spring 2003 COP 3330 Object Oriented Programming 15

Structure of an Applet Program • imported classes – you should import at least Graphics and Applet classes public class <your class name> extends Applet { – declarations • you should declare all variables which will be used in your methods – declarations of methods in your application • Declarations of your own methods and the methods responding to events. • If a required method is needed but it is not declared, it is inherited from Applet class. Normally the free versions we get from Applet class. } Spring 2003 COP 3330 Object Oriented Programming 16

Structure of a Console Application Program imported classes – You should at least import classes in java. io package. public class <your application name> { public static void main (String args[]) throws IOException { declarations of local variables and local objects (references) executable statements } other methods if they exist } Remember the file name should be < your application name>. java Spring 2003 COP 3330 Object Oriented Programming 17

Java Widening Conversions • In widening conversions, they often go from one type to another type uses more space to store the value. • In most widening conversions, we do not loose information. – we may loose information in the following widening conversions: • int float Spring 2003 long float long double From byte To short, int, long, float, double short int, long, float, double char int, long, float, double int long, float, double long float, double float double COP 3330 Object Oriented Programming 18

Input and Output (in Console Applications) • Java I/O is based on input and output streams • There are pre-defined standard streams – System. in – System. out – System. err reading input writing output (for errors) keyboard monitor (Input. Stream object) (Print. Stream object) • print and println methods (defined in Print. Stream class) are used to write to the standard output stream (System. out). • We will get the inputs from the standard input stream (System. in). • To read character strings, we will create a more useful object of Buffered. Reader class from System. in. Buffered. Reader stdin = new Buffered. Reader(new Input. Stream. Reader(System. in)); Spring 2003 COP 3330 Object Oriented Programming 19

Reserved Words • Reserved words are identifiers that have a special meaning in a programming language. • For example, – public, void, class, static are reserved words in our simple programs. • In Java, all reserved words are lower case identifiers (Of course we can use just lower case letters for our own identifiers too) • We cannot use the reserved words as our own identifiers (i. e. we cannot use them as variables, class names, and method names). Spring 2003 COP 3330 Object Oriented Programming 20

Java reserved words • Data declaration: boolean, float, int, char • Loop keywords: for, while, continue • Conditional keywords: if, else, switch • Exceptional keywords: try, throw, catch • Structure keywords: class, extends, implements • Modifier and access keywords: public, private, protected • Miscellaneous: true, null, super, this Spring 2003 COP 3330 Object Oriented Programming 21

Another Simple Console Application Program public class Test 2 { public static void main(String args[]){ // print the city and its population. System. out. println("The name of the city is ” + “Orlando”); System. out. println(“Its population is “ + 1000000); // Different usage of + operator System. out. println(“Sum of 5+4: “ + (5+4)); // Different output method – print System. out. print(“one. . ”); System. out. print(“two. . ”); System. out. println(“three. . ”); System. out. print(“four. . ”); } // end of main } // end of class Spring 2003 COP 3330 Object Oriented Programming 22

Life cycle methods for an applet l l 23 init – put code here that should be executed only once in the applet’s lifetime; this method is called when the applet is first encountered start – put code here to start your applet; called right after init or when user revisits page stop – put code here to stop your applet; called when user leaves page containing applet destroy – put code here to relinquish resources; many don’t even use this method COP 3330 Object Oriented Programming Spring 2003

Examples of life cycle code l l 24 init – load images start – start an animation (usually with separate thread) stop – stop animation (by stopping the thread) destroy – close network connections COP 3330 Object Oriented Programming Spring 2003

Methods • A class contains methods. • A method is a group of statements that are given a name. • Each method will be associated with a particular class (or with an instance of that class). • We may define methods and invoke them with different parameters. When its parameters are different, their behavior will be different. Spring 2003 COP 3330 Object Oriented Programming 25

Methods • A method declaration specifies a block of statements, that gets executed when the method is invoked. • A method either returns some value (of some data type), or is declared void ( exception? ) • A method that returns value must have a return statement. • A void method may have a return statement without an expression. Spring 2003 COP 3330 Object Oriented Programming 26

Creating Objects class C { // fields private int x; private double y; // constructors public C() { x=1; y=2. 2; } // methods public void m 1 (int val) { x=val; } public void m 2 (double val) { y=val; } COP 3330 Object Oriented Programming Spring 2003 } 27

Creating Objects (cont. ) • In some other class, we may create the objects of the class C. (If we want, we can also create the objects of C in C too). public class C 2 {. . main (. . . ) { C obj 1, obj 2; obj 1 = new C(); obj 2 = new C(); }. Spring 2003 COP 3330 Object Oriented Programming x 1 y 2. 2 x obj 1 1 y 2. 2 28

Accessibility Modifiers for Class Members (cont. ) Spring 2003 public private protected The class itself yes yes Classes in the same package yes no yes Sub-classes in a different package yes no Non-subclasses in a different package yes no no no COP 3330 Object Oriented Programming package 29
- Slides: 29