Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Sunday, 15 May 2016

Applet Program : Implement ItemListener

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ItemLis extends Applet implements ItemListener
{
Label l1,l2,l3,l4,l5,l6;
TextField t1,t2,t3,t4;
Choice ch1,ch2;
int i,qty;
float pri;

public void init()
{
l1=new Label("Item Name");
l2=new Label("Quantity    ");
l3=new Label("Price           ");
l4=new Label("Amount      ");
l5=new Label("Tax              ");
l6=new Label("With Tax     ");
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
t4=new TextField(15);
ch1=new Choice();
ch1.addItem("Burger");
ch1.addItem("Pizza");
ch2=new Choice();
for(i=1;i<10;i++)
ch2.addItem(String.valueOf(i));
setBackground(Color.green);
t3.setText("12.24%");
add(l1);
add(ch1);
add(l2);
add(ch2);
add(l3);
add(t1);
add(l4);
add(t2);
add(l5);
add(t3);
add(l6);
add(t4);
ch1.addItemListener(this);
ch2.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch1)
{
String inm=String.valueOf(ie.getItem());
if(inm.equals("Burger"))
{
t1.setText("Rs.75.00");
pri=75.0f;
}
else if(inm.equals("Pizza"))
{
t1.setText("Rs.125.00");
pri=125.0f;
}
}
if(ie.getSource()==ch2)
{
qty=Integer.parseInt(String.valueOf(ie.getItem()));
float amt=pri*qty;
t2.setText(String.valueOf(amt));
float tamt=amt+.1224f*amt;
t4.setText(String.valueOf(tamt));

}
}
}
/*
<applet code=ItemLis height=300 width=250></applet>
*/

Applet program : implement focus listener

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FocusLis extends Applet implements FocusListener
{
Label l1,l2,l3,l4,l5,l6,l7;
TextField t1,t2,t3,t4,t5,t6,t7;
public void init()
{
l1=new Label("Enter Name");
l2=new Label("Marks 1       ");
l3=new Label("Marks 1       ");
l4=new Label("Marks 1       ");
l5=new Label("Marks 1       ");
l6=new Label("Total             ");
l7=new Label("Average      ");
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
t4=new TextField(15);
t5=new TextField(15);
t6=new TextField(15);
t7=new TextField(15);
setBackground(Color.green);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(l5);
add(t5);
add(l6);
add(t6);
add(l7);
add(t7);
t5.addFocusListener(this);
t1.addFocusListener(this);
}
public void focusGained(FocusEvent fe)
{
if(fe.getSource()==t1)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
t7.setText("");

}
}
public void focusLost(FocusEvent fe)
{
if(fe.getSource()==t5)
{
int m1=Integer.parseInt(t2.getText());
int m2=Integer.parseInt(t3.getText());
int m3=Integer.parseInt(t4.getText());
int m4=Integer.parseInt(t5.getText());
int tot=m1+m2+m3+m4;
t6.setText(String.valueOf(tot));
t7.setText(String.valueOf(tot/4.0f));

}
}

}
/*
<applet code=FocusLis height=300 width=250></applet>
*/

Applet Program : Data selection from data base and use all the basic components and Frame , ActionListener,ItemListener

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Select1 extends Frame implements ActionListener,ItemListener
{
Label l1,l2,l3,l4,l5;
Choice ch1,ch2;
TextField t1,t2,t3,t4;
Button b1,b2,b3,b4,b5;
String cnm;
int sem;
Select1()
{
l1=new Label("enter roll no to get Info... ");
l2=new Label("Name    ");
l3=new Label("Course  ");
l4=new Label("Sem      ");
l5=new Label(" ");
ch1=new Choice();
ch1.addItem("BCA");
ch1.addItem("BBA");
ch1.addItem("BTech");
ch1.addItem("MBA");
ch1.addItem("MCA");
ch2=new Choice();
for(int i=1;i<=8;i++)
ch2.addItem(String.valueOf(i));
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
t4=new TextField(15);
//b1=new Button("Save");
//b2=new Button("delete");
//b3=new Button("cancle");
b4=new Button("clear");
b5=new Button("select");
setLayout(new FlowLayout());
setBackground(Color.yellow);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(ch1);
add(l4);
add(ch2);
add(b1);
//add(b2);
//add(b3);
//add(b4);
add(l5);
add(b5);
//b1.addActionListener(this);
//sb2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
ch1.addItemListener(this);
ch2.addItemListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b3)
{
l5.setText("You have Pressed Cancled Button");

}
else if(ae.getSource()==b4)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
else if(ae.getSource()==b5)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select * from table1 where course='"+cnm+"'");
while(res.next())
{
t2.setText(res.getString(2));
}
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch1)
{
cnm=String.valueOf(ie.getItem());
}
if(ie.getSource()==ch2)
{
sem=Integer.parseInt(String.valueOf(ie.getItem()));
}
}
public static void main(String a[])
{
Select1 as=new Select1();
as.setSize(300,200);
as.setVisible(true);
as.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
}
}

Applet Program : JFrame and ActionListener

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

public class AddNewItem extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3,t4;
JButton b1,b2,b3;

AddNewItem()
{
l1=new JLabel("Product Name ");
l2=new JLabel("Product Price   ");
l3=new JLabel("Quantity           ");
l4=new JLabel("Product Type   ");
t1=new JTextField(15);
t2=new JTextField(15);
t3=new JTextField(15);
t4=new JTextField(15);
b1=new JButton("Clear");
b2=new JButton("Save ");
b3=new JButton("Close");
setLayout(new FlowLayout());
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b2)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error :: "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Invent");
Statement st=con.createStatement();
String qry="Insert into prodmaster(prodnm,price,qty,prodtype,mfrdate) values('"+t1.getText()+"',"+t2.getText()+","+t3.getText()+",'"+t4.getText()+"',date())";
int ans=st.executeUpdate(qry);
if(ans==1)
System.out.println("Data Saved... ");
}
catch(SQLException se)
{
System.out.println("Problems :: "+se.getMessage());
}
}
}
public static void main(String ar[])
{
AddNewItem ani=new AddNewItem();
ani.setSize(300,250);
ani.setVisible(true);
}
}

Applet program :use of Frame ,ActionListener,ItemListener

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class AddStudent1 extends Frame implements ActionListener,ItemListener
{
Label l1,l2,l3,l4,l5;
Choice ch1,ch2;
TextField t1,t2;
Button b1,b2,b3,b4,b5,b6;
String cnm;
int sem;
AddStudent1()
{
l1=new Label("Roll No. ");
l2=new Label("Name    ");
l3=new Label("Course  ");
l4=new Label("Sem      ");
l5=new Label("                                ");
ch1=new Choice();
ch1.addItem("BCA");
ch1.addItem("BBA");
ch1.addItem("BTech");
ch1.addItem("MBA");
ch1.addItem("MCA");
ch2=new Choice();
for(int i=1;i<=8;i++)
ch2.addItem(String.valueOf(i));
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Save");
b2=new Button("delete");
b3=new Button("cancle");
b4=new Button("clear");
b5=new Button("select");
b6=new Button("Update");
setLayout(new FlowLayout());
setBackground(Color.yellow);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(ch1);
add(l4);
add(ch2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l5);
add(b5);
add(b6);
//b1.addActionListener(this);
b2.addActionListener(this);
//b3.addActionListener(this);
//b4.addActionListener(this);
//b5.addActionListener(this);
b6.addActionListener(this);
ch1.addItemListener(this);
ch2.addItemListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
String qry="Insert into table1 values("+Integer.parseInt(t1.getText())+",'"+t2.getText()+"','"+cnm+"',"+sem+")";
int ans=st.executeUpdate(qry);
if(ans==1)
System.out.println("New Student Added Successfully !! ");
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}
if(ae.getSource()==b6)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select * from table1 where rollno="+t1.getText()+"");
while(res.next())
{
String s=res.getString(0);
t2.setText(s);
}
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch1)
{
cnm=String.valueOf(ie.getItem());
}
if(ie.getSource()==ch2)
{
sem=Integer.parseInt(String.valueOf(ie.getItem()));
}
}
public static void main(String a[])
{
AddStudent as=new AddStudent();
as.setSize(300,200);
as.setVisible(true);
as.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
}
}

java program : Implementing all the basic methods in one program

import java.util.*;
class Demo extends Thread
{
public void run()
{
System.out.println("Thread is running simuntaneously");
System.out.println("\n");
}
}
abstract class Shape
{
static String user;
void display()
{
System.out.println("The color of this shape is :");
}
void get()
{
Scanner s1=new Scanner(System.in);
System.out.println("Enter your name");
user=s1.next();
String s2=user.toUpperCase();
String name=new String("Welcome\t");
System.out.println(name.concat(s2));
System.out.println("\n");
}
abstract void put();
}
class Hello extends Shape
{
void put()
{
System.out.println("Welcome user");
}
}
interface Calculate
{
void area();
}
class Rectangle extends Shape implements Calculate
{
int length,breadth;
Rectangle()
{
this.length=length;
this.breadth=breadth;
System.out.println("This is the rectangle details");
Scanner sr=new Scanner(System.in);
System.out.println("Enter the length of the rectangle");
length=sr.nextInt();
System.out.println("Enter the length of the rectangle");
breadth=sr.nextInt();

}
public void area()
{
int ar=length*breadth;
System.out.println("The area of the rectangle is :"+ar);
}
void display()
{
super.display();
System.out.println("Red");
System.out.println("\n");
}
void put()
{
System.out.println("End of details");
}
}
class Triangle extends Shape implements Calculate
{
int length,breadth,height;
Triangle()
{
this.length=length;
this.breadth=breadth;
this.height=height;
System.out.println("This is the triangle details");
Scanner sr=new Scanner(System.in);
System.out.println("Enter the length of the triangle");
length=sr.nextInt();
System.out.println("Enter the breadth of the triangle");
breadth=sr.nextInt();
System.out.println("Enter the height of the Triangle");
height=sr.nextInt();
}
public void area()
{
int ar=length*breadth*height;
System.out.println("The area of the triangle is :"+ar);
}
void display()
{
super.display();
System.out.println("Blue");
}
void put()
{
System.out.println("End of details");
}
}
class Demo32
{
public static void main(String st[])
{
try
{
Shape sh=new Hello();
sh.get();
int ch;
Scanner s=new Scanner(System.in);
System.out.println("Enter the choice 1.Triangle 2.Rectangle");
ch=s.nextInt();
switch(ch)
{
case 1:
{
Demo d=new Demo();
d.start();
Triangle tri=new Triangle();
tri.area();
tri.display();
tri.put();
break;
}
case 2:
{
Demo d1=new Demo();
d1.start();
Rectangle rect=new Rectangle();
rect.area();
rect.display();
rect.put();
break;
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

java program : Implementing all the basic methods in one program

import java.util.*;
class Demo extends Thread
{
public void run()
{
System.out.println("Thread is running simuntaneously");
System.out.println("\n");
}
}
abstract class Shape
{
static String user;
void display()
{
System.out.println("The color of this shape is :");
}
void get()
{
Scanner s1=new Scanner(System.in);
System.out.println("Enter your name");
user=s1.next();
String s2=user.toUpperCase();
String name=new String("Welcome\t");
System.out.println(name.concat(s2));
System.out.println("\n");
}
abstract void put();
}
class Hello extends Shape
{
void put()
{
System.out.println("Welcome user");
}
}
interface Calculate
{
void area();
}
class Rectangle extends Shape implements Calculate
{
int length,breadth;
Rectangle()
{
this.length=length;
this.breadth=breadth;
System.out.println("This is the rectangle details");
Scanner sr=new Scanner(System.in);
System.out.println("Enter the length of the rectangle");
length=sr.nextInt();
System.out.println("Enter the length of the rectangle");
breadth=sr.nextInt();

}
public void area()
{
int ar=length*breadth;
System.out.println("The area of the rectangle is :"+ar);
}
void display()
{
super.display();
System.out.println("Red");
System.out.println("\n");
}
void put()
{
System.out.println("End of details");
}
}
class Triangle extends Shape implements Calculate
{
int length,breadth,height;
Triangle()
{
this.length=length;
this.breadth=breadth;
this.height=height;
System.out.println("This is the triangle details");
Scanner sr=new Scanner(System.in);
System.out.println("Enter the length of the triangle");
length=sr.nextInt();
System.out.println("Enter the breadth of the triangle");
breadth=sr.nextInt();
System.out.println("Enter the height of the Triangle");
height=sr.nextInt();
}
public void area()
{
int ar=length*breadth*height;
System.out.println("The area of the triangle is :"+ar);
}
void display()
{
super.display();
System.out.println("Blue");
}
void put()
{
System.out.println("End of details");
}
}
class Demo32
{
public static void main(String st[])
{
try
{
Shape sh=new Hello();
sh.get();
int ch;
Scanner s=new Scanner(System.in);
System.out.println("Enter the choice 1.Triangle 2.Rectangle");
ch=s.nextInt();
switch(ch)
{
case 1:
{
Demo d=new Demo();
d.start();
Triangle tri=new Triangle();
tri.area();
tri.display();
tri.put();
break;
}
case 2:
{
Demo d1=new Demo();
d1.start();
Rectangle rect=new Rectangle();
rect.area();
rect.display();
rect.put();
break;
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}