Java first Session

  • View

  • Download

Embed Size (px)

Text of Java first Session

JavaProgrammingLanguage Session11. Programming concepts and algorithmsa. What is programming: it is the process of writing, testing, debugging/troubleshooting, and maintaining the source code/commands/instructions of computer programs. This source code is written in a programming language e.g. visual basic, C, C++, Pascal, Java. b. What is a programming language: A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks. The term programming language usually refers to high-level languages, such as BASIC, C, C++, COBOL, FORTRAN, Ada, and Pascal. Each language has a unique set of keywords (words that it understands) and a special syntax for organizing program instructions.

High-level programming languages, while simple compared to human languages, are more complex than the languages the computer actually understands, called machine languages. Each different type of CPU has its own unique machine language. Lying between machine languages and high-level languages are languages called assembly languages. Assembly languages are similar to machine languages, but they are much easier to program

in because they allow a programmer to substitute names for numbers. Machine languages consist of numbers only. Lying above high-level languages are languages called fourthgeneration languages (usually abbreviated 4GL). 4GLs are far removed from machine languages and represent the class of computer languages closest to human languages. Regardless of what language you use, you eventually need to convert your program into machine language so that the computer can understand it. There are two ways to do this:

compile the program (explained later) interpret the program (explained later)

c. Kinds of programming Languages: Imperative telling the machine what to do Declarative telling the machine what to achieve

d. What is object oriented programming: a method of implementation in which programs are organized as cooperative collection of objects, each of which represents an instance of a class, and whose classes are all members of a hierarchy of classes

united via inheritance relationships. These objects communicate and interact together E.g. books (science fiction/ adventure/ horror), customer (individual/ retailer), product (CD/ DVD/ HD) . Advantage: easy to create similar objects without having to rewrite the overlapping code, just modify

Four major concepts of OOP

Abstraction: denotes the essential characteristics of an object that distinguish it from all other kinds of objects. This is in variables e.g. name, ID and methods (functions or procedures) used to do a certain operation or calculation. You can add to or modify them. Encapsulation: Encapsulation hides information from unwanted outside access by attaching that information to only methods that need access to it. This binds data and operations tightly together and separates them from external access that may corrupt them intentionally or unintentionally.

Encapsulation is achieved by declaring variables as Private in a class- this gives access to data to only member functions of the class.

Inheritance: Inheritance is the process by which one object acquires the properties of another object Polymorphism: Polymorphism is a term that describes a situation where one name of method may refer to different methods of different implementations. java determines the proper methods to call at the programs run time e.g.

Class Book { String title; String publisher; float price; setBook(String title) {} setBook(String title, String publisher) {} setBook(String title, String publisher,float price){} } e. What are algorithms: a sequence of language independent steps which may be followed to solve a problem

2. Java technologya. What is java:

Simple powerful language

Similar to C++ (both object oriented supporting classes instantiated from objects / similar variable types e.g int, float / case sensitive)

Originated at Sun Microsystems, Inc. in 1991. It was conceived by James Gosling Developed to provide a platform-independent programming language on any system where a Java Virtual Machine is available e.g. Windows 9x/ME/NT/2000/XP etc., MacOS, OS/2, Solaris, LINUX, UNIX and PDAs (Pocket PCs).

b. Applications of java :

Web based applications ecommerce (Java applets small standalone applications e.g. text editor, calculator - can run in a Web browser for many platforms, including Windows, Unix, Mac OS and Linux using a Java Virtual Machine). The client sends an HTTP request to the web server. A web server that implements Java Servlet and JavaServer Pages technology converts the request into an HTTPServletRequest object. This object is delivered to a web component (There are two types of web components: Java Servlets and JavaServer Pages (JSP ) pages. Servlets are Java programming language classes that dynamically process requests and construct responses), which can interact with JavaBeans components or a database to generate dynamic content. The web component can then generate an HTTPServletResponse or it can pass the request to another web component. Eventually a web component generates a HTTPServletResponse object. The web server converts this object to an HTTP response and returns it to the client).

Financial applications (stocks / payroll / banks), Gaming applications (racing cars etc.),

Embedded systems (desktop systems provide a wide spectrum of technologies to serve a broad range of application needs, while embedded devices are fitted with just enough software to handle a specific application.- Microcontroller-Based Temperature Monitoring and Control / Navigation system using a GPS receiver / Communications systems for protocol conversion and VoIP / energy meter reading / air conditioner remote control, audio player remote control, typing speed indicator, database application, electronic voting. Distributed enterprise applications

Mobile applications (cinema repertoire browsing currently displayed movies with reviews, descriptions, photos. Repertoire is updated at users request. Electronic Brochure of Products/Servicesproviding users with information about your services or products). Image processors: helps developers manipulate image pixels using Java 2D API. Processing an image is simply a matter of calculating a new color for each pixel. A platformindependent application to view, edit and print images on any system where a Java Virtual Machine is available

Desktop applications e.g. MagicDraw UML, ArgoUML

c. Java versions

JAVA 1.0 - 1995 JAVA 2 J2SE(standard)) 2006 (J2EE(enterprise), J2ME(micro),

d. Features & Advantages of java Portable - Platform Independent: Unlike many other programming languages when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run. When compiled, the code will run in a similar fashion on any platform. A programmer should be able to write a program one time, compile it one time, and then be able to execute it anywhere. As SUN slogans:Write once, run anywhere Simple: easy to learn Reusable: can use code (classes, objects & methods) all over again dont have to rewrite it

Distributed: With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs Robust: Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory by freeing the memory if the object is no longer needed.Also the concept of Exception Handling allows java to detect errors during runtime and prevent the program from crashing suddenly.

Secure: Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers - Java programs cannot access arbitrary unprotected addresses in memory. High Performance: fast compilation with databases & enterprise Dynamic : interacts information systems EIS

Multi-Threaded: One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program independently and continuously e.g. Multiple Java applets can run on the browser at the same time sharing the CPU time

e. Why use it rather than C/C++

3. Java code

Platform independent Easier not complex to learn

a. Classes & Objects Classes are a blueprints, templates or prototypes from which objects are created. An object is an instance of a class. So 2 dogs but one has long tail the other has short but they both have tails Objects are often used to model the real-world objects that you find in everyday life Objects have state (attributes/characteristics) and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail) Objects interact with each other by sending messages E.g. invoice & receipt e.g. lego objects form larger objects together

b. Methods

An object stores its state in fields (variables in some programming languages) and exposes its behavior and interaction with the outer world through methods (functions in some programming languages). Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation (e.g. when assembling a pc, each object- card, processor etc.- is a self contained unit, and all you are interested in is how the units interact with each other not how each one works alone)

c. How to write Java:

Enter the program source code in a data file using an editor e.g. JCreator / IBM Websphere etc.