Anda di halaman 1dari 48

Object Oriented Programming

with Java
Topic
Lecture 1

C++ vs. Java

Fundamentals of Java

Java Application and Applet

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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.

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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 × √

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 4
C++ vs. Java
Java source

 Programming code

Environment Java
compiler

– C++ provides platform


dependent C++
Complier
Java BYTE
JVM
programming C++ source
code
code C++ object
code

– Java provides platform Java Java Java

independent interprter
WIN32
interprter
Solaris
interprter
Macintosh

programming
Intel Pentium Sun Solaris Apple Macintosh

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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)
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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)
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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. }

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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!!


Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 18
Building a Java Application

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 19
Building a Java Application
Step 1: Edit and Save

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 20
Building a Java Application
Step 1: Edit and Save

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 21
Building a Java Application
Step 1: Edit and Save

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 22
Building a Java Application
Step 1: Edit and Save

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 23
Building a Java Application
Step 2: Compile

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 24
Building a Java Application
Step 2: Compile

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 25
Building a Java Application
Step 3: Execute

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 26
Building a Java Applet
Suppose, we want to build an applet which will
look like the following:

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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. }

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 31
General Structure of an Application
Document section (optional)

Package statement (optional)

Interface statement (optional)

Class definition(s) (optional)

Main class definition


{
main method definition
}

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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);
}
}

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 33
Application with Multiple Classes
// Application with more than one classes //

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

class SecondClass {
int idNo;
idNo = 111;
public startic void print( ) {
System.out.println ( " Second Class citizen " + idNo) ;
}
}
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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( );
}
}

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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[0];
12. System.out.println( "args[“ + I + "]“ + "=“ + aString);
13. }
14. }
15. }

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 38
Get Input using DataInputStream
interestTotal

principalAmount

numberOfYears

rateOfInterest

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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);
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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);


}
}
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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);
 }
 }

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 42
Structure of an Applet

Import Section Part 1

public class NewAppletName extends Applet { Part 2

Variable declaration(s) Part 3

Part 4
Method(s) for object interaction declared and defined here

Java code to accomplished a task

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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 );
}
}

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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(get Parameter (" xValue" ));
y = Integer.parseInt(get Parameter (" yValue" ));
w = Integer.parseInt(get Parameter (" wValue" ));
h = Integer.parseInt(get Parameter (" hValue" ));
}

public void paint ( Graphics g ) {


g.drawRect (x, y, w, h );
}
}
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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 >

Short Term Course on C++ and


Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 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 severs 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??
Short Term Course on C++ and
Java, 14-30 June, 2004, IIT
19.03.2018 Kharagpur, Lecture #1 48

Anda mungkin juga menyukai