Advance Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 70

1 | Page

Ques1: Wriite a program to create Lable and Button.


Ans: import java.awt.*;
public class LabelButtonTest extends java.applet.Applet
{
public void init()
{
setFont(new Font("Helevetica",Font.BOLD,14));
Label lblLeft=new Label("aligned left",Label.LEFT);
add(lblLeft);
Button btnPlay=new Button("Play");
add(btnPlay);
}
}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="LabelButtonTest.class"Width=300 Height=200>
</APPLET></BODY></HTML>

2 | Page

3 | Page

Ques:2 Write a program to create a Checkbox.


Ans: import java.awt.*;
import java.applet.Applet;
public class CheckboxTest1 extends Applet
{
public void init()
{
Checkbox chkshoes=new Checkbox("shoes");
add(chkshoes);
Checkbox chksocks=new Checkbox("socks",true);
add(chksocks);
}
}
<HTML>
<HEAD>
<TITLE>Applet Test </TITLE>
</HEAD>
<BODY>
<APPLET Code="CheckboxTest1.class"Width=300 Height=200>
</APPLET></BODY></HTML>

4 | Page

5 | Page

Ques:3 Write a program to draw checkbox group to the applet.


Ans: import java.awt.*;
import java.applet.*;
public class AppletProgram1 extends Applet
{
public void init()
{
CheckboxGroup colorGrp = new CheckboxGroup();
Checkbox red = new Checkbox("Red",colorGrp, true);
Checkbox blue = new Checkbox("Blue",colorGrp, true);
Checkbox yellow = new Checkbox("Yellow", colorGrp, true);
add(red);
add(blue);
add(yellow);
}
}
<html>
<title>Event Handling</title>
<hr>
<applet code="AppletProgram1.class"
width="300" height="300">
</applet>
<hr>
</html>

6 | Page

7 | Page

Ques:4 Write a program to create choice menus or choice lists.


Ans: import java.awt.*;
public class ChoiceTest1 extends java.applet.Applet
{
public void init()
{
Choice ch = new Choice();
ch.add("apples");
ch.add("bananas");
ch.add("mangoes");
add(ch);
}}

<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="ChoiceTest1.class"Width=300 Height=200>
</APPLET>
</BODY></HTML>

8 | Page

Ques:5 Write a program to create text field.


9 | 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

Ques:6 write a program to create TextArea.


Ans:import java.awt.*;
import java.applet.Applet;
11 | P a g e

public class TextAreaTest extends Applet


{
String letter = "java is a programming language.Language are used \n"+
"to compose a set of instructions,which \n"+
"a computer obey";
TextArea taDetails;
public void init()
{
taDetails = new TextArea(letter,10,45);
add(taDetails);
}
}

<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

Ques:7 Write a program to create ScrollingLists.


Ans:import java.awt.*;
import java.applet.Applet;
public class ListTest extends Applet
{
List lt = new List(4,true);
public void init()
{
lt.add("ram");
lt.add("sham");
lt.add("jass");
lt.add("geeta");
add(lt);
}
}

<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="ListTest.class"Width=300 Height=200>
</APPLET>
</BODY></HTML>

14 | P a g e

Ques:8 Write a program to create Scrollbars.


15 | 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

Ques:9 Write a program Working with Frames.


17 | 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

Ques:10 Write a program Working with panels.


Ans:import java.awt.*;
import java.applet.*;
public class PanelApplet extends Applet
{
public void init()
{
Panel mainPanel = new Panel();
Panel subPanel = new Panel();
add(new Button("Applet Button"));
add(mainPanel);
mainPanel.add(new Button("mainPanel Button"));
mainPanel.add(subPanel);
subPanel.add(new Button("subPanel Button"));
}
}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="PanelApplet.class"Width=300 Height=200>
</APPLET>
</BODY></HTML>
20 | P a g e

21 | P a g e

Ques:11 Write a program using GRID LAYOUT.


Ans: import java.awt.*;
import java.applet.Applet;
public class Bunch extends Applet
{
GridLayout grid = new GridLayout(2,2,10,10);
Button btnMarcia = new Button("Marcia");
Button btnCarol = new Button("Carol");
Button btnGreg = new Button("Greg");
Button btnJan = new Button("Jan");
public void init()
{
setLayout(grid);
add(btnMarcia);
add(btnCarol);
add(btnGreg);
add(btnJan);
}}
<HTML>
<HEAD>
<TITLE>Applet Test</TITLE>
</HEAD>
<BODY>
<APPLET Code="Bunch.class"Width=300 Height=200>
</APPLET></BODY></HTML>

22 | P a g e

23 | P a g e

Ques:12 Write a program using BORDER LAYOUT.


Ans: import java.awt.*;
import java.applet.Applet;
public class Border extends Applet
{
BorderLayout border = new BorderLayout();
Button btnNorth = new Button("North");
Button btnSouth = new Button("South");
Button btnEast = new Button("East");
Button btnWest = new Button("West");
Button btnCenter = new Button("Center");
public void init()
{
setLayout(border);
add("North",btnNorth);
add("South",btnSouth);
add("East",btnEast);
add("West",btnWest);
add("Center",btnCenter);
}}
<HTML><HEAD>
<TITLE>Applet Test</TITLE>
</HEAD><BODY>
<APPLET Code="Border.class"Width=300 Height=200>
</APPLET></BODY></HTML>

24 | P a g e

25 | P a g e

Ques:13 Write a program that processes its own


events.
Ans:import java.awt.*;
import java.awt.event.*;
public class SelfButton extends Button
{
public SelfButton(String lbl)
{
super(lbl);
enableEvents(AWTEvent.ACTION_EVENT_MASK);
}
public void processActionEvent (ActionEvent ActEvt)
{
System.out.println("Hey!! I'M handling my own Events");
}
}

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

Ques:14 Write a program to attach an event handler to scrollbar.


Ans:import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public interface AdjustmentListener extends EventListener
{
public void adjustmentValueChanged(AdjustmentEvent AdjEvt);
}
public class AdjustmentEventTest extends Applet
{
public void init()
{
Scrollbar s1 = new Scrollbar();
// s1.addAdjustmentListener(this);
add(s1,"West");

Scrollbar s2 = new Scrollbar();


add(s2,"East");
}
public void adjustmentValueChanged(AdjustmentEvent AdjEvt)
{
System.out.println("Scrollbar: " + AdjEvt.getValue());
}
}
class SelfScrollbar extends Scrollbar
28 | 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

Ques:15 Write a program to dropdown list and attach event to it.


Ans:import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ItemEventTest extends Applet implements ItemListener
{
List it;
SelfChoice sl;
public void init()
{
it = new List(5,false);
it.add ("Chocolate");
it.add ("Vanilla");
it.add ("Strawberry");
it.add ("Mocha");
it.add ("Peppermint Swirl");
it.add ("Blackberry Ripple");
it.add ("Butterscotch");
it.add ("Almond");
it.addItemListener(this);
add(it);
sl = new Selfchoice();
sl.add("Ice Cream");
sl.add("Frozen Yogurt");
sl.add("Sorbet");
31 | 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

Ques:16 Write a program to demonstrate the mouse events.


33 | P a g e

Ans:import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class MouseEventTest extends Applet implements MouseListener,MouseMotionListener


{
Public void init()
{
Canvas can = new Canvas();
can.setBackground(Color.white);
can.addMouseListener(this);
add(can);
SelfMouseCanvas slfCan = new SelfMouseCanvas();
add(slfCan);
}
public void mousePressed(MouseEvent MosEvt)
{
System.out.println("UPPER: mouse pressed at " + MosEvt.getX() + " " + MosEvt.getY());
}
public void mouseReleased(MouseEvent MosEvt)
{
System.out.println("UPPER: mouse released at " + MosEvt.getX() + " " + MosEvt.getY());
}
public void mouseEntered(MouseEvent MosEvt)
{
34 | P a g e

System.out.println("UPPER: mouse entered");


}
public void mouseExited(MouseEvent MostEvt) {}
public void mouseClicked(MouseEvent MostEvt) {}
public void mouseMoved(MouseEvent MostEvt) {}
public void mouseDragged(MouseEvent MostEvt) {}
class selfMouseCanvas extends Canvas
{
public SelfMouseCanvas()
{
setBackground(Color.green);
enableEvent(AWTEvent.MOUSE_EVENT_MASK|
AWTEvent.MOUSE_MOTION_EVENT_MASK);
}

public void processMouseEvent(MouseEvent MosEvt)


{
if(MostEvt.getID() == MouseEvent.MOUSE_PRESSED)
{
System.out.println("LOWER: mouse pressed at " + MosEvt.getX() + " " + MosEvt.getY());
}

if(MostEvt.getID() == MouseEvent.MOUSE_RELEASED)
{
System.out.println("LOWER: mouse released at " + MosEvt.getX() + " " + MosEvt.getY());
35 | P a g e

else if(MostEvt.getID() == MouseEvent.MOUSE_ENTERED)


{
System.out.println("LOWER: mouse entered");
}
}
}
}
<html>
<title>Event Handling</title>
<hr>
<applet code="MouseEventTest.class"
width="300" height="300">
</applet>
<hr>
</html>

36 | P a g e

Ques:17 Write a program to demonstrate the use of KillableFrame class.


Ans:import java.awt.*;
37 | 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

Ques:18 Write a program to implement key Events attached with textfield.


Ans:import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class KeyEventTest extends Applet implements KeyListener
{
public void init()
{
setLayout(new BorderLayout());
TextField txt = new TextField();
txt.addKeyListener(this);
add(txt);
SelfKeyTextArea slfTa = new SelfKeyTextArea();
add(slfTa,"Center");
public void keyTyped(KeyEvent KyEvt)
{
System.out.println("Key typed in text field:" + kyEvt.getKeyChar());
}
public void keyPressed(keyEvent KyEvt) {}
public void keyReleased(keyEvent KyEvt) {}

}
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

Ques:19 Write a program to create a thread using Thread Class.


Ans: import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\tFrom Thread A: i = " + i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\tFrom Thread B: j = " + j);
}
System.out.println("Exit from B");
}
}

44 | P a g e

class C extends Thread


{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\tFrom Thread C: k = " + k);
}
System.out.println("Exit from C");
}
}

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

Ques:20 Write a program to create a thread and implement thread methods.


Ans import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\tFrom Thread A: i = " + i);
if(i<=2)
{
yield();
}
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\tFrom Thread B: j = " + j);
if(k==3)
47 | 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();

System.out.println("Start thread C");


threadC.start();

System.out.println("End of main thread");


}
}

49 | P a g e

50 | P a g e

Ques:21 Write a program to set the priority of the thread.


Ans: class A extends Tread
{
public void run()
{
System.out.println("Thread A started");
for(int i =1;i<=4;i++)
{ System.out.println("\tFrom Thread A : i = " + i);}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B started");
for(int j =1;j<=4;j++)
{ System.out.println("\tFrom Thread B : j = " + j);}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
51 | 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

Ques:22 Write a program to create thread using Runnable Interface.


Ans; class X implement Runnable
{
public void run()
{
for(int i = 1;i<=10;i++)
{
System.out.println("\tThreadX: " + i);
}
System.out.println("End of ThreadX");
}
}
class RunnableTest
{
public static void main(String args[])
{
X runnable = new X();
Thread theadX = new Thread(runnable);
}
threadX.start();
System.out.println("End of main Thread");
}

54 | P a g e

55 | P a g e

Ques:23Write a program to copy the content of the one file to another.


Ans: import java.io.*;
class CopyCharacters
{
public static void main(String args[])
{
File inFile = new File("input.dat");
File outFile = new File("output.dat");
FileReader ins = null;
FileWriter outs = null;
ins = new FileReader(inFile);
outs = new FileWriter(outFile);
int ch;
while(ch = ins.read() != -1)
{
outs.write(ch);
}
ins.close();
outs.close();
}
}

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

Ques:26Write a program to copy byte from one file to another.


Ans:import java.io.*;
public class CopyByte
{
public static void main(string[] args)
{
FileInputStream i = null;
FileOutputStream o = null;
byte byteRead;
i = new FileInputStream("input.dat");
o = new EileOutputStream("output.dat");
do
{
byteRead = (byte)i.read();
o.write(byteRead);
i.close();
o.close();
}
}

62 | P a g e

63 | P a g e

Ques:27Write a program to copy byte from one file to another.


Ans::import java.io.*;
public class CopyByte
{
public static void main(string[] args)
{
FileInputStream i = null;
FileOutputStream o = null;
byte byteRead;
i = new FileInputStream("input.dat");
o = new EileOutputStream("output.dat");
do
{
byteRead = (byte)i.read();
o.write(byteRead);
i.close();
o.close();
}
}
}

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();

dis = new DataInputStram(new FileInputStream(intFile));


for(int i = 0;i<5;i++)
{
int n = dis.readInt();
System.out.println(n);
}
dis.close();
}
}
66 | P a g e

67 | P a g e

Ques:29 Write a program to demonstrate concatenation and buffering.


Ans: import java.io.*;
class SequenceBuffer
{
public static void main(string args[])
{
FileInputStream file1 = null;
FileInputStream file2 = null;
SequenceInputStream file3 = null;
file1 = new FileInputStream("file1.txt");
file2 = new FileInputStream("file2.txt");
file3 = new SequenceInputStream("file1.txt");
BufferedInputStream inb = new BufferedInputStream(file3);
BufferedOutputStream ob = new BufferedOutputStream(System.out);
int ch;
while((ch = inBuffer.read() != -1))
{
ob.write((char)ch);
}
inb.close();
ob.close();
file1.close();
file2.close();
}
}
68 | P a g e

69 | P a g e

Ques:30Write a program to read and write with random access.


Ans: import java.io.*;
class RandomIO
{
public static void main(string args[])
{
RandomAccessFile file = null;
file.writeChar('X');
file.WriteInt(2);
file.WriteDouble(3.14);
file.seek(0);
System.out.println(file.readInt());
System.out.println(file.readChar());
System.out.println(file.readDouble());
file.seek(2);
System.out.println(file.readInt());
file.close();
}
}

70 | P a g e

You might also like