AJPQB
AJPQB
AJPQB
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;
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.*;
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);
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.*;
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);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
f.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
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.*;
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"));
}
}
}
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>