Professional Documents
Culture Documents
CS6501 Internet Programming V - Semester
CS6501 Internet Programming V - Semester
IN
V - SEMESTER
STUDY MATERIAL
CS6501
INTERNET PROGRAMMING
ANNA UNIVERSITY
REGULATION 2013
} } IN
Assignment Operator 1-D array example:-
- The assignment operator is the single equal sign = class array
- The assignment operator works in Java much as it does in {
any other computer language. public static void main(String bala [ ] )
{
public class assignment int a [ ] = { 1, 2, 3 }; O/p:-
{ int b [ ] = { 2, 4, 6 }; 3
public static void main(String bala[]) int c [ ] = new int [ 3 ] ;
6
{ for(int i = 0; i < 3 ; i++) 9
int a=8, b=15; O/p:- {
System.out.println(a+=b); 23 c [ i ] = a [ i ] + b [ i ];
System.out.println(a-=b); 8 System.out.println(c[i]);
System.out.println(a*=b); 120 }
System.out.println(a/=b); 8 }
System.out.println(a%=b); 8 }
System.out.println(a<=2); false
System.out.println(a>=2); true 2-D array example:-
System.out.println(a&=b); 8 class array
System.out.println(a^=b); 7 {
System.out.println(a|=b); 15 public static void main(String bala [ ] )
} {
} int a [ ] [ ] = { { 1, 1, 1 }, {1,1,1}, {1,1,1} } ;
int b [ ] [ ] = { { 2, 2, 2 }, {2,2,2}, {2,2,2} } ; O/p:-
Conditional Operator int c [ ] [ ] = new int [ 3 ] [ 3 ] ; 333
- It is also called as Ternary/Conditional/Question-colon/three- for(int i = 0; i < 3 ; i++) 333
way operator { 333
- It can replace “if-then-else” for(int j = 0; j<3; j++)
- General form: expression1? expression2:expression3 {
- If expression1 is true, then expression2 is evaluated; c[i][j]=a[i][j] +b[i][j];
otherwise, expression3 is evaluated. System.out.println(c[ i ] [ j ] );
}
public class conditional System.out.println(“\n”);
{ }
public static void main(String bala[]) }
{ }
int a = 20, b; O/p:-
b = (a = = 1) ? 50 : 100; 100 4. Explain the concept of inheritance and its types.
System.out.println(b); Inheritance in java is defined as the process of deriving
} a child class from a parent class. The child class
} acquires all the properties of the parent class.
3. What is an array? Explain the types of array with ex.
“extends” is the keyword used to inherit the properties
Defn: An array in Java is defined as a collection of elements of of parent class.
similar data type, which are stored in continuous memory Ex:
locations. Class parent Class child extends
The elements are accessed by their indexes. { parent
Array index always starts from zero ------------- {
The size of the array is fixed. ------------- -------
All the elements are stored under the same name. } -------
Ex:- }
int a [ ] = new int [ 5 ] ; //declaration
int [ ] a = new int [ 5 ]; //another way of declaration Types: Single, Multilevel, Hierarchical (Multiple &
int [ ] a = {1,2,3}; // declared and initialized Hybrid - achieved thru interface)
Types:- Advantages: Flexibility, Reusability, Extensibility, Data
Single Dimensional array (1-D array) hiding, Overriding
- Here elements are stored with reference to rows only.
Multi-dimensional array (n-D array)
- Here elements are stored with reference to rows and
columns. (rows x columns)
outer.java outer.java
class outer public class outer
{ {
static class inner void omethod()
{ {
public void imethod() int a=10;
{ class inner
System.out.println("Inner class"); {
} public void imethod()
} {
public static void main(String[ ] bala) System.out.println(a);
{ }
outer o = new outer(); O/p:- }
inner i = new inner(); Inner class inner i = new inner();
i.imethod(); i.imethod();
} }
} public static void main(String bala[ ])
{
oter o = new outer(); O/p:-
Normal inner class o.omethod(); 10
They are non-static member of outer class }
They can access only non-static members of outer class }
innernormal.java
class outer Anonymous inner class
{ Local class without a name
class inner One-shot class, created for ad-hoc purpose
{ It is used under these situations:-
public void imethod() - When a class has short body
{ - Only one instance of class needed
System.out.println("inner class"); - Class is used immediately after defining
}
} outer1.java
void omethod() abstract class anony
{ {
inner i = new inner(); abstract void absmethod();
i.imethod(); }
} public class outer1
} {
public class innernormal public static void main(String bala[ ])
{ {
public static void main(String bala[ ]) anony a = new anony()
{ {
outer o = new outer(); O/p:- public void absmethod()
o.omethod(); Inner class {
}
} System.out.println("anonymous class");
}
}; O/p:-
a.absmethod(); Anonymous class
}
}
Error Exception
Other
ClassNotFoundExceptio RunTimeExcep
IOException exception
n tion
ArrayIndexOutOfBoundsException
} IN
Life cycle of a thread:- Type-2 : Example program:-
class ex2 implements Runnable
{
public void run( )
{
System.out.println(“Thread is running”);
}
public static void main(String bala[ ] )
{
ex2 e = new ex2 ( ); O/p:-
Thread t = new Thread( e ); Thread is running
t.start( ) ;
}
}
Type-2 is preferable because:-
A class that extends Thread class can’t be extended further
If a class extends a Thread class, then all its functionalities
are extended, which is an expensive operation
In the above program, “Thread” class object is created
separately, because, only by extending Thread class, our own
class object will be treated as Thread object. We did not do it
States of a Java Thread:- So we need to create explicitly an object for Thread class and
New: A new thread begins its life cycle in the new state. It pass it to our own class object that implements Runnable, so
remains in this state until the program starts the thread. It is also that run() method is extended
referred to as a born thread. This method is efficient and time saving for Threads.
Runnable: After a newly born thread is started, the thread
becomes runnable. A thread in this state is considered to be Some popular Thread methods in Java:-
executing its task. class sleep extends Thread
Waiting: Sometimes, a thread transitions to the waiting state {
while the thread waits for another thread to perform a task. A public void run()
thread transitions back to the runnable state only when another {
thread signals the waiting thread to continue executing. for(int i = 0; i < 2; i++)
Timed waiting: A runnable thread can enter the timed waiting {
state for a specified interval of time. A thread in this state try
transitions back to the runnable state when that time interval {
expires or when the event it is waiting for occurs. Thread.sleep(1000);
Terminated: A runnable thread enters the terminated state }
when it completes its task or otherwise terminates. catch(InterruptedException e)
{
Type-1 Example program:- System.out.print("Exception here");
class ex1 extends Thread }
{ System.out.println(i);
public void run() System.out.println(Thread.currentThread().getName());
{ }
System.out.println(“Thread is running”); }
} public static void main(String bala[ ] )
public static void main(String bala[ ] ) {
{ sleep s1 = new sleep();
ex1 e = new ex1( ); System.out.println(s1.getName() );
O/p:-
e.start( ); s1.setName("bala");
Thread is running
} System.out.println(s1.getName() );
} s1.start();
run() is the method used to tell a thread, what task it }
should perform while running. } O/p:-
start() is the method used to start execution of a thread Thread-0 name of thread1
Bala Thread name modified
Thread scheduler handles all the thread processes.
0 for loop’s i value printed
This type is the easiest, but not efficient one. Bala current thread name is printed
1 for loop’s i value printed
Bala current thread name is printed
} IN
11. Describe various input and output stream classes with ex import java.io.*;
A stream is defined as a continuous flow of objects class fos
sequentially. {
In Java, A stream is defined as a channel on which data flow public static void main(String bala[ ] ) throws Exception
from sender to receiver {
Categories: Input stream and output stream try
Input Stream: Input stream is defined as an object that {
reads a stream of data from a file String s = "I am a Tamilan";
Output Stream: Output stream is defined as an object that byte a[] = new byte[50];
writes stream of data to a file a= s.getBytes();
Nature of stream: Byte Stream and character stream OutputStream os = new
Byte Stream FileOutputStream("bala.txt");
To give input and get output in bytes for(byte i=0; i<a.length; i++)
os.write(a[i]);
Super classes available: InputStream, OutputStream
System.out.print("\n Write finished");
Methods: read(), write()
os.close();
InputStream classes OutputStream classes }
FileInputStream FileOuputStream catch(FileNotFoundException e1)
PipedInputStream PipedOuputStream {
FilterInputStream FilterOuputStream System.out.print(e1);
ByteArrayInputStream ByteArrayOuputStream }
catch(IOException e2)
Character Stream {
To give input and get output in character System.out.print(e2);
Super classes available: Reader, Writer }
Methods: read(), write()
Classes in Reader Classes in Writer }
FileReader FileWriter }
PipeReader PipeWriter O/p:-
FilterReader FilterWriter d:\> javac fos.java
ByteArrayreader ByteArrayWriter d:\> java fos
write finished
Example for FileInputStream and FileOutputStream classes d:\> type bala.txt
import java.io.*; I am a tamilan
class fis
{ FilterInputStream and FilterOutputStream class
public static void main(String bala[ ] ) FilterStream is a class that wraps the input stream with
{ a byte
int n; With input stream, we can read only bytes
try But if we want to read int, double, char, etc, we need a
{ filter class to wrap the byte input stream
InputStream is = new FileInputStream("bala.txt"); i) DataInputStream and DataOutputStream class
System.out.println("Total Bytes = "+(n=is.available())); DataInputStream reads bytes from stream, converts to
for(int i = 0; i <n; i++) primitive data type
System.out.println((char)is.read()); DataOutputStream converts the primitive data type into
is.close(); bytes and writes these bytes to stream
} import java.io.*;
catch(FileNotFoundException e1) class data
{ {
System.out.print(e1); public static void main(String bala[ ] )
} {
catch(IOException e2) try
{ {
System.out.print(e2); DataOutputStream dos = new
} DataOutputStream(new FileOutputStream("bala.txt"));
dos.writeUTF("Bala");
} dos.writeDouble(12345.67);
} dos.writeInt(1234);
O/p:- dos.writeChar('s');
Total bytes = 20 dos.close();
This is the file content from bala.txt
PREPARED BY V.BALAMURUGAN, ASST.PROF/CSE
CS6501 – INTERNET PROGRAMMING
IN
DataInputStream dis = new DataInputStream(new 12. Explain applet programming in Java with ex.
FileInputStream("bala.txt")); Applets are small Java programs that can be used in internet
System.out.println(dis.readUTF()); They can be transferred over internet from one PC to another,
System.out.println(dis.readDouble()); displayed on Java enabled web browsers
System.out.println(dis.readInt()); Uses: To perform arithmetic operations, display graphics, play
System.out.println(dis.readChar()); sounds, display videos, etc
} Situations for applet usage:-
catch(FileNotFoundException e1) To display dynamic web pages
{ To display special effects (Audio/Video)
System.out.print(e1); To transfer an application to a user who is located remotely
} Differences between applet and application
catch(IOException e2) Applet does not have main() method
{ On loading applets, some methods of applet are called
System.out.print(e2); automatically by JVM
}
O/p:- It cannot work independently; either executed by embedding
Bala applet tag in HTML page (or) using appletviewer.exe
} 12345.67 They cannot read from a file, cannot write into a file
} 1234
s They cannot execute any program in local PC
They cannot communicate with other server in a network
BufferedInputStream and BufferedOutputStream classes They cannot use library files of other languages
It is preferred over other streams for R/W operations They are more secured
It is more efficient in handling files Implement applet:-
All their methods are extended from InputStream and We should import java.awt.*; and import java.applet.*;
OutputStream class which are their parents. AWT = Abstract Window toolkit (contains graphics)
We can specify buffer size (default size = 512 bytes) Class hierarchy:-
import java.io.*; Object
class buf
{
Offers GUI Component
public static void main(String bala[ ] ) (button,check box, etc)
{
try Java.awt
Container
{
DataOutputStream dos = new DataOutputStream(new
BufferedOutputStream(new FileOutputStream("bala.txt"))); Panel
dos.writeUTF("Bala");
dos.writeDouble(12345.67); Java.applet
dos.writeInt(1234); Applet
dos.writeChar('s'); Advantages Disadvantages
dos.close();
Simple, good GUI Java-plugin web browsers
DataInputStream dis = new DataInputStream(new needed
BufferedInputStream(new FileInputStream("bala.txt"));
Supports many platforms GUI based programming is
System.out.println(dis.readUTF());
complex, compared to latest
System.out.println(dis.readDouble());
PHP, HTML&CSS
System.out.println(dis.readInt());
Supported by many browsers Takes a lot of downloading
System.out.println(dis.readChar());
time
}
Suitable for Real time apps Slow to execute
catch(FileNotFoundException e1)
{ Client-server communication Outdated as of now
System.out.print(e1); is possible
}
catch(IOException e2) Life cycle of an applet
{ applet loaded
System.out.print(e2);
} new init( )
O/p:-
}
Bala start( ) stop( ) destroy( )
12345.67 idle dead
} 1234 running
s start( )
browser closed
PREPARED BY V.BALAMURUGAN, ASST.PROF/CSE
CS6501 – INTERNET PROGRAMMING
IN
import java.awt.*;
import java.applet.*;
public class smiley extends Applet
{
public void paint(Graphics g)
{
g.drawOval(60,60,200,200);
g.fillOval(90,120,50,20);
g.fillOval(190,120,50,20);
g.drawLine(165,125,165,175);
g.drawArc(110,130,95,95,0,-180);
}
}
O/p:-
D:\>javac smiley.java
D:\>appletviewer smiley.java