Você está na página 1de 48

Object Oriented Programming

with Java
Topic
Lecture 1

C++ vs. Java

Fundamentals of Java

Java Application and Applet

08.12.21 M. Venugopal, OU, Hyd 2


C++ vs. Java
 Area of Application
– C++ is best suitable for developing large
software
 Library management system, GIS etc.
– Java is best suitable for developing Internet
application software
 Network Protocols, Internet programs, web page,
web browser etc.

08.12.21 M. Venugopal, OU, Hyd 3


C++ vs. Java
 Programming Features
Features in C++ in Java

Data abstraction and encapsulation √ √


Polymorphism √ √
Static √ √
Binding
Dynamic √ √
Single Inheritance √ √
Inheritance
Multiple Inheritance √ ×

Operator overloading √ ×
Template classes √ ×
Global variables √ ×
Header files √ ×
Pointers √ ×
Interface and Package × √
API × √

08.12.21 M. Venugopal, OU, Hyd 4


C++ vs. Java
J a v a s o u rc e

 Programming code

Environment Java
c o m p ile r

– C++ provides platform


dependent Java B YTE
C++
C o m p lie r

programming C + + s o u rc e
code
code C J+ +V oMb j e c t
code

– Java provides platform


Java Java Java

independent in te r p r te r
W IN 3 2
in te r p r te r
S o la r is
in te r p r te r
M a c in t o s h

programming
In te l P e n tiu m S u n S o la r is A p p le M a c in to s h

08.12.21 M. Venugopal, OU, Hyd 5


Fundamentals of Java
 Java developed by Sun
 Sun describes Java as:
– Simple, Object-oriented, Distributed, Interpreted,
Robust, Secure, Architecture neutral, Portable, High-
performance, Multithreaded, and Dynamic Language
 Java is touted as
– Web-savvy programming language
– Language for Internet Programming
– Better C++
 Without difficulties and bug commonly encountered in C, C++
programming languages

08.12.21 M. Venugopal, OU, Hyd 6


Tools Available for Java Programming

 Java Developer’s Kit (JDK)


– JDK from JavaSoft a division of Sun
Microsystems Inc.
– Contains the basic tools and libraries necessary
for creating, testing, documenting and executing
Java programs
– There are seven main programs in JDK
 javac – the Java Compiler
 java – the Java Interpreter
 javadoc – generates documentation in HTML

08.12.21 M. Venugopal, OU, Hyd 7


Tools Available for Java Programming
– Main programs in JDK (contd.)
 appletviewer – the Java Interpreter to execute Java
applets
 jdb – the Java Debugger to find and fix bugs in Java
programs
 javap – the Java Disassembler to displays the
accessible functions and data in a compiled class; it
also displays the meaning of byte codes
 javah – to create interface between Java and C
routines

08.12.21 M. Venugopal, OU, Hyd 8


Tools Available for Java Programming
 Packages in JDK
– API – the Application Programming Interface
enables Java programmers to develop varieties
of applets and applications
– It contains six packages:
 java.applet – for applet programming
 java.awt – the Abstract Windowing Toolkit for
designing GUI like Button, Checkbox, Choice, Menu,
Pannel etc.
 java.io – file input/output handling

08.12.21 M. Venugopal, OU, Hyd 9


Tools Available for Java Programming
– API in Java (contd.)
 java.lang – provides useful classes like to handle
Object, Thread, Exception, String, System, Math,
Float, Integer etc.
 java.net – classes for network programming;
supports TCP/IP networking protocols
 java.util – it contains miscellaneous classes like
Vector, Stack, List, Date, Dictionary, Hash etc.

JDK is a free software and can be downloaded from


JavaSoft’s web site at http://java.sun.com

08.12.21 M. Venugopal, OU, Hyd 10


Third Part Tools for Java Programming
 Web browser
– Web browser in a client machine connects a link
to a web site, download web page from it and then
executes
– Java environment requires Java-enabled web
browser to supports Java applets
– Few (free) popular Java-enabled web browsers:
 HotJava from JavaSoft web site (http://java.sun.com)
 Netscape Navigator from Netscape home page (
http://home.nescape.com)
 Internet Explorer from Microsoft’s web page (
http://www.microsoft.com)

08.12.21 M. Venugopal, OU, Hyd 11


Other Third Part Tools
 Java IDE
– Number of IDEs are available to support the
productivity of software development
– Few important of them are:
 Sun’s Java Workshop dev 5 from Sun’s JavaSoft
(recently powered with Visual Java)
 Mojo from Penumbra Software (best visual environment
for creating Java applets)
 Jumba from Aimtech and IBM (graphical applet builder)
 Semantic Café from Semantics (a de-facto standard for
Java development on Windows systems)

08.12.21 M. Venugopal, OU, Hyd 12


Programming in Java
 Java programs are available in two flavors:
– Applet
 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
– Application
 It is similar to all other kind of programs like in C, C+
+ to solve a particular problem

In the subsequent discussions we will learn how to manage


these two types of Java programs

08.12.21 M. Venugopal, OU, Hyd 13


Building a Java Application
Our first Java program is a simple Application to print a
message on the screen.
Let us consider the following Application:

1. // Hello Java Application //

2. class HelloWorldApp {
3. public static void main ( String args[] ) {
4. System.out.println ("Hello Java !");
5. }
6. }

08.12.21 M. Venugopal, OU, Hyd 14


How to edit this program?
 Any text editor can be used to write Java
programs. For example,
– In Windows
 Notepad, EDIT etc.
– In Unix
 vi, emacs etc.
 Save the Application
– Save the Application in a file with the name
HelloWorldApp.java

08.12.21 M. Venugopal, OU, Hyd 15


How to compile this program?
 The Java compiler ( javac ) converts a Java
Application into Java byte code.
– Open a DOS shell (Windows or NT) or Terminal
(Unix)
– Move to the directory where your Java program
has been saved
– Enter the following command to compile:
javac HelloWorldApp.java

08.12.21 M. Venugopal, OU, Hyd 16


How to compile this program?
 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

For the running example, the class filename


will be
HelloWorldApp.class
08.12.21 M. Venugopal, OU, Hyd 17
How to execute this program?
 To execute the Java Application, type the
command java (from the command prompt).
For example, for the current example
HelloWorldApp Application can be execured
as
java HelloWorldApp

Wait! Let’s recapitulate whole things once again!!

08.12.21 M. Venugopal, OU, Hyd 18


Building a Java Application

08.12.21 M. Venugopal, OU, Hyd 19


Building a Java Application
Step 1: Edit and Save

08.12.21 M. Venugopal, OU, Hyd 20


Building a Java Application
Step 1: Edit and Save

08.12.21 M. Venugopal, OU, Hyd 21


Building a Java Application
Step 1: Edit and Save

08.12.21 M. Venugopal, OU, Hyd 22


Building a Java Application
Step 1: Edit and Save

08.12.21 M. Venugopal, OU, Hyd 23


Building a Java Application
Step 2: Compile

08.12.21 M. Venugopal, OU, Hyd 24


Building a Java Application
Step 2: Compile

08.12.21 M. Venugopal, OU, Hyd 25


Building a Java Application
Step 3: Execute

08.12.21 M. Venugopal, OU, Hyd 26


Building a Java Applet
Suppose, we want to build an applet which will
look like the following:

08.12.21 M. Venugopal, OU, Hyd 27


Building a Java Applet
Following piece of code is required:

1. // An applet to print Hello World! //

2. import java.awt.Graphics;
3. import java.applet.Applet;
4. public class HelloWorld extends Applet {
5. public void paint (Graphics g ) {
6. g.drawString("Hello World!" 50, 25);
7. }
8. }

08.12.21 M. Venugopal, OU, Hyd 28


Building a Java Applet
Edit → Save → Compile
 Edit the code in the same fashion as an Application
 The name of the applet will be same as the public
class, here
HelloWorld.java
 The program can be compiled in the same fashion
as an Application is compiled. That is,
javac HelloWorld.java
After successful compilation, the javac will produce
a file named
HelloWorld.class

08.12.21 M. Venugopal, OU, Hyd 29


Building a Java Applet
Execution
 Edit an HTML file to host the applet just created. The
HTML file will look like as:

<applet code = HelloJava.class width = 200 height = 100>


</applet>

 Save this to file giving a file name HelloJava.html


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
 Now the applet is ready for its execution!
 To run with appletviewer type the following:
appletviewer HelloJava.html

08.12.21 M. Venugopal, OU, Hyd 30


More on Java Application
Structure of a Java Application
 Let us analyze the different components in the
HelloWorldApp.java
// Hello Java Application //

class HelloWorldApp {
public static void main ( String args[ ] ) {
System.out.println ("Hello Java !");
}
}
class
public, static, void, main
String args[ ]
System.out.println

08.12.21 M. Venugopal, OU, Hyd 31


General Structure of an Application
D o c u m e n t s e c tio n (o p tio n a l)

P a c k a g e s ta te m e n t (o p tio n a l)

In te rfa c e s ta te m e n t (o p tio n a l)

C la s s d e fin itio n (s ) (o p tio n a l)

M a in c la s s d e fin itio n
{
m a in m e th o d d e fin itio n
}

08.12.21 M. Venugopal, OU, Hyd 32


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("Square root of "+ x +"=" + y);
}
}

08.12.21 M. Venugopal, OU, Hyd 33


Application with Multiple Classes
// Application with more than one classes //

class FirstClass {
int idNo;
iIdNo = 555;
public static void print( ) {
System.out.println ( " First Class citizen" + idNo );
}
}

class SecondClass {
int idNo;
idNo = 111;
public static void print( ) {
System.out.println ( " Second Class citizen " + idNo) ;
}
}

08.12.21 M. Venugopal, OU, Hyd 34


Application with Multiple Classes
(contd..)

public class PeopleAppln {


FirstClass female;
SecondClass male;
public static void main( String args[ ] ) {
System.out.print("People from Java World");
female.print( );
male.print( );
}
}

08.12.21 M. Venugopal, OU, Hyd 35


Application without any Class!

// Edit the following program as HelloNoClass.java

public static void main (String args[ ] ) {


System.out.println( "Hello Classless Java!]);
}

Type following two commands to run the Hello.java Application :

javac HelloNoClass.java // To compile


java HelloNoClass // To run the program

08.12.21 M. Venugopal, OU, Hyd 36


Communication to Java Application
 How input can be passed to an Application
while it is running?

 Java provides two methods for it


– Using the command line arguments
– Using the DataInputStream class

08.12.21 M. Venugopal, OU, Hyd 37


Command Line Arguments
1. class CommnadLineInputTest
2. {
3. public static void main(String args[ ] ) {
4. int count;
5. String aString;
6. count = args.length;
7.
8. System.out.println( "Number of arguments = “ + count);
9.
10. for(int i = 0; i < count; i++) {
11. aString = args[i];
12. System.out.println( "args[“ + i + "]“ + "=“ + aString);
13. }
14. }
15. }

08.12.21 M. Venugopal, OU, Hyd 38


Get Input using DataInputStream
in te r e s tT o ta l

p r in c ip a lA m o u n t

n u m b e rO fY e a rs

r a te O fIn te r e s t

08.12.21 M. Venugopal, OU, Hyd 39


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("Enter Principal Amount: ");


System.out.flush();
tempString = in.readLine();
principalAmount = Float.valueOf(tempString);

08.12.21 M. Venugopal, OU, Hyd 40


Calculator Program (contd..)
System.out.print("Enter Rate of Interest: ");
System.out.flush();
tempString = in.readLine();
rateOfInterest = Float.valueOf(tempString);

System.out.print("Enter Number of Years: ");


System.out.flush();

tempString = in.readLine();
numberOfYears = Integer.parseInt(tempString);

// Input is over: calculate the interest


int interestTotal = principalAmount*rateOfInterest*numberOfYears;

System.out.println("Total Interest = " + interestTotal);


}
}

08.12.21 M. Venugopal, OU, Hyd 41


Applet Revisited
import java.awt.Graphics;
 import java.applet.Applet;
 public class HelloWorld extends Applet {
 public void paint (Graphics g ) {
 g.drawString("Hello World!" 50, 25);
 }
 }

08.12.21 M. Venugopal, OU, Hyd 42


Structure of an Applet

Im p o rt S e c tio n P a rt 1

p u b lic c la s s N e w A p p le tN a m e e x te n d s A p p le t { P a rt 2

V a ria b le d e c la ra tio n (s ) P a rt 3

P a rt 4
M e th o d (s ) fo r o b je c t in te ra c tio n d e c la re d a n d d e fin e d h e re

J a v a c o d e to a c c o m p lis h e d a ta s k

08.12.21 M. Venugopal, OU, Hyd 43


Basic Methods in Applet
 public void init( )
– To initialize or pass input to an applet
 public void start( )
– The start( ) method called after the init( ) method, starts
an applet
 public void stop( )
– To stop a running applet
 public void paint (Graphics g)
– To draw something within an applet
 public void destroy( )
– To remove an applet from memory completely

08.12.21 M. Venugopal, OU, Hyd 44


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 ( " Hello World !", 50, 25 );
}
}

08.12.21 M. Venugopal, OU, Hyd 45


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(getParameter (" xValue" ));
y = Integer.parseInt(getParameter (" yValue" ));
w = Integer.parseInt(getParameter (" wValue" ));
h = Integer.parseInt(getParameter (" hValue" ));
}

public void paint ( Graphics g ) {


g.drawRect (x, y, w, h );
}
}

08.12.21 M. Venugopal, OU, Hyd 46


One More Example: Use of init( )
Corresponding HTML document containing this applet and providing
parameter values will be :

< applet code = " RectangleTest" 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 >

08.12.21 M. Venugopal, OU, Hyd 47


Application vs. Applet
 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
 Unlike Application (stand alone), applets cannot be run
independently. They are to be embedded in HTML pages
as applet code, which browser can run
 Applet cannot read from or write to the file in the local
computers
 Applet cannot communicate with other servers in the
networks
 Applet cannot run any program from local computers
 Applets are restricted from using libraries from other
languages, such as, C, C++.
Why applets are designed with so many restrictions??
08.12.21 M. Venugopal, OU, Hyd 48

Você também pode gostar