Java

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

SLIP 1

1) Design an applet to demonstrate the use of radio button and checkbox

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
public class demo extends Applet
{
public void init()
{
CheckboxGroup lngGrp = new CheckboxGroup();
Checkbox java = new Checkbox("Java", lngGrp, true);
Checkbox cpp = new Checkbox("C++", lngGrp, false);
Checkbox vb = new Checkbox("VB", lngGrp, false);
add(java);
add(cpp);
add(vb);
}
}

Slip-2
1) Write a program to design a form using the components list & Choice
List-
/*<applet code="Subjects" width=300 height=300></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Subjects extends Applet
{
public void init() {
List l1=new List(10);
l1.setBounds(50,60,100,120);
l1.add("DSU");
l1.add("PIC");
l1.add("OOP");
l1.add("CGR");
add(l1);
setLayout(null);
}
public void paint(Graphics g)
{ repaint(); }
Choice-
/*<applet code="News" width=300 height=300></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class News extends Applet {
                public void init() {
                               Choice l1=new Choice(10,true);
                                l1.setBounds(50,60,100,120);
                                l1.add("TImes of India");
                                l1.add("Cwiedu");
                                add(l1);
        setLayout(null); 
       
                }
                public void paint(Graphics g)
                {
                                repaint();
                }}

Slip 3-
1) Write a program to design following output using GridLayout

import java.awt.*;
public class PR3a{
public static void main(String[] args) {
Frame f=new Frame();
Button b1=new Button("Button1");
Button b2=new Button("Button2");
Button b3=new Button("Button3");
Button b4=new Button("Button4");
Button b5=new Button("Button5"); f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(new GridLayout(3,2,30,30));
f.setSize(400,250);
f.setVisible(true);
}
}

Slip-4
1) Write a program to design two level card deck that allows the user to select component
of panel using CardLayout

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements ActionListener
{
CardLayout card; JButton b1, b2, b3; Container c; CardLayoutExample()
{
c=getContentPane(); card=new CardLayout(40,30);
b1=new JButton("Apple"); b2=new JButton("Boy"); b3=new JButton("Cat");
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);
c.add("a",b1); c.add("b",b2); c.add("c",b3);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample(); cl.setSize(400,400);
cl.setVisible(true); cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}}
Slip-5
1) Write a program using AWT to create a menubar in a frame where menubar contains
menu items such as File, Edit, View and the submenu under the File menu item should
contain New and Open
import java.awt.*;
class MenuDemo
{

MenuDemo()
{
Frame f= new Frame("Menu Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("File");
MenuItem i1=new MenuItem("Edit");
MenuItem i2=new MenuItem("View");
MenuItem i3=new MenuItem("New");
MenuItem i4=new MenuItem("Open");

menu.add(i1);
menu.add(i2);
submenu.add(i3);
submenu.add(i4);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]) {
new MenuDemo();
}
}
Slip-6
Write a program using swing to display a Scroll pane & JcomboBox in an frame with the items
English, Marathi, hindi, Sanskrit
Slip-7
1) Write a program to create a Jtree to show root directory and its sub Folders of your system
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample {
JFrame f;
TreeExample(){
f=new JFrame();
DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");
DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");
DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
color.add(red); color.add(blue); color.add(black); color.add(green);
JTree jt=new JTree(style);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeExample(); }}
Slip-8
1) Write a program to demonstrate status of key like keyPress, keyRelease and keyType.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyEventDemo extends Applet implements KeyListener
{
String msg = "";
public void init()
{ addKeyListener(this);
}
public void keyReleased(KeyEvent k)
{ showStatus("Key Released");
repaint();
}
public void keyTyped(KeyEvent k)
{ showStatus("Key Typed");
repaint();
}
public void keyPressed(KeyEvent k)
{ showStatus("Key Pressed");
repaint();
}
public void paint(Graphics g)
{ g.drawString(msg, 10, 10);
}}
/* <applet code="KeyEventDemo" height="400" width="400"> </applet>*/
Slip-9
1) Write a program to change the background color of Applet when user performs events
using Mouse.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
 
public class MouseColor extends Applet implements
MouseMotionListener
{
    public void init()
    {
        addMouseMotionListener(this);
    }
 
    public void mouseDragged(MouseEvent me)
    {
        setBackground(Color.red);
        repaint();
    }
 
    public void mouseMoved(MouseEvent me)
    {
        setBackground(Color.green);
        repaint();
    }
 
}
/*<applet code="MouseColor" width=300 height=300></applet>*/
Slip-10
1) Write a program using Jtextfiled & Jpassword filed to accept password from user and if the
length is less than 6 characters then error message should be displayed “Password length must be
>6 character”.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class demo2 extends JFrame implements ActionListener
{
JButton b1;
JPanel newPanel;
JLabel userLabel, passLabel;
final JTextField textField1,textField3;
JPasswordField textField2;
demo2()
{
userLabel = new JLabel("Username");
textField1 = new JTextField(15);
passLabel = new JLabel("Password");
textField2 = new JPasswordField(15);
textField3 = new JTextField(30);
b1 = new JButton("SUBMIT");
newPanel = new JPanel(new GridLayout(3, 1));
newPanel.add(userLabel);
newPanel.add(textField1);
newPanel.add(passLabel);
newPanel.add(textField2);
newPanel.add(b1);
newPanel.add(textField3);
add(newPanel, BorderLayout.CENTER);
b1.addActionListener(this);
setTitle("LOGIN FORM");
}

public void actionPerformed(ActionEvent ae)


{
if (textField2.getPassword().length<6) {
textField3.setText("Password length must be >6 character");
}
else{
textField3.setText(" ");
}
}
}
class LoginFormDemo
{
public static void main(String arg[])
{
demo2 form = new demo2();
form.setSize(300,100);
form.setVisible(true);
}
}
Slip-11
1) Develop a program using InetAddress class to retrieve IP address of computer when hostname
is entered by the user.
import java.io.*;
import java.net.*;
public class NetDemo
{
public static void main(String[] args)
{
try
{
InetAddress ip=InetAddress.getByName("www.javatpoint.com");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e)
{
System.out.println(e);
}
}
}

Slip-12
1) Write a program using URL class to retrieve the host, protocol, port and file of URL
http://www.msbte.org.in.
import java.net.URL;
import java.net.MalformedURLException;
public class URLRetrive
{
public static void main(String[] args) throws MalformedURLException
{
URL url = new URL("https://msbte.org.in/");
System.out.println("Authority: "+ url.getAuthority());
System.out.println("Default Port: "+ url.getDefaultPort());
System.out.println("File: "+ url.getFile());
System.out.println("Path: "+ url.getPath());
System.out.println("Protocol: "+ url.getProtocol());
System.out.println("Reference: "+ url.getRef());
}
}

Slip-13
1) Write a program to insert & retrieve the data from database using JDBC.
Program to insert data
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
public class InsertStaticOracle
{
public static void main(String args[])
{
Statement st = null;
Connection connection = null;
try{
oracle.jdbc.OracleDriver driverObj = new oracle.jdbc.OracleDriver();
String url = "jdbc:oracle:thin:@localhost:1521:XE", username = "System" ,password = "pass";
connection =DriverManager.getConnection(url,username,password);
if(connection!=null)
System.out.println("Connection established successfully");
st = connection.createStatement();
String qry = "Insert into Student values(104 ,'Atharva Agrawal','Dhule')";
int count = st.executeUpdate(qry);
System.out.println(count+" Statement Created Successfully ");
}
catch(Exception e)
{ e.printStackTrace();
}
finally{
try
{ if(st != null)
st.close();
if(connection != null)
connection.close();
}
catch(SQLException e){
e.printStackTrace();
}}}}
Program to retrieve data
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
 
public class SelectOracle
{
 public static void main(String args[]) throws SQLException
 {
  System.out.println("Step 1: ");
  oracle.jdbc.driver.OracleDriver obj = new oracle.jdbc.driver.OracleDriver();
  // Class.forName(oracle.jdbc.driver.OracleDriver);
  System.out.println("Driver loaded successfully");
 
  System.out.println("Step 2: ");
  String url="jdbc:oracle:thin:@localhost:1521:XE",uname="SYSTEM" ,
password="Atharva007";
  Connection connection = DriverManager.getConnection(url,uname,password);
  if(connection!=null)
   System.out.println("Connection Established Successfully");
  else
   System.out.println("Connection Not Established Successfully");
 
   
  System.out.println("Step 3: ");
  Statement st = connection.createStatement();
  System.out.println("Statement Referenced   ");
 
  System.out.println("Step 4: ");
  System.out.println("Step 5: ");
  String qry = "select * from Student";
  ResultSet rs = st.executeQuery(qry);
  System.out.println("rs: "+rs);
   
  System.out.println("Step 6: ");
  System.out.println("Id\tName\taddress");
  while(rs.next())
  {
   int x = rs.getInt(1);
   String y = rs.getString("Name");
   String s = rs.getString(3);
   System.out.println(x+"\t"+y+"\t"+s);
  }   
     
  System.out.println("Step 7: ");
  rs.close();
  st.close();
  connection.close();
 }
}

Slip-14
1) Write a servlet program to display hello message
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException {
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{}
}

Slip-15
1) Design an applet to create form using Text Field, Text Area, Button and Label.
import java.awt.*;
import java.applet.*;
/*<applet code=”BasicAWT.class” width=”300” height=”300”></applet>*/
public class BasicAWT extends applet
{
public void init()
{
Label l1 = new Label();
l1.setText("Enter Your Name ");
TextField tf = new TextField("Atharva");
Label l2 = new Label("Address");
TextArea ta = new TextArea("",3,40);
Button b = new Button("Submit");
add(l1);
add(tf);
add(l2);
add(ta);
add(b);
}
}

Slip-16
1) Develop a program to select multiple languages known to user(eg.
Marathi,Hindi,English,Sanskrit).
import java.awt.*;
public class Lang(
lang()
{
Frame f= new Frame();
Choice c=new Choice();
c.setBounds(100,100, 75,75);
c.add("Marathi");
c.add("Hindi");
c.add("English");
c.add("Sanskrit");
c.add("Gujarati");
f.add(c);f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new lang();
}}

Slip-17
1) Write a program to create three Buttons with Caption OK,Reset and Cancel using frame.
import java.awt.*;
class But
{ But()
{     Frame f=new Frame();
                Button b1=new Button("Ok");
                b1.setBounds(100,50,50,50);
                f.add(b1);
                Button b2=new Button("Reset");
                b2.setBounds(100,101,50,50);
                f.add(b2);
                Button b3=new Button("Cancel");
                b3.setBounds(100,150,80,50);
                f.add(b3);
                f.setSize(500,500);
                f.setLayout(null);
                f.setVisible(true);
}
public static void main(String a[])
{    new But();
}}

               
             
Slip-18

1) Develop an applet using List component to add names of 10 different cities.

import java.awt.*; 
public class ChoiceDemo
{  public static void main(String args[])
    {
        Frame f = new Frame();
        f.setSize(400,400);
        f.setVisible(true);
        f.setLayout(new FlowLayout());  
        List l = new List();
        l.add("Pune");
        l.add("Nasik");
        l.add("Mumbai");
        l.add("Solapur");
        l.add("Saswad");
        l.add("Nagpur");
        l.add("Dehradun");
        l.add("Sangli");
        l.add("Kolhapur");
        l.add("Goa");
        f.add(l);
    }
}

Slip-19

1) Write a program to generate following output using border layout .


import java.awt.*;
public class BorderDemo
{
    public static void main( String args[] )
    {
        Frame f = new Frame();
        f.setVisible(true);
        f.setSize(400,400);
        f.setLayout(new BorderLayout());
         
        Button b1 = new Button("North");   
        Button b2 = new Button("South");   
        Button b3 = new Button("East");
        Button b4 = new Button("West");
        Button b5 = new Button("Center");  
         
        f.add(b1,BorderLayout.NORTH);
        f.add(b2,BorderLayout.SOUTH);
        f.add(b3,BorderLayout.EAST);
        f.add(b4,BorderLayout.WEST);
        f.add(b5,BorderLayout.CENTER); }}
Slip-20

1) Write a program to generate following output using gridbag layout.

import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends JFrame{
public static void main(String[] args)
{
GridBagLayoutExample a = new GridBagLayoutExample();
}
public GridBagLayoutExample() {
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Slip-21

1) Write a program which creates Menu of different colors and disable menu item for Black
color
import java.awt.*;
class MenuEx extends Frame
{
MenuEx()
{
MenuBar mr=new MenuBar();
setMenuBar(mr);
Menu m1=new Menu("Colours");
MenuItem mn1=new MenuItem("RED");
MenuItem mn2=new MenuItem("YELLOW");
MenuItem mn3=new MenuItem("BLACK");
mn3.setEnabled(false);
MenuItem mn4=new MenuItem("BLUE");
MenuItem mn5=new MenuItem("GREEN");
m1.add(mn1);
m1.add(mn2);
m1.add(mn3);
m1.add(mn4);
m1.add(mn5);
mr.add(m1);
}
}
class MenuBarEx
{
public static void main(String args[])
{
MenuEx m=new MenuEx();
m.setTitle("Menu Bar");
m.setSize(500,500);
m.setVisible(true);
}
}

Slip-22

1) Develop a program to accept two numbers and display product of two


numbers when user pressed “Multiply” button.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Q2 extends Applet implements ActionListener
{
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(10);
Label l1 = new Label("FIRST NO=:");
Label l2 = new Label("SECOND NO:");
Label l3 = new Label("SUM:");
Button b = new Button("ADD");
public void init()
{
t1.setForeground(Color = Red);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
t3.setText(" " + (n1 * n2));
}
}
}

Slip-23

1) Write a program to count the number of clicks performed by the user in a frame
window
import javax.swing.*;
import java.awt.event.*;
import java.util.List;
import java.awt.*;

public class demo extends Frame{


demo(){
Label l = new Label("hi");
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
int a = e.getClickCount();
l.setText("count:"+a);
}
public void mousePressed(MouseEvent e){
int a = e.getClickCount();
l.setText("count:"+a);
}
public void mouseReleased(MouseEvent e){
int a = e.getClickCount();
l.setText("count:"+a);
}
public void mouseEntered(MouseEvent e){
int a = e.getClickCount();
l.setText("count:"+a);
}
public void mouseExited(MouseEvent e){
int a = e.getClickCount();
l.setText("count:"+a);
}
}
);
add(l);
setLayout(new FlowLayout(FlowLayout.CENTER));
setVisible(true);
setSize(500,400);
}
public static void main(String[] args){
demo m= new demo();
}
}

Slip-24

1) Write a program to demonstrate the use of mouseDragged and mouseMoved


method of MouseMotionListener
Slip-25

1) Write a program using URL and URLConnection class to retrieve the date,
content type, content length information of any entered URL.

import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import  java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
 
public class URLInfo
{
    public static void main(String[] args) throws IOException ,
MalformedURLException
    {
        Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter any Url: ");
        String ad = sc.nextLine();
 
        URL url = new URL(ad);
        URLConnection uc = url.openConnection();
 
        System.out.println("Date:"+ new Date(uc.getDate()) );
        System.out.println("Content Type: "+
uc.getContentType());
        System.out.println("Content Length: "+
uc.getContentLength());
         
    }
}

You might also like