You are on page 1of 12

1)Design an applet to create biodata using following components.

Button, Choice, List, TextField,


TextArea, Label, RadioButton

import java.awt.*;
public class practice {
public static void main(String args[])
{
Frame f=new Frame();
f.setSize(500,300);
f.setLayout(new GridLayout(7,2));
Label namelLabel =new Label("Name");
TextField namField=new TextField();
Label addLabel=new Label("address");
TextArea addArea=new TextArea();
CheckboxGroup cbg=new CheckboxGroup();
Label age=new Label("Are you above 18?");
Checkbox MaleCheckbox=new Checkbox("Above 18",cbg,false);
Label l=new Label("Select your gender");
Choice c=new Choice();
c.addItem("male");
c.addItem("female");
c.addItem("gender");
Label listl=new Label("Knowledge required:");
List list=new List();
list.add("C++");
list.add("Java");
list.add("Python");
Button submitButton= new Button("Submit");
Label l1=new Label(" ");

f.add(namelLabel);
f.add(namField);
f.add(addLabel);
f.add(addArea);
f.add(age);
f.add(MaleCheckbox);
f.add(l);
f.add(c);
f.add(listl);
f.add(list);
f.add(l1);
f.add(submitButton);
f.setVisible(true);
}

}}
2) Develop a program to create six buttons and apply GridLayout.

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;

public class Exp2 {

public static void main(String[] args) {


Frame frame = new Frame("Button GridLayout Example");

Button button1 = new Button("Button 1");


Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
Button button5 = new Button("Button 5");
Button button6 = new Button("Button 6");

frame.setLayout(new GridLayout(2, 3));


frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

frame.setSize(300, 200);
frame.setVisible(true);
}
}
3) WAP which will create checkable menu item ‘Picture’ under Insert Menu and ‘Paste’ menu item
under the menu Home.

import javax.swing.*;

public class Exp3 {


public static void main(String[] args) {
JFrame frame = new JFrame("Menu Item Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();


JMenu insertMenu = new JMenu("Insert");
JMenu homeMenu = new JMenu("Home");

JCheckBoxMenuItem pictureMenuItem = new JCheckBoxMenuItem("Picture");


JMenuItem pasteMenuItem = new JMenuItem("Paste");

insertMenu.add(pictureMenuItem);
homeMenu.add(pasteMenuItem);

menuBar.add(insertMenu);
menuBar.add(homeMenu);

frame.setJMenuBar(menuBar);

frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
4)Create a frame having title as “changing colors” with a provision to select a particular among Red,
Green, and Blue. Make use of JComboBox.

import java.awt.FlowLayout;

import javax.swing.*;
public class practice
{
public static void main(String args[])
{
JFrame f = new JFrame("Practice");
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.setVisible(true);

String colors[]={"red","blue","green"};
JComboBox<String> combo=new JComboBox<String>(colors);
f.add(combo);

}
}
5)WAP to create three radio buttons once user click on button background color will change such as
red, green and blue.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class practice
{
public static void main(String args[])
{
final JFrame f=new JFrame();
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.setVisible(true);

final JRadioButton r1=new JRadioButton("red");


final JRadioButton r2=new JRadioButton("blue");
final JRadioButton r3=new JRadioButton("green");

ActionListener al=new ActionListener(){


public void actionPerformed(ActionEvent e)
{
if (r1.isSelected())
{
f.getContentPane().setBackground(Color.RED);
}
else if (r2.isSelected())
{
f.getContentPane().setBackground(Color.BLUE);
}
else if(r3.isSelected())
{
f.getContentPane().setBackground(Color.GREEN);
}
else
{
f.getContentPane().setBackground(Color.WHITE);
}
}
};
r1.addActionListener(al);
r2.addActionListener(al);
r3.addActionListener(al);

f.add(r1);
f.add(r2);
f.add(r3);

}
}

6) Develop a program which will implement various methods of Mouse Motion Listener.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exp6 extends JFrame {


public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
Graphics g = frame.getGraphics();
g.setColor(Color.YELLOW);
g.fillOval(e.getX(), e.getY(), 20, 20);
}

public void mouseMoved(MouseEvent e) {


Graphics g = frame.getGraphics();
g.setColor(Color.RED);
g.fillOval(e.getX(), e.getY(), 20, 20);
}
});
}
}
7) Develop a program which will implement various methods of KeyListener.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class practice


{
public static void main(String[] args) {
final JFrame f = new JFrame("Practice");
f.setSize(500,500);
f.setVisible(true);

final Label l=new Label("");


f.add(l);
f.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e)
{
l.setText("key pressed");
}
public void keyReleased(KeyEvent e)
{
l.setText("key released");
}
public void keyTyped(KeyEvent e)
{
l.setText("key Typed");
}
});
}
}
8) Develop a program to create an applet to accept a no in text field and display the square of the no
when a button with caption Square is pressed.

9) WAP which will make use of Mouse Adapter class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Practice {


public static void main(String[] args) {
JFrame f = new JFrame("Practice");
f.setSize(500, 500);
final JLabel l = new JLabel("");
f.add(l);

f.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}

public void mouseExited(MouseEvent e) {


l.setText("Mouse Exited");
}
});

f.setVisible(true);
}
}

10) WAP to send user name to the server and server will send “Hello <user name>” to client using TCP.
Server:
import java.io.*;
import java.net.*;
import java.util.*;
public class sender
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(1234);
Socket s=ss.accept();
Scanner sc=new Scanner(System.in);
System.out.println("Enter yoour name");
String us=sc.nextLine();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(us);
}
catch(Exception e)
{

}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.*;

public class receiver {


public static void main(String[] args)
{
try{
Socket s=new Socket("localhost", 1234);
DataInputStream dis=new DataInputStream(s.getInputStream());
String user=dis.readUTF();
System.out.println("Hello"+user);
}
catch(Exception e){}
}

11) WAP to send a no through client to the server and server will send the message to the client that the
no. is prime or not.

Server Code:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class sender
{
public static boolean isPrime(int number)
{
boolean isPrimenum=false;
int i=(int)Math.ceil(Math.sqrt(number));
while (i>1)
{
if((number!=i)&&(number%i==0))
{
isPrimenum=false;
break;
}
else if(!isPrimenum)
{
isPrimenum=true;
}
i--;
}
return isPrimenum;
}
public static void main(String[] args)
{
try
{
ServerSocket ss=new ServerSocket(1234);
Socket s= ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
int num=dis.readInt();
System.out.println("Number sent by client: "+num);
dos.writeBoolean(isPrime(num));
}
catch(Exception e)
{}
}
}
Client Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class receiver
{
public static void main(String[] args) throws IOException
{
Socket s=new Socket("localhost",1234);
Scanner sc=new Scanner(System.in);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
System.out.println("Enter any number");
int num=sc.nextInt();
dos.writeInt(num);
boolean isPrime=dis.readBoolean();
if(isPrime)
{
System.out.println("It is a prime number");
}
else
{
System.out.println("It is not a prime number");
}
}
}

12)WAP to create student table in database having three columns rollno,name and marks , Insert some
values into table and display records of those students whose marks > 70.

import java.io.*;
import java.sql.*;
class Expt_18_Ex_2
{
public static void main(String args[])throws Exception
{
String url="jdbc:odbc:Sanika";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
Statement s= c.createStatement();
System.out.println("Roll no\t Name");
ResultSet r = s.executeQuery("select Roll_no,Name from student where Marks>70");
while(r.next())
{
System.out.println(r.getInt("Roll_no")+"\t "+ r.getString("Name"));
}
}
}

13) WAP for given output

 TPOLY
 CO
 FYCO
 SYCO
 TYCO
CE
ME
IF
14) WAP to create JTable on JApplet window.
import java.awt.BorderLayout;

import javax.swing.*;
import javax.swing.table.*;
public class practice extends JApplet
{
public void init()
{
String[] columnNames={"one","two","three"};
Object[][] data={{1,2,3},{4,5,6},{7,8,9}};
DefaultTableModel dtm= new DefaultTableModel(data,columnNames);
JTable table=new JTable(dtm);
JScrollPane scrollPane=new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
}
}
// <applet code="practice.class" width="300" height="300">
// </applet>

You might also like