0% found this document useful (0 votes)
7 views

AJP_Practical Exam

The document contains Java code examples for implementing various GUI components using AWT and Swing, including a College Admission form and a Resume form. It also demonstrates different layout managers such as Grid Layout, Border Layout, Flow Layout, and Grid Bag Layout. Each example includes the setup of components, their properties, and the main method to run the application.

Uploaded by

iamstudent6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AJP_Practical Exam

The document contains Java code examples for implementing various GUI components using AWT and Swing, including a College Admission form and a Resume form. It also demonstrates different layout managers such as Grid Layout, Border Layout, Flow Layout, and Grid Bag Layout. Each example includes the setup of components, their properties, and the main method to run the application.

Uploaded by

iamstudent6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1. WAP to implement all components of AWT.

1) College Admission form


ANS:
import java.awt.*;
public class a_college extends Frame{
Label l1, l2, l3, l4, l5, l6, l7;
TextField t1, t2, t3, t4, t5;
TextArea ta1;
Choice c1;
Checkbox M, F;
Button b1;
a_college(){
setVisible(true);
setSize(600,500);
setLayout(null);
setTitle("College Admission Form");

l1=new Label("Name:");
l2=new Label("DOB:");
l3=new Label("Gender:");
l4=new Label("Email:");
l5=new Label("Phone No:");
l6=new Label("Select Course:");
l7=new Label("Address:");

l1.setBounds(70,50,60,20);
l2.setBounds(70,80,60,20);
l3.setBounds(70,110,60,20);
l4.setBounds(70,140,60,20);
l5.setBounds(70,170,60,20);
l6.setBounds(50,200,90,20);
l7.setBounds(70,230,60,20);

add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);

t1=new TextField();
t2=new TextField();
CheckboxGroup cbg=new CheckboxGroup();
M=new Checkbox("Male",cbg,true);
F=new Checkbox("Female",cbg,false);
t3=new TextField();
t4=new TextField();
t5=new TextField();
c1=new Choice();
c1.add("IT");
c1.add("CO");
c1.add("AN");
ta1=new TextArea(5,4);
b1=new Button("Submit");

t1.setBounds(150,50,150,20);
t2.setBounds(150,80,150,20);
M.setBounds(150,110,50,20);
F.setBounds(200,110,70,20);
t3.setBounds(150,110,150,20);
t4.setBounds(150,140,150,20);
t5.setBounds(150,170,150,20);
c1.setBounds(150,200,150,20);
ta1.setBounds(150,235,150,70);
b1.setBounds(150,320,70,20);

add(t1);
add(t2);
add(t4);
add(t5);
add(M);
add(F);
add(c1);
add(ta1);
add(b1);
}
public static void main(String[] args) {
a_college ac=new a_college();
}
}
2) Resume
ANS:
import java.awt.*;
import java.awt.event.*;
public class BiodataExample extends Frame implements ActionListener{

Label l1, l2, l3, l4, l5, l6, l7, l8, l9;
TextField t1, t2, t3, t4, t5, t6;
TextArea ta1;
CheckboxGroup cbg;
Checkbox M, F;
Choice c1;
Button b1, b2;

BiodataExample(){
setVisible(true);
setSize(500, 500);
setTitle("BIODATA");
setLayout(null);

l1=new Label("Name:");
l2=new Label("Age:");
l3=new Label("Address:");
l4=new Label("Contact No:");
l5=new Label("Email-Id:");
l6=new Label("Gender:");
l7=new Label("Qualification:");
l8=new Label("Skills:");
l9=new Label("Hobbies:");

l1.setBounds(50, 50, 40, 20);


l2.setBounds(50, 80, 40, 20);
l3.setBounds(50, 110, 50, 20);
l4.setBounds(50, 220, 70, 20);
l5.setBounds(50, 250, 70, 20);
l6.setBounds(50, 280, 50, 20);
l7.setBounds(50, 310, 75, 20);
l8.setBounds(50, 340, 75, 20);
l9.setBounds(50, 370, 75, 20);

add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);

t1=new TextField(3);
t1.setBounds(130, 50, 200, 20);
add(t1);
t2=new TextField(3);
t2.setBounds(130, 80, 200, 20);
add(t2);

ta1=new TextArea();
ta1.setBounds(130, 110, 300, 100);
add(ta1);

t3=new TextField(3);
t3.setBounds(130, 220, 200, 20);
add(t3);

t4=new TextField(3);
t4.setBounds(130, 250, 200, 20);
add(t4);

cbg=new CheckboxGroup();
M=new Checkbox("Male", cbg, true);
add(M);
F=new Checkbox("Female", cbg, true);
add(F);
M.setBounds(130, 280, 50, 24);
F.setBounds(190, 280, 70, 24);

c1=new Choice();
c1.add("---------Select Qualification---------");
c1.add("B.Tech");
c1.add("M.Tech");
c1.add("B.Sc");
c1.add("B.com");
c1.setBounds(130, 310, 200, 20);
add(c1);

t5=new TextField(3);
t5.setBounds(130, 340, 200, 20);
add(t5);

t6=new TextField(3);
t6.setBounds(130, 370, 200, 20);
add(t6);

b1=new Button("SUBMIT");
b1.setBounds(100, 410, 70, 30);
add(b1);

b2=new Button("CLEAR");
b2.setBounds(200, 410, 70, 30);
add(b2);
}
public static void main(String[] args) {
BiodataExample bd=new BiodataExample();
}
}
2. WAP to implement all swing components.
1) College Admission Form
ANS:
import javax.swing.*;
public class CollegeForm extends JFrame {
JLabel l1, l2, l3, l4, l5, l6, l7;
JTextField t1, t2, t3, t4, t5;
JTextArea ta1;
JComboBox c1;
JRadioButton M, F;
JButton b1;

CollegeForm() {
// Set up the frame
setVisible(true);
setSize(600, 500);
setLayout(null);
setTitle("College Admission Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Initialize components
l1 = new JLabel("Name:");
l2 = new JLabel("DOB:");
l3 = new JLabel("Gender:");
l4 = new JLabel("Email:");
l5 = new JLabel("Phone No:");
l6 = new JLabel("Select Course:");
l7 = new JLabel("Address:");

l1.setBounds(70, 50, 100, 20);


l2.setBounds(70, 80, 100, 20);
l3.setBounds(70, 110, 100, 20);
l4.setBounds(70, 140, 100, 20);
l5.setBounds(70, 170, 100, 20);
l6.setBounds(50, 200, 120, 20);
l7.setBounds(70, 230, 100, 20);

add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);

t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
t5 = new JTextField();

M = new JRadioButton("Male",true);
F = new JRadioButton("Female");
c1 = new JComboBox();
c1.addItem("IT");
c1.addItem("CO");
c1.addItem("AN");

ta1 = new JTextArea(5, 4);


b1 = new JButton("Submit");

t1.setBounds(150, 50, 150, 20);


t2.setBounds(150, 80, 150, 20);
M.setBounds(150, 110, 70, 20);
F.setBounds(220, 110, 70, 20);
t3.setBounds(150, 140, 150, 20);
t4.setBounds(150, 170, 150, 20);
c1.setBounds(150, 200, 150, 20);
ta1.setBounds(150, 235, 150, 70);
b1.setBounds(150, 320, 100, 30);

add(t1);
add(t2);
add(t3);
add(t4);
add(M);
add(F);
add(c1);
add(ta1);
add(b1);
}

public static void main(String[] args) {


new CollegeForm();
}
}
2) Resume
ANS:

import javax.swing.*;
public class BiodataExample extends JFrame {
JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
JTextField t1, t2, t3, t4, t5, t6;
JTextArea ta1;
JRadioButton M, F;
JComboBox c1;
JButton b1, b2;

BiodataExample() {
// Frame setup
setVisible(true);
setSize(500, 500);
setTitle("BIODATA");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Initialize labels
l1 = new JLabel("Name:");
l2 = new JLabel("Age:");
l3 = new JLabel("Address:");
l4 = new JLabel("Contact No:");
l5 = new JLabel("Email-Id:");
l6 = new JLabel("Gender:");
l7 = new JLabel("Qualification:");
l8 = new JLabel("Skills:");
l9 = new JLabel("Hobbies:");

// Set label bounds


l1.setBounds(50, 50, 70, 20);
l2.setBounds(50, 80, 70, 20);
l3.setBounds(50, 110, 70, 20);
l4.setBounds(50, 220, 70, 20);
l5.setBounds(50, 250, 70, 20);
l6.setBounds(50, 280, 70, 20);
l7.setBounds(50, 310, 100, 20);
l8.setBounds(50, 340, 70, 20);
l9.setBounds(50, 370, 70, 20);

// Add labels to the frame


add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
// Initialize text fields
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
t5 = new JTextField();
t6 = new JTextField();

// Set text field bounds


t1.setBounds(130, 50, 200, 20);
t2.setBounds(130, 80, 200, 20);
t3.setBounds(130, 220, 200, 20);
t4.setBounds(130, 250, 200, 20);
t5.setBounds(130, 340, 200, 20);
t6.setBounds(130, 370, 200, 20);

// Add text fields to the frame


add(t1);
add(t2);
add(t3);
add(t4);
add(t5);
add(t6);

// Initialize text area


ta1 = new JTextArea();
ta1.setBounds(130, 110, 300, 100);
add(ta1);

// Initialize radio buttons and group them


M = new JRadioButton("Male");
F = new JRadioButton("Female");

// Set bounds for radio buttons


M.setBounds(130, 280, 70, 20);
F.setBounds(210, 280, 80, 20);

// Add radio buttons to the frame


add(M);
add(F);

// Initialize combo box


c1 = new JComboBox();
c1.addItem("---------Select Qualification---------");
c1.addItem("B.Tech");
c1.addItem("M.Tech");
c1.addItem("B.Sc");
c1.addItem("B.Com");
c1.setBounds(130, 310, 200, 20);
add(c1);

// Initialize buttons
b1 = new JButton("SUBMIT");
b2 = new JButton("CLEAR");
// Set button bounds
b1.setBounds(100, 410, 100, 30);
b2.setBounds(220, 410, 100, 30);

// Add buttons to the frame


add(b1);
add(b2);
}

public static void main(String[] args) {


new BiodataExample();
}
}
3. Grid Layout || Border Layout || Flow Layout || Grid Bag Layout.

ANS:
Grid Layout:
import java.awt.*;
class GridLayoutDemo extends Frame
{
GridLayoutDemo()
{
setLayout(new GridLayout(3,2,3,3));
Button b1=new Button("Button1");
add(b1);
Button b2=new Button("Button2");
add(b2);
Button b3=new Button("Button3");
add(b3);
Button b4=new Button("Button4");
add(b4);
Button b5=new Button("Button5");
add(b5);
Button b6=new Button("Button6");
add(b6);
setVisible(true);
setSize(500,500);
setTitle("GridLayoutDemo");
}
public static void main(String args[])
{
new GridLayoutDemo();
}
}

Border Layout:
import java.awt.*;
class BorderLayoutDemo extends Frame
{
BorderLayoutDemo()
{
setLayout(new BorderLayout(3,3));
Button north=new Button("North");
add(north,BorderLayout.NORTH);
Button south=new Button("South");
add(south,BorderLayout.SOUTH);
Button east=new Button("EAST");
add(east,BorderLayout.EAST);
Button west=new Button("West");
add(west,BorderLayout.WEST);
setVisible(true);
setSize(500,500);
setTitle("BorderLayoutDemo");
}
public static void main(String args[])
{
new BorderLayoutDemo();
}
}
Flow Layout:
import java.awt.*;
class FlowLayoutDemo extends Frame
{
Button b1,b2;
FlowLayoutDemo()
{
setLayout(new FlowLayout());
b1=new Button("Button1");
add(b1);
b2=new Button("Button2");
add(b2);
setVisible(true);
setSize(500,500);
setTitle("Hello");
}
public static void main(String args[])
{
new FlowLayoutDemo();
}}

Grid Bag Layout:


import java.awt.*;
class GridBagLayoutDemo extends Frame
{
GridBagLayoutDemo(){
Label lblName = new Label("Name");
TextField txtName =new TextField(10);
Label lblcomments = new Label("Comments");
TextArea TAreaComments=new TextArea (6,15);
Button btnSubmit = new Button("Submit");
setLayout(new GridBagLayout());
GridBagConstraints gc =new GridBagConstraints();
add(lblName, gc,0,0,1,1,0,0);
add(txtName, gc,1,0,1,1,0,20);
add(lblcomments, gc,0,1,1,1,0,0);
add(TAreaComments, gc,1,1,1,1,0,60);
add(btnSubmit, gc, 0,2,2,1,0,20);
}
void add(Component comp, GridBagConstraints gc, int x, int y, int w, int h, int wx, int wy)
{
gc.gridx=x;
gc.gridy=y;
gc.gridwidth=w;
gc.gridheight=h;
gc.weightx=wx;
gc.weighty=wy;
add(comp,gc);
}
}
class GridBagLayoutJavaExample
{
public static void main(String args[])
{
GridBagLayoutDemo frame=new GridBagLayoutDemo();
frame.setTitle("GridBagLayout In Java Example");
frame.setSize(300,200);
frame.setVisible(true);
}
}
4. Create Menu
file Edit View
| | |
New Cut Home.
Open Copy
Paste
Disable 'View' Menu
Create Checkbox on Menultem open.
ANS:
import java.awt.*;
import java.awt.event.*;
public class MenuExample extends Frame {
public MenuExample2() {
MenuBar menuBar = new MenuBar();
setMenuBar(menuBar);
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("View");

MenuItem mt1 = new MenuItem("New");


CheckboxMenuItem mt2 = new CheckboxMenuItem("Open");
m1.add(mt1);
m1.add(mt2);

MenuItem mt4 = new MenuItem("Cut");


MenuItem mt5 = new MenuItem("Copy");
MenuItem mt6 = new MenuItem("Paste");
m2.add(mt4);
m2.add(mt5);
m2.add(mt6);

MenuItem mt7 = new MenuItem("Home");


m3.add(mt7);

menuBar.add(m1);
menuBar.add(m2);
menuBar.add(m3);

m3.setEnabled(false);
setSize(400, 400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new MenuExample();
}
}
5. JComboBox in Swing.
ANS:
import javax.swing.*;
public class JComboboxDemo extends JFrame {
public JComboboxDemo()
{
String country[] = {"India", "Aus", "U.S.A", "England", "Newzeland"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50, 90, 20);
add(cb);
setLayout(null);
setSize(400, 500);
setVisible(true);
}
public static void main(String args[]) {
new JComboboxDemo();
}}

6. JTree.
ANS:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class JTreeDemo
{
JTree tree;
JFrame f;
JTreeDemo(){
f=new JFrame();
f.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3=new DefaultMutableTreeNode("B3");
b.add(b3);
tree=new JTree(top);
JScrollPane jsp=new JScrollPane(tree);
f.add(jsp, BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(true);
}
public static void main(String args[]){
JTreeDemo jl=new JTreeDemo();
}}
7. JTable.
ANS:
import javax.swing.*;
public class JTableDemo
{
JFrame f;
JTableDemo()
{
f=new JFrame();
String rows[][]={ {"101", "Amit", "670000"},
{"102", "Jai", "780000"},
{"101", "Sachin", "700000"}
};
String columns[]={"ID", "NAME", "SALARY"};
JTable jt=new JTable (rows, columns);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args)
{
new JTableDemo();
}
}

8. JProgress Bar - use all constructors.


ANS:
import javax.swing.*;
public class ProgressBarExample extends JFrame{
JProgressBar jb;
int i=0, num=0;
ProgressBarExample(){
jb=new JProgressBar (0,2000);
jb.setBounds (40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250, 150);
setLayout(null);
}
public void iterate(){
while(i<=2000) {
jb.setValue(i);
i=i+20;
try{Thread.sleep(150); }
catch (Exception e){}
}}
public static void main(String[] args) {
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}}
9. WAP to accept keyboard input to show the pressed /released status of each Key
on window.
ANS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyListenerEXP10 extends JFrame implements KeyListener
{
Font lf = new Font("Times New Roman",Font.BOLD,30);
JLabel label = new JLabel("KEY EVENTS");
JLabel label2 = new JLabel("KEY PRESSED");
JLabel label3 = new JLabel("KEY TYPED");
JLabel label4 = new JLabel("KEY RELEASED");
JTextField tf = new JTextField(12);

KeyListenerEXP10(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(500,500);
getContentPane().setBackground(new Color(255, 236, 0, 255));

label.setFont(lf);
label2.setFont(lf);
label3.setFont(lf);
label4.setFont(lf);

label.setVisible(true);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);

label.setBounds(30,40,400,50);
label2.setBounds(30,40,400,50);
label3.setBounds(30,40,400,50);
label4.setBounds(30,40,400,50);

tf.setBounds(100,120,400,50);
tf.setVisible(true);

add(label);
add(label2);
add(label3);
add(label4);
add(tf);

addKeyListener(this);
tf.addKeyListener(this);
setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
tf.setBackground(Color.green);
label.setVisible(false);
label2.setVisible(true);
label3.setVisible(false);
label4.setVisible(false);
}

@Override
public void keyTyped(KeyEvent e) {
tf.setBackground(Color.red);
label.setVisible(false);
label2.setVisible(false);
label3.setVisible(true);
label4.setVisible(false);
}

@Override
public void keyReleased(KeyEvent e) {
tf.setBackground(Color.blue);
label.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(true);
}
public static void main(String[] args) {
new KeyListenerEXP10();
}
}
10. WAP to demonstrate Mouse Event.
ANS:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
/*
<applet code="Practical11" width="300" height="300">
</applet>
*/
public class Practical11 extends Applet implements MouseListener
{
String s="See your event here";
public void init()
{
this.addMouseListener(this);
}
public void paint(Graphics g)
{
g.drawString(s,100,100);
}
public void mouseEntered(MouseEvent me)
{
s="MouseEntered";
repaint();
}
public void mouseExited(MouseEvent me)
{
s="Mouse Exited";
repaint();
}
public void mouseClicked(MouseEvent me)
{
s="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent me)
{
s="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
s="Mouse Released";
repaint();
}
}
11. WAP to demonstrate the Use of JTextField and JPassword field.
ANS:
import javax.swing.*;
public class Example {
public static void main(String[] args) {
JFrame f=new JFrame();
JTextField id = new JTextField();
id.setBounds(100, 50, 100, 30);

JPasswordField value = new JPasswordField();


value.setBounds (100, 100, 100,30);
value.setToolTipText("Enter your Password");

JLabel l1=new JLabel("User Id:");


l1.setBounds (30,50, 80,30);

JLabel l2=new JLabel("Password:");


l2.setBounds (20,100, 80,30);

f.add(id);
f.add(value);
f.add(l1);
f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}}
12. WAP that creates Username and pwd use setEchochar().
ANS:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="TextFieldEvent" width=300 height=300></applet>
*/
public class TextFieldEvent extends Applet
{
TextField name,pass;
Button b1,b2;
public void init()
{
Label n=new Label("Name:",Label.CENTER);
Label p=new Label("Password:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('*');
b1=new Button("Submit");
b2=new Button("Cancel");
add(n);
add(name);
add(p);
add(pass);
add(b1);
add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,100,90,60);
name.setBounds(280,100,90,20);
pass.setBounds(200,120,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public void paint(Graphics g)
{
repaint();
}}
13. WAP to perform basic arithmetic operation on 2 no’s using TextField and
Button to handle ActionEvent.
ANS:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/* <applet code ="Applet6" width = 500 height = 500></applet> */

public class Applet6 extends Applet implements ActionListener {

Label label1, label2, label3;


TextField tf1, tf2, tf3;
Button b1, b2, b3, b4;
String whichButtonClk;

public void init() {


System.out.println("Initializing an applet");

label1 = new Label("Number1");


tf1 = new TextField(10);
label2 = new Label("Number2");
tf2 = new TextField(10);
b1 = new Button("Add");
b2 = new Button("Subtract");
b3 = new Button("Multiply");
b4 = new Button("Divide");

add(label1);
add(tf1);
add(label2);
add(tf2);
add(b1);
add(b2);
add(b3);
add(b4);

tf1.addActionListener(this);
tf2.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getActionCommand().equals("Add") || ae.getActionCommand().equals("Subtract")||
ae.getActionCommand().equals("Multiply") || ae.getActionCommand().equals("Divide"))
{
whichButtonClk = ae.getActionCommand();
repaint();
}
}
public void paint(Graphics g) {
g.drawString("Please enter two numbers to perform math operations", 10, 130);
if (tf1.getText().equals("") && tf2.getText().equals("")) {
}
else {

Integer i1 = new Integer(tf1.getText());


Integer i2 = new Integer(tf2.getText());

int sum = i1 + i2;


int subtract = i1 - i2;
int multiply = i1 * i2;
float divide = (float) i1 / (float) i2;

if (whichButtonClk.equals("Add"))
g.drawString("Your sum is " + sum, 10, 190);

if (whichButtonClk.equals("Subtract"))
g.drawString("Your subtract is " + subtract, 10,190);

if (whichButtonClk.equals("Multiply"))
g.drawString("Your multiply is " + multiply, 10,190);

if (whichButtonClk.equals("Divide"))
g.drawString("Your divide is " + divide, 10, 190);
}
}
}
14. WAP to Implement Adapter class.
ANS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAdapter{
JFrame f;
MouseMotionAdapterExample() {
f = new JFrame ("Mouse Motion Adapter");
f.addMouseMotionListener (this);
f.setLayout (null);
f.setVisible (true);
f.setSize(500,500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void mouseDragged (MouseEvent e) {
Graphics g = f.getGraphics();
g.setColor (Color.GRAY);
g.fillOval (e.getX(), e.getY(), 10, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}

15. WAP to implement InetAddress class.


ANS:
import java.io.*;
import java.net.*;
class InetDemo{
public static void main(String args[]){
try{
InetAddress ip=InetAddress.getByName("www.google.com");
System.out.println("host name=" + ip.getHostName());
System.out.println("IP Address=" + ip.getHostAddress());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
16. WAP to implement Factory method of InetAddress.
ANS:
import java.net.*;
class InetAddressDemo
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress ia=InetAddress.getLocalHost();
System.out.println(ia);
ia= InetAddress.getByName("google.com");
System.out.println(ia);

InetAddress sw[] =InetAddress.getAllByName("www.yahoo.com");


for(int i=0; i<sw.length;i++)
System.out.println(sw[i]);
}
}

17. WAP to implement Instance method of InetAddress.


ANS:
import java.net.*;
public class NetDemo3 {
public static void main(String[] args) {
try {
InetAddress local = InetAddress.getLocalHost();
String address = local.getHostAddress();
String hostName = local.getHostName();

System.out.println("Local Host IP Address: " + address);


System.out.println("Local Host Name: " + hostName);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

18. WAP to Display parts of URL.


ANS:
import java.net.*;
class Test {
public static void main(String[] arg) {
try {
URL hp = new URL("http://www.yahoo.com:80/index");

System.out.println("Protocol: " + hp.getProtocol());


System.out.println("Path: " + hp.getPath());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Port: " + hp.getPort());
} catch (MalformedURLException e) {
System.out.println("Invalid URL: " + e.getMessage());
}
}
}
19. WAP to implement Connection of database.
ANS:
import java.sql.*;
public class ConnectionTest{
public static void main(String args[]){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
String url="jdbc:odbc:ABD";
Connection conn=DriverManager.getConnection(url);
System.out.println("Connection to database is created");
} catch(Exception e)
{
System.out.println(e.getMessage());
}}}

20. WAP to implement Create table.


ANS:
import java.sql.*;
public class Create{
public static void main(String args[]){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:ABD";
Connection conn=DriverManager.getConnection(url);
Statement st=conn.createStatement();
st.executeUpdate("create table employee(eid int,ename char)");
System.out.println("Table created");
conn.close();
} catch(Exception e)
{
System.out.println(e.getMessage());
}}}

21. WAP to implement Insert record in table.


ANS:
import java.sql.*;
public class Insert{
public static void main(String args[]){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:ABD";
Connection conn=DriverManager.getConnection(url);
Statement st=conn.createStatement();
st.executeUpdate("insert into employee values(101,'Ajay')");
System.out.println("1 Row inserted");

st.executeUpdate("insert into employee values(102,'Sachin')");


System.out.println("1 Row inserted");
conn.close();
} catch(Exception e)
{
System.out.println(e.getMessage());
}}}
22. WAP to implement update and delete record.
ANS:
import java.sql.*;
public class update_delete{
public static void main(String args[]){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:ABD";
Connection conn=DriverManager.getConnection(url);
Statement st=conn.createStatement();
st.executeUpdate("update employee set ename='Vijay' where eid=101");
System.out.println("Table Updated");

st.executeUpdate("delete * from employee where eid=102");


System.out.println("Row Deleted");

conn.close();
} catch(Exception e)
{
System.out.println(e.getMessage());
}}}

You might also like