Advance Java
Advance Java
Advance Java
2 | Page
3 | Page
4 | Page
5 | Page
6 | Page
7 | Page
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="ChoiceTest1.class"Width=300 Height=200>
</APPLET>
</BODY></HTML>
8 | Page
Ans:import java.awt.*;
public class TextFieldTest extends java.applet.Applet
{
public void init()
{
Label lblName = new Label("enter name");
add(lblName);
TextField txtName = new TextField("your name here",20);
add(txtName);
}
}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="TextFieldTest.class"Width=300 Height=200>
</APPLET>
</BODY>
</HTML>
10 | P a g e
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="TextAreaTest.class"Width=300 Height=200>
</APPLET></BODY></HTML>
12 | P a g e
13 | P a g e
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="ListTest.class"Width=300 Height=200>
</APPLET>
</BODY></HTML>
14 | P a g e
Ans:import java.awt.*;
import java.applet.Applet;
public class ScrollTest extends Applet
{
Scrollbar bar = new Scrollbar(Scrollbar.VERTICAL,10,0,1,100);
public void init()
{
add(bar);
}
}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="ScrollTest.class"Width=300 Height=200>
</APPLET>
</BODY>
</HTML>
16 | P a g e
Ans:import java.awt.*;
import java.applet.Applet;
public class FrameTestApplet extends Applet
{
public void init()
{
Frame frm = new Frame("My New Frame");
frm.setLayout(new FlowLayout());
frm.setSize(300,200);
Label lblname = new Label("Name");
frm.add(lblname);
TextField txtname = new TextField("Enter your name");
frm.add(txtname);
frm.setVisible(true);
}
}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="FrameTestApplet.class"Width=300 Height=200>
</APPLET></BODY></HTML>
18 | P a g e
19 | P a g e
21 | P a g e
22 | P a g e
23 | P a g e
24 | P a g e
25 | P a g e
import java.applet.*;
public class SelfButtonTest extends Applet
{
String str = new String("Click here");
public void init()
{
add(new SelfButton(str))
;}
26 | P a g e
27 | P a g e
{
public SelfScrollbar()
{
enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
}
public void processAdjustmentEvent(AdjustmentEvent AdjEvt)
{
System.out.println("Scrollbar-2"+AdjEvt.getValue());
}
}
[Note:AnAppletWithScrollBar.html]
<html>
<title>Event Handling</title>
<hr>
<applet code="AnAppletWithScrollbar.class"
width="300" height="300">
</applet>
<hr>
</html>
29 | P a g e
30 | P a g e
add(sl);
}
public void itemStateChanged(ItemEvent ItmEvt)
{
System.out.println("New item from list:"+it.getSelectedItem());
}
}
class SelfChoice extends Choice
{
public SelfChoice()
{
enableEvents(AWTEvent.ITEM_EVENT_MASK);
}
public void processItemEvent(ItemEvent ItmEvt)
{
System.out.println ("New item from Choice:"+getSelectedItem());
}
}
<html>
<title>Event Handling</title>
<hr>
<applet code=" ItemEventTest.class"
width="300" height="300">
</applet>
<hr></html>
32 | P a g e
Ans:import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
if(MostEvt.getID() == MouseEvent.MOUSE_RELEASED)
{
System.out.println("LOWER: mouse released at " + MosEvt.getX() + " " + MosEvt.getY());
35 | P a g e
36 | P a g e
import java.awt.event.*;
public class KillableFrame extends Frame
{
public static void main(String args[])
{
KillableFrame kfrm = new KillableFrame();
kfrm.setVisible(true);
}
public KillableFrame()
{
setSize(250,250);
enableEvent(AWTEvent.WINDOW_EVENT_MASK);
}
public void paint(Graphics Grph)
{
Grph.drawSting("Click On X to Exit.",20,100);
}
public void processWindowEvent(WindowEvent Winevt)
{
if(WinEvtt.getID() == WindowEvent.WINDOW_CLOSING)
{
setVisible(false);
dispose();
System.exit(0);
}
38 | P a g e
}
}
<html>
<title>Event Handling</title>
<hr>
<applet code="Killableframe.class"
width="300" height="300">
</applet>
<hr>
</html>
39 | P a g e
40 | P a g e
}
class SelfKeyTextArea extends TextArea
{
public SelfKeyTextArea()
41 | P a g e
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
}
public void processKey(KeyEvent KyEvt)
{
if(KyEvt.getID() == KeyEvent.KEY_TYPED)
{
System.out.println("Key typed in text area: " + KyEvt.getKeyChar());
}
}
}
}
<html>
<title>Event Handling</title>
<hr>
<applet code="KeyEventTest.class"
width="300" height="300">
</applet>
<hr>
</html>
42 | P a g e
43 | P a g e
44 | P a g e
class ThreadTest
{
public static void main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();
}
}
45 | P a g e
46 | P a g e
{
stop();
}
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\tFrom Thread C: k = " + k);
if(k==1)
{
sleep(1000);
}
}
System.out.println("Exit from C");
}
}
class ThreadTest
{
public static void main(string[] args)
48 | P a g e
{
A a = new A();
B b = new B();
C c = new C();
System.out.println("Start thread A");
threadA.start();
System.out.println("Start thread B");
threadB.start();
49 | P a g e
50 | P a g e
System.out.println("Thread C started");
for(int k =1;k<=4;k++)
{ System.out.println("\tFrom Thread C : k = " + k)}
System.out.println("Exit from C");
}
}
class ThreadPriority
{
public static void main(string args[])
{
A threadA = new A();
B threadB = new B();
C threadC = new C();
threadA.setPriority(Thread.MAX_PRIORITY);
threadA.setPriority(Thread.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
threadA.start();
System.out.println("Start thread B");
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of main thread");
}
}
52 | P a g e
53 | P a g e
54 | P a g e
55 | P a g e
56 | P a g e
57 | P a g e
Ques:24 Write a program to copy the content of the one file to another.
Ans:import java.io.*;
class WriteBytes
{
public static void main(string[] args)
{
byte cities[] = {
'D','E','L','H','I','\N',
'M','A','D','R','A','S','\N',
'L','O','N','D','O','N'
};
FileOutputStream outfile = null;
outfile = new FileOutputStream("city.txt");
outfile.write(cities);
outfile.close();
}
}
58 | P a g e
59 | P a g e
Ques:25 Write a program to copy the content of the one file to another.
Ans: import java.io.*;
class ReadBytes
{
public static void main(string[] args)
{
int i;
FileInputStream infile = null;
infile = new FileInputStream("city.txt");
while((i = infile.read()) != -1)
{
System.out.println((char)i);
}
infile.close();
}
}
60 | P a g e
61 | P a g e
62 | P a g e
63 | P a g e
64 | P a g e
65 | P a g e
Ques:28 Output of the program Write a program to read and write to the same file.
Ans: import java.io.*;
class ReadWriteInteger
{
public static void main(String args[])
{
DateInputStream dis = null;
DateOutputStream dis = null;
File intFile = new file("rand.dat");
dos = new DataOutputStream(new FileOutputStream(intFile));
for(int i = 0;i<5;i++)
{
dos.writeInt((int)i);
}
dos.close();
67 | P a g e
69 | P a g e
70 | P a g e