Javalecture 1

3,671 views

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,671
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
265
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Javalecture 1

  1. 1. Object Oriented Programming with Java
  2. 2. Topic <ul><li>Lecture 1 </li></ul><ul><li>C++ vs. Java </li></ul><ul><li>Fundamentals of Java </li></ul><ul><li>Java Application and Applet </li></ul>
  3. 3. C++ vs. Java <ul><li>Area of Application </li></ul><ul><ul><li>C++ is best suitable for developing large software </li></ul></ul><ul><ul><ul><li>Library management system, GIS etc. </li></ul></ul></ul><ul><ul><li>Java is best suitable for developing Internet application software </li></ul></ul><ul><ul><ul><li>Network Protocols, Internet programs, web page, web browser etc. </li></ul></ul></ul>
  4. 4. C++ vs. Java <ul><li>Programming Features </li></ul>√ × API √ × Interface and Package × √ Pointers × √ Header files × √ Global variables × √ Template classes × √ Operator overloading × √ Multiple Inheritance √ √ Single Inheritance Inheritance √ √ Dynamic √ √ Static Binding √ √ Polymorphism √ √ Data abstraction and encapsulation in Java in C++ Features
  5. 5. C++ vs. Java <ul><li>Programming Environment </li></ul><ul><ul><li>C++ provides platform dependent programming </li></ul></ul><ul><ul><li>Java provides platform independent programming </li></ul></ul>
  6. 6. Fundamentals of Java <ul><li>Java developed by Sun </li></ul><ul><li>Sun describes Java as: </li></ul><ul><ul><li>Simple, Object-oriented, Distributed, Interpreted, Robust, Secure, Architecture neutral, Portable, High-performance, Multithreaded, and Dynamic Language </li></ul></ul><ul><li>Java is touted as </li></ul><ul><ul><li>Web-savvy programming language </li></ul></ul><ul><ul><li>Language for Internet Programming </li></ul></ul><ul><ul><li>Better C++ </li></ul></ul><ul><ul><ul><li>Without difficulties and bug commonly encountered in C, C++ programming languages </li></ul></ul></ul>
  7. 7. Tools Available for Java Programming <ul><li>Java Developer’s Kit (JDK) </li></ul><ul><ul><li>JDK from JavaSoft a division of Sun Microsystems Inc. </li></ul></ul><ul><ul><li>Contains the basic tools and libraries necessary for creating, testing, documenting and executing Java programs </li></ul></ul><ul><ul><li>There are seven main programs in JDK </li></ul></ul><ul><ul><ul><li>javac – the Java Compiler </li></ul></ul></ul><ul><ul><ul><li>java – the Java Interpreter </li></ul></ul></ul><ul><ul><ul><li>javadoc – generates documentation in HTML </li></ul></ul></ul>
  8. 8. Tools Available for Java Programming <ul><ul><li>Main programs in JDK (contd.) </li></ul></ul><ul><ul><ul><li>appletviewer – the Java Interpreter to execute Java applets </li></ul></ul></ul><ul><ul><ul><li>jdb – the Java Debugger to find and fix bugs in Java programs </li></ul></ul></ul><ul><ul><ul><li>javap – the Java Disassembler to displays the accessible functions and data in a compiled class; it also displays the meaning of byte codes </li></ul></ul></ul><ul><ul><ul><li>javah – to create interface between Java and C routines </li></ul></ul></ul>
  9. 9. Tools Available for Java Programming <ul><li>Packages in JDK </li></ul><ul><ul><li>API – the Application Programming Interface enables Java programmers to develop varieties of applets and applications </li></ul></ul><ul><ul><li>It contains six packages: </li></ul></ul><ul><ul><ul><li>java.applet – for applet programming </li></ul></ul></ul><ul><ul><ul><li>java.awt – the Abstract Windowing Toolkit for designing GUI like Button , Checkbox , Choice , Menu , Pannel etc. </li></ul></ul></ul><ul><ul><ul><li>java.io – file input/output handling </li></ul></ul></ul>
  10. 10. Tools Available for Java Programming <ul><ul><li>API in Java (contd.) </li></ul></ul><ul><ul><ul><li>java.lang – provides useful classes like to handle Object , Thread , Exception , String , System , Math , Float , Integer etc. </li></ul></ul></ul><ul><ul><ul><li>java.net – classes for network programming; supports TCP/IP networking protocols </li></ul></ul></ul><ul><ul><ul><li>java.util – it contains miscellaneous classes like Vector , Stack , List, Date , Dictionary , Hash etc. </li></ul></ul></ul><ul><ul><li>JDK is a free software and can be downloaded from JavaSoft’s web site at http://java.sun.com </li></ul></ul>
  11. 11. Third Part Tools for Java Programming <ul><li>Web browser </li></ul><ul><ul><li>Web browser in a client machine connects a link to a web site, download web page from it and then executes </li></ul></ul><ul><ul><li>Java environment requires Java-enabled web browser to supports Java applets </li></ul></ul><ul><ul><li>Few (free) popular Java-enabled web browsers: </li></ul></ul><ul><ul><ul><li>HotJava from JavaSoft web site ( http://java.sun.com ) </li></ul></ul></ul><ul><ul><ul><li>Netscape Navigator from Netscape home page ( http:// home.nescape.com ) </li></ul></ul></ul><ul><ul><ul><li>Internet Explorer from Microsoft’s web page ( http://www.microsoft.com ) </li></ul></ul></ul>
  12. 12. Other Third Part Tools <ul><li>Java IDE </li></ul><ul><ul><li>Number of IDEs are available to support the productivity of software development </li></ul></ul><ul><ul><li>Few important of them are: </li></ul></ul><ul><ul><ul><li>Sun’s Java Workshop dev 5 from Sun’s JavaSoft (recently powered with Visual Java) </li></ul></ul></ul><ul><ul><ul><li>Mojo from Penumbra Software (best visual environment for creating Java applets) </li></ul></ul></ul><ul><ul><ul><li>Jumba from Aimtech and IBM (graphical applet builder) </li></ul></ul></ul><ul><ul><ul><li>Semantic Café from Semantics (a de-facto standard for Java development on Windows systems) </li></ul></ul></ul>
  13. 13. Programming in Java <ul><li>Java programs are available in two flavors: </li></ul><ul><ul><li>Applet </li></ul></ul><ul><ul><ul><li>A java applet is a program that appears embedded in a web document and applet come into effect when the browser browse the web page </li></ul></ul></ul><ul><ul><li>Application </li></ul></ul><ul><ul><ul><li>It is similar to all other kind of programs like in C, C++ to solve a particular problem </li></ul></ul></ul><ul><li>In the subsequent discussions we will learn how to manage these two types of Java programs </li></ul>
  14. 14. Building a Java Application <ul><li>Our first Java program is a simple Application to print a message on the screen. </li></ul><ul><li>Let us consider the following Application: </li></ul><ul><li>// Hello Java Application // </li></ul><ul><li>class HelloWorldApp { </li></ul><ul><li> public static void main ( String args[] ) { </li></ul><ul><li> System.out.println (&quot; Hello Java ! &quot;); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>
  15. 15. How to edit this program? <ul><li>Any text editor can be used to write Java programs. For example, </li></ul><ul><ul><li>In Windows </li></ul></ul><ul><ul><ul><li>Notepad, EDIT etc. </li></ul></ul></ul><ul><ul><li>In Unix </li></ul></ul><ul><ul><ul><li>vi , emacs etc. </li></ul></ul></ul><ul><li>Save the Application </li></ul><ul><ul><li>Save the Application in a file with the name </li></ul></ul><ul><ul><li>HelloWorldApp.java </li></ul></ul>
  16. 16. How to compile this program? <ul><li>The Java compiler ( javac ) converts a Java Application into Java byte code. </li></ul><ul><ul><li>Open a DOS shell (Windows or NT) or Terminal (Unix) </li></ul></ul><ul><ul><li>Move to the directory where your Java program has been saved </li></ul></ul><ul><ul><li>Enter the following command to compile: </li></ul></ul><ul><ul><li> javac HelloWorldApp.java </li></ul></ul>
  17. 17. How to compile this program? <ul><li>After the successful compilation, Java byte code will be produced which will be automatically stored in the same directory but with file name having extension .class </li></ul><ul><li>For the running example, the class filename will be </li></ul><ul><li>HelloWorldApp .class </li></ul>
  18. 18. How to execute this program? <ul><li>To execute the Java Application, type the command java (from the command prompt). </li></ul><ul><li>For example, for the current example HelloWorldApp Application can be execured as </li></ul><ul><li>java HelloWorldApp </li></ul><ul><li>Wait! Let’s recapitulate whole things once again!! </li></ul>
  19. 19. Building a Java Application
  20. 20. Building a Java Application Step 1: Edit and Save
  21. 21. Building a Java Application Step 1: Edit and Save
  22. 22. Building a Java Application Step 1: Edit and Save
  23. 23. Building a Java Application Step 1: Edit and Save
  24. 24. Building a Java Application Step 2: Compile
  25. 25. Building a Java Application Step 2: Compile
  26. 26. Building a Java Application Step 3: Execute
  27. 27. Building a Java Applet <ul><li>Suppose, we want to build an applet which will look like the following: </li></ul>
  28. 28. Building a Java Applet <ul><li>Following piece of code is required: </li></ul><ul><li>// An applet to print Hello World! // </li></ul><ul><li>import java.awt.Graphics; </li></ul><ul><li>import java.applet.Applet; </li></ul><ul><li>public class HelloWorld extends Applet { </li></ul><ul><li> public void paint (Graphics g ) { </li></ul><ul><li>g.drawString(&quot;Hello World!&quot; 50, 25); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>
  29. 29. Building a Java Applet Edit -> Save -> Compile <ul><li>Edit the code in the same fashion as an Application </li></ul><ul><li>The name of the applet will be same as the public class, here </li></ul><ul><li>HelloWorld.java </li></ul><ul><li>The program can be compiled in the same fashion as an Application is compiled. That is, </li></ul><ul><li>javac HelloWorld.java </li></ul><ul><li>After successful compilation, t he javac will produce a file named </li></ul><ul><li>HelloWorld.class </li></ul>
  30. 30. Building a Java Applet Execution <ul><li>Edit an HTML file to host the applet just created. The HTML file will look like as: </li></ul><ul><li>Save this to file giving a file name HelloJava.html </li></ul><ul><li>Note: The name of the file not necessary be the same as the name of the class; But extension should be same as the .html </li></ul><ul><li>Now the applet is ready for its execution! </li></ul><ul><li>To run with appletviewer type the following: </li></ul><ul><li> appletviewer HelloJava.html </li></ul><applet code = HelloJava.class width = 200 height = 100> </applet>
  31. 31. More on Java Application Structure of a Java Application <ul><li>Let us analyze the different components in the HelloWorldApp.java </li></ul><ul><li>class </li></ul><ul><li>public, static, void, main </li></ul><ul><li>String args[ ] </li></ul><ul><li>System.out.println </li></ul>// Hello Java Application // class HelloWorldApp { public static void main ( String args[ ] ) { System.out.println (&quot;Hello Java !&quot;); } }
  32. 32. General Structure of an Application
  33. 33. Example: Square Root Calculation /* * One more simple Java Application * * This application computes square root * */ // This is also a comment (one line comment) import java.lang.Math; class SquareRoot { public static void main (String args[ ]) { double x = 45; // Variable declaration and initialization double y; // Declaration of another variable y = Math.sqrt (x); System.out.println(&quot;Square root of &quot;+ x +&quot;=&quot; + y); } }
  34. 34. Application with Multiple Classes // Application with more than one classes // class FirstClass { intidNo; iIdNo = 555; public static void print( ) { System.out.println ( &quot; First Class citizen&quot; + idNo ); } } class SecondClass { int idNo; idNo = 111; public startic void print( ) { System.out.println ( &quot; Second Class citizen &quot; + idNo) ; } }
  35. 35. Application with Multiple Classes (contd..) public class PeopleAppln { FirstClass female; SecondClass male; public static void main( String args[ ] ) { System.out.print(&quot;People from Java World&quot;); female.print( ); male.print( ); } }
  36. 36. Application without any Class! // Edit the following program as HelloNoClass.java public static void main (String args[ ] ) { System.out.println( &quot;Hello Classless Java!]); } Type following two commands to run the Hello.java Application : javac HelloNoClass.java // To compile java HelloNoClass // To run the program
  37. 37. Communication to Java Application <ul><li>How input can be passed to an Application while it is running? </li></ul><ul><li>Java provides two methods for it </li></ul><ul><ul><li>Using the command line arguments </li></ul></ul><ul><ul><li>Using the DataInputStream class </li></ul></ul>
  38. 38. Command Line Arguments <ul><li>class CommnadLineInputTest </li></ul><ul><li>{ </li></ul><ul><li> public static void main(String args[ ] ) { </li></ul><ul><li> int count; </li></ul><ul><li>String aString; </li></ul><ul><li>count = args.length; </li></ul><ul><li>System.out.println( &quot;Number of arguments = “ + count); </li></ul><ul><li>for(int i = 0; i < count; i++) { </li></ul><ul><li> aString = args[0]; </li></ul><ul><li>System.out.println( &quot;args[“ + I + &quot;]“ + &quot;=“ + aString); </li></ul><ul><li>} </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>
  39. 39. Get Input using DataInputStream
  40. 40. Get Input using DataInputStream Calculator Program import java.io.*; class InterestCalculator { public static void main(String args[ ] ) { Float principalAmount = new Float(0); Float rateOfInterest = new Float(0); int numberOfYears = 0; DataInputStream in = new DataInputStream(System.in); String tempString; System.out.print(&quot;Enter Principal Amount: &quot;); System.out.flush(); tempString = in.readLine(); principalAmount = Float.valueOf(tempString);
  41. 41. Calculator Program (contd..) System.out.print(&quot;Enter Rate of Interest: &quot;); System.out.flush(); tempString = in.readLine(); rateOfInterest = Float.valueOf(tempString); System.out.print(&quot;Enter Number of Years: &quot;); System.out.flush(); tempString = in.readLine(); numberOfYears = Integer.parseInt(tempString); // Input is over: calculate the interest int interestTotal = principalAmount*rateOfInterest*numberOfYears; System.out.println(&quot;Total Interest = &quot; + interestTotal); } }
  42. 42. Applet Revisited <ul><li>import java.awt.Graphics; </li></ul><ul><li>import java.applet.Applet; </li></ul><ul><li>public class HelloWorld extends Applet { </li></ul><ul><li> public void paint (Graphics g ) { </li></ul><ul><li>g.drawString(&quot;Hello World!&quot; 50, 25); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>
  43. 43. Structure of an Applet
  44. 44. Basic Methods in Applet <ul><li>public void init( ) </li></ul><ul><ul><li>To initialize or pass input to an applet </li></ul></ul><ul><li>public void start( ) </li></ul><ul><ul><li>The start( ) method called after the init( ) method, starts an applet </li></ul></ul><ul><li>public void stop( ) </li></ul><ul><ul><li>To stop a running applet </li></ul></ul><ul><li>public void paint (Graphics g) </li></ul><ul><ul><li>To draw something within an applet </li></ul></ul><ul><li>public void destroy( ) </li></ul><ul><ul><li>To remove an applet from memory completely </li></ul></ul>
  45. 45. Example: Use of init( ) // Use of init( ) method in an applet // import java.awt .Graphics ; import java.applet.Applet; public class HelloWorld extends Applet { public void init( ) { resize(200,200); } public void paint (Graphics g ) { g.drawString ( &quot; Hello World !&quot;, 50, 25 ); } }
  46. 46. One More Example: Use of init( ) // Use of init( ) to pass value through HTML to applet // import java.awt . *; import java.applet. * ; public class RectangleTest extends applet { int x, y, w, h; public void init ( ) { x = Integer.parseInt(get Parameter (&quot; xValue&quot; )); y = Integer.parseInt(get Parameter (&quot; yValue&quot; )); w = Integer.parseInt(get Parameter (&quot; wValue&quot; )); h = Integer.parseInt(get Parameter (&quot; hValue&quot; )); } public void paint ( Graphics g ) { g.drawRect (x, y, w, h ); } }
  47. 47. One More Example: Use of init( ) Corresponding HTML document containing this applet and providing parameter values will be : < applet code = &quot; RectangleTest&quot; width = 150 height = 100 > < param name = xValue value = 20 > < param name = yValue value = 40 > <param name = wValue value = 100> < param name = hValue value = 50 > < /applet >
  48. 48. Application vs. Applet <ul><li>Applets do not use main() method for initiating the execution of code. Applets, when loaded, automatically call certain methods of Applet class to start and execute the code in Applets </li></ul><ul><li>Unlike Application (stand alone), applets cannot be run independently . They are to be embedded in HTML pages as applet code, which browser can run </li></ul><ul><li>Applet cannot read from or write to the file in the local computers </li></ul><ul><li>Applet cannot communicate with other severs in the networks </li></ul><ul><li>Applet cannot run any program from local computers </li></ul><ul><li>Applets are restricted from using libraries from other languages, such as, C, C++. </li></ul><ul><li>Why applets are designed with so many restrictions?? </li></ul>

×