Object-Oriented Software Development - PowerPoint PPT Presentation

About This Presentation
Title:

Object-Oriented Software Development

Description:

[Visibility][Type] identifier ( [parameter-list] ) public int area ... Execute the byte-codes of your Java program by a Java interpreter. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 30
Provided by: IBMU362
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Object-Oriented Software Development


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

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

3
Classes in Java
  • A class in Java can be defined as follows
  • class Rectangle
  • int length, width
  • public int area()
  • public void changeSizes(int x, int y)
  • The name of the class is Rectangle
  • Its attributes are length width
  • Its methods are area changeSizes
  • This Rectangle class is a template for all
    rectangle objects. All instances of this class
    will have same structure.

4
Objects in Java
  • An object in Java is created from a class using
    new operator.
  • Rectangle r1 new Rectangle()
  • length
    r1
  • width
  • Rectangle r2 new Rectangle()
  • length
    r2
  • width

5
Graphical Representation of Classes
ClassName is the name of the class
ClassName
field1 field2
method1 methodm
Each field is VisibilityType identifier
initialvalue
Each method is VisibilityType
identifier ( parameter-list )
Rectangle
int length int width
public int area () public void changeSizes (int x, int y)
Ex
  • We may not give field,
  • methods parts

6
Graphical Representations of Objects
  • We may omit ClassName, and just use objectName.
  • In this case the class of the object is no
    interest for us.
  • We may omit objectName, and just use ClassName.
  • In this case, the object is an anonymous object.

objectNameClassName
field1 value1 fieldn valuen
Rectangle r1 new Rectangle() r1.length
20 r1.width 10
r1Rectangle
length 20 width 10
r2Rectangle
length 40 width 30
Rectangle r2 new Rectangle() r2.length
40 r2.width 30
7
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.

8
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

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

10
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

11
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

12
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/j2se/1.3
     
  • Editing
  • Create a file containing a Java program.
  • You may use any text editor.
  • This file will be .java extension (Ex
    Test1.java )
  • Compiling
  • Use Java compiler to compile the Java program. (
    javac Test1.java )
  • Java compiler will create a file with .class
    extension. This file will contain the Java
    byte-codes of your Java program ( Test1.class ).
    You can run this file in different platforms.
  • The other compilers produce executable files.

13
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 Test1 (will interpret Test1.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 Test1.html
  • We may run our applets under web-browsers
    too.

14
A Simple Console Application Program
  • // Author Ilyas Cicekli Date October 9,
    2001
  • // A simple application program which prints
    Hello, World
  • public class Test1
  • public static void main(String args)
  • // print Hello, World
  • System.out.println("Hello, World)
  • // end of main
  •  
  • // end of class

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

16
Structure of an Applet Program
  • imported classes
  • you should import at least Graphics and Applet
    classes
  • public class ltyour class namegt 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.

17
Structure of a Console Application Program
  • imported classes
  • You should at least import classes in java.io
    package.
  • public class ltyour application namegt
  •  
  • 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 lt your
    application namegt.java

18
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 long ? float long ? double

From To
byte short, int, long, float, double
short int, long, float, double
char int, long, float, double
int long, float, double
long float, double
float double
19
Input and Output (in Console Applications)
  • Java I/O is based on input and output streams
  • There are pre-defined standard streams
  • System.in reading input keyboard
    (InputStream object)
  • System.out writing output monitor
    (PrintStream object)
  • System.err writing output (for errors) monitor
    (PrintStream object)
  • print and println methods (defined in PrintStream
    class) are used to write to the 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 BufferedReader class from
    System.in.
  • BufferedReader stdin
  • new BufferedReader(new InputStreamReader(System.i
    n))

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

21
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

22
Another Simple Console Application Program
  • public class Test2
  •  
  • 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 54 (54))
  •  
  • // 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

23
Life cycle methods for an applet
  • init put code here that should be executed only
    once in the applets 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 dont even use this method

24
Examples of life cycle code
  • init load images
  • start start an animation (usually with separate
    thread)
  • stop stop animation (by stopping the thread)
  • destroy close network connections

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

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

27
Creating Objects
  • class C
  • // fields
  • private int x
  • private double y
  • // constructors
  • public C() x1 y2.2
  • // methods
  • public void m1 (int val) xval
  • public void m2 (double val) yval
  • The constructor method must have the same name as
    the class, and it does not have any return type
    (not even void).
  • Variables x and y are instance-variables, and
    they can be seen only methods of this class.

28
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 C2
  • .... main (...)
  • C obj1, obj2
  • obj1 new C()
  • obj2 new C()
  • .
  • .

x
obj1
1
y
2.2
obj2
1
x
y
2.2
29
Accessibility Modifiers for Class Members (cont.)

public private protected package
The class itself yes yes yes yes
Classes in the same package yes no yes yes
Sub-classes in a different package yes no yes no
Non-subclasses in a different package yes no no no
Write a Comment
User Comments (0)
About PowerShow.com