Appelts:
applets are small java programs.
applets are small java programs.
they can be transported from one computer to
other and run using applet viewer or any web browser that supports java.
applets can perform:
1.arithmetic operations
2.graphics
3.play sound /music
4.accepts user input
5.create animations
6.we can play games
7.moving images
*applets differ from java applications.
*although both java programs, there are significant differences between them.
*there is no main method for applets for initiating the execution of code
applets can not run independently they run from inside a web page called html.
life cycle of an applet
1.init()
2.start()
3.run()
4.stop()
* in applets the output may be a text or a graphics for this we need to use a method called paint method. which requires a graphics object as an argument.
public void paint(graphics g)
his method contains in java.awt package
syntax:
for a simple applet program
for a simple applet program
public class classname extends applet
{
………
………
public void paint(graphics g)
{
……
…….
}
}
html tags:
<head>
<body>
<title>
eventhandling
event handling is a mechanism that is used to handle events generated by applets.
an event could be anything a mouse click or pressing key.
types of events:
1.action event
2.item event
3.window event
4.key event
5.text event.
event listeners:
event listeners are available in a package called
import java.awt.event
program on applet in Java
import java.applet.*;
import java.awt.*;
public class firstapp extends applet
{
public void paint(graphics g)
{
g.drawstring("hello",100,100);
}
}
<html>
<applet code="firstapp" width=400 height=300>
</applet>
</html>
ex2:
import java.applet.*;
import java.awt.*;
public class arc extends applet
{
public void paint(graphics g)
{
g.drawarc(10,20,30,40,50,60);
g.fillarc(10,20,30,40,50,60);
}
}
<html>
<applet code="arc" width=300 height=200>
</applet>
</html>
example3:
import java.awt.*;
import java.applet.*;
public class reg extends applet
{
label l1,l2;
public void init()
{
l1=new label("one");
l2=new label("two");
add(l1);
add(l2);
}
}
<html>
<applet code="reg" width=400 height=400>
</applet>
</html>
example4:
import java.applet.*;
import java.awt.*;
public class rect extends applet
{
public void paint(graphics g)
{
g.drawrect(100,100,50,50);
//g.fillrect(150,150,20,20);
g.fillrect(100,100,50,50);
}
}
<html>
<applet code="rect" width=300 height=200>
</applet>
</html>
/example4
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class textfield extends applet
{
textfield t1,t2;
public void init()
{
t1=new textfield();
t2=new textfield();
add(t1);
add(t2);
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class textfield1 extends applet
{
label l1,l2;
textfield t1,t2;
public void init()
{
l1=new label("one");
t1=new textfield();
l2=new label("two");
t2=new textfield();
add(l1);
add(l2);
add(t1);
add(t2);
}
}
string methods:
examples on action listeners in Java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class actlogin extends applet implements actionlistener
{
label l1,l2;
textfield t1,t2;
button b1;
string msg=" ";
public void init()
{
l1=new label("login id");
l2=new label("pwd");
t1=new textfield();
t2=new textfield();
add(l1);
add(l2);
add(t1);
add(t2);
b1=new button("submit");
add(b1);
b1.addactionlistener(this);
}
public void actionperformed(actionevent e)
{
string s1=e.getactioncommand();
if(s1.equals("submit"))
{
string s2=t1.gettext();
string s3=t2.gettext();
if(s2.equals("ram")&&s3.equals("sam"))
msg="u have logged succesfully";
repaint();
}
}
public void paint(graphics g)
{
g.drawstring(msg,100,100);
}
}
examples on action listeners in Java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class actionlis extends applet implements actionlistener
{
button b1,b2;
string msg;
public void init()
{
b1=new button("add");
b2=new button("sub");
add(b1);
add(b2);
b1.addactionlistener(this);
b2.addactionlistener(this);
}
public void actionperformed(actionevent e)
{
string s1=e.getactioncommand();
if(s1.equals("add"))
msg="u have cicked add";
else
msg="u have cicked sub";
repaint();
}
public void paint(graphics g)
{
g.drawstring(msg,100,100);
}
}
strings:
string represents a sequence of characters
strings represent in java by using character array
char chararray[]=new char[4];
chararray[0]=’j’;
chararray[1]=’a’;
chararray[2]=’v’;
chararray[3]=’a’;
note:java strings are more reliable and predictable compare to ‘c’ strings
declaration of a string:
string s=new string(“hello”);
*to get the length of a string we have a method called “length” method of the string class
ex: int m=s1.length();
concatenation using + operator
string s1=name1+name2;
string methods:
tolowercase;
touppercase;
s2=s1.trim();
s1.equals(s2);
s1.length();
s1.compareto(s2);
s1.concat(s2);
s1.indexof(‘x’);
Example on string concatination in Java
class strconcat
{
public static void main(string args[])
{
string s1 = "table";
string s2 = "tennis";
string s3 = s1+s2;
system.out.println(s3);
}
}
Example on string lower case in Java
class strlower
{
public static void main(string args[])
{
string s1 = "java programming";
string s2 = s1.tolowercase();
system.out.println("original string"+s1);
system.out.println("string changed to "+s2);
}
}
Example on string upper in Java
class strupper
{
public static void main(string args[])
{
string s1 = "java programming";
string s2 = s1.touppercase();
system.out.println("original string"+s1);
system.out.println("string changed to uppercase"+s2);
}
}
//ex on string reverse
public class strreverse
{
public static void main(string[] args)
{
string s="abcde";
system.out.println(s+"..."+new stringbuffer(s).reverse());
}
}
Example on string compare in Java
class strcompare
{
public static void main(string args[])
{
string s1 = "java";
string s1 = new string("j2ee");
if(s1==s2)
{
system.out.println("the string are equal");
}
else{
system.out.println("the string are not equal");
}
}
}
Example on charat in Java
public class charat
{
public static void main(string args[]) {
string s = "java";
char c=s.charat(2);
system.out.println(c);
}
}
string buffer:
string is a fixed length, where as string buffer creates strings of flexible length that can be in terms of both length and content we can insert characters, substrings in the middle of a string and append another string.
Example on length of a string buffer in Java
import java.io.*;
class strbuffer
{
public static void main(string[] args)
{
try
{
BufferedReader object= new bufferedreader (new inputstreamreader(system.in));
system.out.println("enter string value:");
string s=object.readline();
int len=s.length();
system.out.println(len);
}
catch(exception e)
{
}
}
}
Example on srtbuffer using delete,charat methods in Java
import java.io.*;
class strbuff
{
public static void main(string[] args)
{
stringbuffer sb1 = new stringbuffer("hello world");
sb1.delete(0,8);
stringbuffer sb2 = new stringbuffer("railways");
system.out.println(sb2);
sb2.delete(3, sb2.length());
system.out.println(sb2);
stringbuffer sb3 = new stringbuffer("hello world");
sb3.deletecharat(0);
system.out.println(sb3);
}
}
collections:
lists:
arraylist:
the underline data structure for array list is global and resizable array.insertion order is preserved.duplicate objects are allowed.
it is best suitable if our frequent operation is retrieval operation
it is not suitable if our frequent operation is insertion or deletion in the middle, because insertion or deletion causes so many shifting operations hence they are costly operations in the array list.
linked list:
the main drawback of an array list is insertion or deletion in the middle is the most costliest operation to overcome this drawback linked list has been evolved.
the main drawback of an array list is insertion or deletion in the middle is the most costliest operation to overcome this drawback linked list has been evolved.
1.insertion order is preserved.
2.duplicate objects are allowed
3.null insertion is possible
4.best suitable if our frequent operation is insertion or deletion in the middle because it does not require any shifting operations.
5.not suitable in retrieval operations
in arraylist there are no new methods were introduced.where as in linked list there are some new methods are introduced.
6.usually linked list is used for implementing stack and queues.
methods
void addlast(object removefirst)
object getfirst()
object getlast()
set:
1.it is an interface which extends collection interface
2.it is an unordered collection
3.it has classes and interface
classes interface
1. hash set 1.sorted set
2.
linked hashset tree set
linked hashset tree set
hash set:
it does not allow duplicate values.
it does not allow duplicate values.
linked hashset:
similar to linked list but does not allow duplicate values
vector:
since it is introduced in 1.0 version of jdk it is also called as legacy classes(old oldest class) undefined data structure is resizable and global array.
1.insertion order is preserved
2.duplicate objects are allowed
3.null insertion is possible
4.heterogeneous objects are allowed
5.it implements
1.cloneable(duplicate copy)
2.serializable
3.random access interfaces
6.best suitable if thread safety is required
7.all the methods are synchorized hence vector object is thread safe.
sorted set:
sorted set is a class which implement set interface
1.it allows null values
2.the order is preserved
3. it does not allow duplicate values
4.it is best suitable in retrieval as well as sorting or elements in ascending or descending order.
Program example on arraylist in Java
import java.util.*;
class arraylist
{
public static void main(string args[])
{
arraylist al=new arraylist();
system.out.println("size of al"+al.size());
al.add("a");
al.add("b");
al.add("c");
al.add("d");
al.add(1,"a2");
system.out.println("size of alafter adding"+al.size());
system.out.println("size of al"+al);
al.remove("b");
al.remove(3);
system.out.println("size of al after deleting"+al.size());
system.out.println("contents of al"+al);
}
Example program on linked list in Java
import java.util.*;
class linkedlist
{
public static void main(string args[])
{
linkedlist ll=new linkedlist();
ll.add("f");
ll.add("b");
ll.add("d");
ll.add("d");
ll.add("e");
ll.add("c");
ll.add("z");
ll.add("a");
ll.add(1,"a2");
system.out.println("original contents of ll" +ll);
ll.remove("f");
ll.remove(2);
system.out.println("contents of all after deletion"+ll);
ll.removefirst();
ll.removelast();
system.out.println(" ll after delting first and last" +ll);
}
}
Program example on queue in Java
.
import java.util.*;
public class queue
{
public static void main(string args[]) {
list list = new arraylist();
list.add("bere");
list.add("eli");
list.add("gen");
list.add("eli");
list.add("cla");
system.out.println(list);
system.out.println("2: " + list.get(2));
system.out.println("0: " + list.get(0));
linkedlist queue = new linkedlist();
queue.addfirst("bere");
queue.addfirst("eli");
queue.addfirst("gen");
queue.addfirst("eli");
queue.addfirst("cla");
system.out.println(queue);
queue.removelast();
queue.removelast();
system.out.println(queue);
}
Example program on tree in Java
import java.util.*;
class tree1
{
public static void main(string args[])
{
treeset tt=new treeset();
tt.add("a");
tt.add("q");
tt.add("w");
tt.add("d");
tt.add("h");
tt.add("b");
tt.add("c");
tt.add("d");
system.out.println(tt);
}
}
Example program on hash set in Java
import java.util.*;
class hashset
{
public static void main(string args[])
{
hashset hs=new hashset();
system.out.println("add elements to hashset");
hs.add("c");
hs.add("d");
hs.add("a2");
hs.add("e");
hs.add("f");
hs.add("g");
hs.add("h");
hs.add("a2");
hs.add("null");
system.out.println(hs);
}
Example program on tree in Java
import java.util.*;
class treeset
{
public static void main(string args[])
{
treeset ts=new treeset();
ts.add("c");
ts.add("d");
ts.add("a");
ts.add("e");
ts.add("f");
system.out.println(ts);
}
}
program on file input stream in Java
import java.io.fileinputstream;
class ipfile {
public static void main(string args[]) throws exception {
fileinputstream fis = new fileinputstream(args[0]);
// read and display data
int i;
while ((i = fis.read()) != -1) {
system.out.println(i);
}
fis.close();
}
}
io streams:
We know that variables and arrays are used for storing data inside a program
disadvantages of variables and arrays are:
1.lost of data because of temporary storage.
2.difficult to handle large volumes of data
to overcome this we have secondary storage devices like floppy disks and hard disks.
in this we can store the data using “files” concept
what is a file?
a file is a collection of related records placed in one data.
what is a file processing?
storing and managing data using files is known as file processing.it includes
1.creating
2.updating
3.manipulate
data representing in java files:
field(4 characters)
r | a | m | a |
record(3 fields)
rama | 102 | 23 |
file(4 records)
rama | 101 | 38 |
sita | 102 | 36 |
geeta | 103 | 34 |
stream:stream is a flow of data
input:flow of data into a program
output:flow of data out of a program
java streams are two types
1.input stream: reads(or extracts data from the source file and sends it to the program
2.output stream:takes data from the program and sends or writes it to the destination file.
stream classes:
byte stream classes: |
these are used for creating and manipulating streams and files for reading and writing bytes.
character stream classes:
it can be used to read and write 16 bit unicode characters.
string tokenizer:
string tokenizer can be used to breaking up a stream of text from an input text file into meaningful pieces called tokens.
methods
buffer :
buffer is used to store temporarily data that is read from or written to a stream.
buffer sits between the program and the source or destination and functions like a filter.
import java.io.*;
public class createfile
{
public static void main(string[] args) throws ioexception
{
file f;
f=new file("rama.txt");
if(!f.exists())
{
f.createnewfile();
system.out.println("new file \"rama1.txt\" has been created to the current directory");
}
}
}
Example on writing into a file in Java
import java.io.*;
class filewrite
{
public static void main(string args[])
{
try
{
// create file
filewriter fw = new filewriter("out.txt");
bufferedwriter out = new bufferedwriter(fw);
out.write("welcome to files ");
//close the output stream
out.close();
}catch (exception e)
{
//catch exception if any
system.err.println("error:" + e.getmessage());
}
}
}
Example Programming on reading from a file in Java
import java.io.*;
class fileread
{
public static void main(string args[])
{
try
{
// open the file that is the first
// command line parameter
fileinputstream fi = new fileinputstream("out.txt");
// get the object of datainputstream
datainputstream in = new datainputstream(fi);
bufferedreader br = new bufferedreader(new inputstreamreader(in));
string strline;
//read file line by line
while ((strline = br.readline()) != null)
{
// print the content on the console
system.out.println (strline);
}
//close the input stream
in.close();
}
catch (exception e)
{
//catch exception if any
system.err.println("error: " + e.getmessage());
}
}
}
Example program on file append in Java
import java.io.*;
class fileappend
{
public static void main(string args[])
{
try
{
// create file
filewriter fstream = new filewriter("out.txt",true);
bufferedwriter out = new bufferedwriter(fstream);
out.write("this is beautiful");
//close the output stream
out.close();
}
catch (exception e){//catch exception if any
system.err.println("error: " + e.getmessage());
}
}
}
Example on reading from a bufferreader in Java
import java.io.*;
public class buffread
{
public static void main(string[] args)
{
try
{
bufferedreader br = new bufferedreader(
new filereader("out.txt"));
string s;
while ((s = br.readline()) != null)
{
system.out.println(s);
}
} catch (ioexception e)
{
}
}
}
Example program on path in Java
import java.io.*;
public class pathfile1
{
public static void main(string[] args) throws ioexception
{
file f;
f=new file( "rama3.txt");
f.createnewfile();
system.out.println("new file \"rama3.txt\"has been created to the specified location");
system.out.println("the absolute path of the file is:"+f.getabsolutepath());
}
}
Example on stringtokenizer in Java
import java.util.*;
public class strtoken
{
public static void main(string args[])
{
string demo = "this is a string that we want to tokenize";
stringtokenizer tok = new stringtokenizer(demo);
int n=0;
while (tok.hasmoreelements())
system.out.println("" + ++n +": "+tok.nextelement());
}
}
packages:
package is group of classes and interfaces.
adv’s of oops is reusability
if we want to use other programs classes into another program without copying physically what happens?
this can be done using packages.
adv’s of packages
1.packages provide a way to “hide” classes
2.packages also provide the way for separating “design” from “coding”
3. packages also provide different sets of classes
a.internal representation
b. external representation
packages are two types
1.java api package
2.user defined package
java api package
For Part1 Click here
For Part3 Click here
For Frequently asked Java Programs Click here
Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.
No comments:
Post a Comment