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

Saturday 21 May 2016

Patterns questions and answers in java

Ques1.)       1
                 121
                12321
              1234321
            123454321
          12345654321

        1234567654321     

Ques2.   123456787654321
                 1234567654321
                   12345654321
                     123454321
                       1234321
                         12321
                           121
                             1

Ques3.                                1                 1
                                           12              21
                                           123          321
                                           1234      4321
                                           12345  54321
                                           12345654321


Ques 4.)  12345654321
 12345  54321
 1234      4321
             123           321
             12                21
             1                    1

Ques 5.)  
        1
      232
    34543
  4567654
567898765



Ques 6.)   
    1
                       1   2   1
                   1   2   4   2   1
               1   2   4   8   4   2   1
           1   2   4   8  16   8   4   2   1
       1   2   4   8  16  32  16   8   4   2   1
   1   2   4   8  16  32  64  32  16   8   4   2   1


Ques 7.)  
1
11
121
1331
14641
161051
1771561
19487171

Sunday 15 May 2016

console based Quiz application java using file handling

QuizTest.java 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class QuizTest {
public static void main (String[] args) throws IOException{

    String question = "";
    String choiceA = "";
    String choiceB = "";
    String choiceC = "";
    String choiceD = "";
    String answer = "";
    String line;
    String userAnswer;
    int i = 0;
    BufferedReader reader = null;          

        reader = new BufferedReader(new FileReader("Caitto.txt"));
        Scanner scan = new Scanner(System.in);

        QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);

        while((line = reader.readLine()) != null){

            //Finds the question
            if(line.contains("?")){
                run.question = line;
                System.out.println(run.getQuestion() + "\n");
            }

            //Finds answer "A"
            if(line.contains("A.")){
                run.choiceA = line;
                System.out.println(run.getChoiceA());
            }

            //Finds answer "B"
            if(line.contains("B.")){
                run.choiceB = line;
                System.out.println(run.getChoiceB());
            }

            //Finds answer "C"
            if(line.contains("C.")){
                run.choiceC = line;
                System.out.println(run.getChoiceC());
            }
            //Finds answer "D"
            if(line.contains("D.")){
                run.choiceD = line;
                System.out.println(run.getChoiceD() + "\n");
            }

            //Finds the correct answer for the question
            if(line.contains("Correct Answer:"))
            {
                //System.out.println("correct answer block");
                String[] a = line.split(":");
                answer = a[a.length - 1];
                run.answer = answer;
               // System.out.print(" Answer:"+answer);
                System.out.print("Your Answer:");
                userAnswer = scan.next();

                    //Checks if the user's input matches the correct answer from the file
                    if(userAnswer.equalsIgnoreCase(answer)){
                        System.out.println("Correct!\n");
                        i++;
                    }

                    //Checks if the user's input doesn't match the correct answer from the file
                    else if (!userAnswer.equalsIgnoreCase(answer)) {
                        System.out.println("Wrong, the correct answer was: " + run.getAnswer());
                        i++;
                    }

            }


        }

                if(i == 274){
                    reader.close();
                    scan.close();
                }
}
}




QuizClass.java 


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
public class QuizClass {

    String question, answer, choiceA, choiceB, choiceC, choiceD;
    private int questionNum;

    public QuizClass(int questionNum, String question, String choiceA, String choiceB, String choiceC, String choiceD, String answer){
        this.question = question;
        this.answer = answer;
        this.choiceA = choiceA;
        this.choiceB = choiceB;
        this.choiceC = choiceC;
        this.choiceD = choiceD;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public String getChoiceA(){
        return choiceA;
    }

    public String getChoiceB(){
        return choiceB;
    }

    public String getChoiceC(){
        return choiceC;
    }

    public String getChoiceD(){
        return choiceD;
    }

    public int getQuestionNum(){
        return questionNum;
    }


}

Math class methods for finding power, absolute value, min, max and rounding number etc in java

import java.lang.Math;

public class MathDemo {

   public static void main(String[] args) {

   // get some doubles to find their absolute values
   double x = 4876.1874d;
 
   //syntax is static double abs(double a)
   // get and print their absolute values
   System.out.println("Math.abs(" + x + ")=" + Math.abs(x));//abs method returns the absolute value of the argument.
 
    // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc cosine of x
   System.out.println("Math.acos(" + x + ")=" + Math.acos(x));

 // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc sine of x
   System.out.println("Math.asin(" + x + ")=" + Math.asin(x));



   // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc tangent of x
   System.out.println("Math.atan(" + x + ")" + Math.atan(x));

// get two double numbers
   double x = 45.0;
   double y = 180.0;

   // convert them to radians
   x = Math.toRadians(x);
   y = Math.toRadians(y);

   // print their cosine
   System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   System.out.println("Math.cos(" + y + ")=" + Math.cos(y));

 // get two double numbers
   double x = 5;
   double y = 0.5;

   // print e raised at x and y
   System.out.println("Math.exp(" + x + ")=" + Math.exp(x));
   System.out.println("Math.exp(" + y + ")=" + Math.exp(y));

// get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // call floor and print the result
   System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
   System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
   System.out.println("Math.floor(0)=" + Math.floor(0));


   // get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // get the natural logarithm for x
   System.out.println("Math.log(" + x + ")=" + Math.log(x));

   // get the natural logarithm for y
   System.out.println("Math.log(" + y + ")=" + Math.log(y));


   // get two integer numbers
   int x = 60984;
   int y = 1000;

   // print the larger number between x and y
   System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));

 // get two random double numbers
   double x = Math.random();
   double y = Math.random();

   // print the numbers and print the higher one
   System.out.println("Random number 1:" + x);
   System.out.println("Random number 2:" + y);
   System.out.println("Highest number:" + Math.max(x, y));
   }
}



2 .program  

/*
 * Program to demonstrate the use of Math class methods
 */

import java.io.*;
public class Mathstest {


public static void main(String[] args)
{
int num1,num2,x,y,z;
double l1,l2,l3;
String str;
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter a number");
str=bf.readLine();
num1=Integer.parseInt(str);
x=Math.abs(num1);
System.out.println("Absolute value of "+num1+" is ="+x);

System.out.println("Enter first number");
str=bf.readLine();
num1=Integer.parseInt(str);
System.out.println("Enter second number");
str=bf.readLine();

num2=Integer.parseInt(str);
y=Math.max(num1, num2);
System.out.println("Largest number is ="+y);
y=Math.min(num1, num2);
System.out.println("Smallest number is ="+y);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.log(l1);
System.out.println("Log of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.sqrt(l1);
System.out.println("Square root of "+l1+" is ="+l2);
l3=Math.floor(l2);
System.out.println("Floor value of "+Math.sqrt(l1)+" is ="+l3);
l2=Math.ceil(l2);
System.out.println("Ceil value of "+Math.sqrt(l1)+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.cbrt(l1);
System.out.println("Cube root of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
System.out.println("Enter the power to raise ");
str=bf.readLine();

l2=Float.valueOf(str);
l3=Math.pow(l1, l2);
System.out.println(l1+"raised to the power "+l2+ " = "+l3);
}
catch(Exception e){}


}


}

console application in java : Student Management System In java Project (Mini project)

First way : student management system

package java_trainkj;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Scanner;

/**
 *
 * @author user
 */
public class Stud {

    private int rollno;
    String name;
    static String School_name;
    String address;
    String course;

    static {

        School_name = "Witshaper";
    }

    Stud() {

    }

    Stud Add(int l) {
        Stud o = new Stud();
        if (l > 1000) {
            System.out.println(" over flow");
        } else {
            try {System.out.println("--------------------------");
                Scanner s = new Scanner(System.in);
                System.out.println(" Enter name :");
                o.name = s.next();
                System.out.println(" Enter Address :");
                o.address = s.next();
                System.out.println(" Enter Course :");
                o.course = s.next();
                System.out.println(" Enter rollno. :");
                o.rollno = s.nextInt();

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

            }
        }
        return (o);
    }

    void search(int rollno, int l, Stud s[]) {
        for (int i = 0; i < l; i++) {
            if (s[i].rollno == rollno) {
                s[i].display(s[i]);
            }
        }
    }

    boolean delete(int rollno, Stud o[], int l) {
        boolean t = false;
        for (int i = 0; i < l; i++) {
            if (rollno == o[i].rollno) {
                for (int j = i; j < l - 1; j++) {
                    o[j] = o[j + 1];
                }
               
                t = true;
                return (t);
            }

        }

        return (t);
    }

    boolean update(int rollno, Stud obj[], int l) {
        boolean t = false;

        for (int i = 0; i < l; i++) {
            if (rollno == obj[i].rollno) {
                try {System.out.println("--------------------------");
                    Scanner s = new Scanner(System.in);
                    System.out.println(" Enter name :");
                    obj[i].name = s.next();
                    System.out.println(" Enter Address :");
                    obj[i].address = s.next();
                    System.out.println(" Enter Course :");
                    obj[i].course = s.next();

                    t = true;
                    return (t);
                } catch (Exception e) {
                    System.out.println(e);
                    e.getMessage();

                }
            }

        }

        return (t);

    }

    void display(Stud s) {
        System.out.println("--------------------------");
        System.out.println("  name :" + s.name);
        System.out.println(" Address :" + s.address);
        System.out.println(" course :" + s.course);
        System.out.println(" rollno. :" + s.rollno);

    }

    void show(int l,Stud s[]) {
        for(int i=0;i<l;i++){
         System.out.println("--------------------------");
        System.out.println(" Name       :" + s[i].name);
        System.out.println(" Address    :" + s[i].address);
        System.out.println(" course     :" + s[i].course);
        System.out.println(" rollno.    :" + s[i].rollno);
        }
    }

    public static void main(String args[]) {
        Stud obj[] = new Stud[1000];
        Scanner s = new Scanner(System.in);
        boolean f = true;
        int choice;
        int l = 0;
        int r;
        Stud o = new Stud();

        do {
            try {System.out.println("#### WELCOME ####");
                System.out.println(" Enter your choice ");
                System.out.println("1) ......Add");
                System.out.println("2).......Delete");
                System.out.println("3).......Search");
                System.out.println("4).......Update");
                 System.out.println("5).......Display");
                choice = s.nextInt();
                try {
                    switch (choice) {
                        case 1:
                            obj[l] = o.Add(l);

                            l++;
                            break;
                        case 2:
                            System.out.println(" Enter rollno.for deletion ");
                            r = s.nextInt();
                            boolean b = o.delete(r, obj, l);
                            if (b = true) {l--;
                                System.out.println("Successfully deleted");
                            } else {
                                System.out.println(" rollno. not found");
                            }
                            break;
                        case 3:
                            System.out.println(" Enter rollno. to search student ");
                            r = s.nextInt();
                            o.search(r, l, obj);
                            break;
                        case 4:
                            System.out.println(" Enter rollno. for update");
                            r = s.nextInt();
                            boolean c = o.update(r, obj, l);
                            if (c = true) {
                                System.out.println("Successfully updated");
                            } else {
                                System.out.println(" rollno. not found");
                            }
                            break;
                        case 5:
                            o.show(l,obj);
                        default:
                            System.out.println(" Enter right choice");

                    }
                } catch (Exception m) {
                    System.out.println(m);
                }

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

            System.out.println(" want to do more ... true/false");
            f = s.nextBoolean();

        } while (f);

    }

}



// second way : student management system 

package myproj.exceptionhandling;

import java.util.Scanner;

public class studentmanagement {

    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);

        int c, usr;
        store ob = new store();

        do {
            System.out.println("::::MENU::::");
            System.out.println("ENTER 1 FOR ADDING DETAIL");
            System.out.println("ENTER 2 FOR SEARCHING");
            System.out.println("ENTER 3 FOR DELETION");
            System.out.println("ENTER 4 FOR UPDATION");
            System.out.println("ENTER 5 TO EXIT");
            int ch = s.nextInt();

            switch (ch) {
                case 1:
                    System.out.println("enter the no of entries");
                    usr = s.nextInt();
                    ob.add(usr);
                    break;
                case 2:
                    ob.search();
                    break;
                case 3:
                    ob.delete();
                    break;
                case 4:
                    ob.update();
                    break;
                case 5:
                    System.exit(0);
            }

            System.out.println("press any key other than 5 to do more operations else press 5 to exit");
            c = s.nextInt();
        } while (c != 5);

    }
}

abstract class student {

    protected String name[];
    protected int roll[];

    abstract void display();
}

class store extends student {

   int usr;

    Scanner s = new Scanner(System.in);
    int i = 0, c = 0, r = 0;

    store() {
        roll = new int[10];
        name = new String[10];
    }

    public void add(int usr) {
        this.usr = usr;
        for (i = 0; i < usr; i++) {
            System.out.println("Enter name and roll no ");
            name[i] = s.next();
            roll[i] = s.nextInt();

        }
        display();

    }

    public void search() {

        System.out.println("Enter the roll no you want to search");
        r = s.nextInt();
        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                break;
            }
        }
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("---DETAILS OF THE SPECIFIED STUDENT IS---");
        System.out.println("NAME        ROLL NO");
        System.out.print(name[i]+"         ");
        System.out.print(roll[i]);
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");    
       

    }

    public void delete() {
        System.out.println("Enter the rollno to be deleted");
        r = s.nextInt();

        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                roll[i] = 0;
                name[i] = " ";
            }
        }
        display();

    }

    public void update() {
        System.out.println("Enter the rollno to be updated");
        r = s.nextInt();

        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                System.out.println("Enter the new name and roll no");
                name[i] = s.next();
                roll[i] = s.nextInt();
            }
        }
        display();
    }

    @Override
    void display() {
        System.out.println("----THE UPDATED RECORDS OF STUDENTS ARE----");
        System.out.println("NAME        ROLL NO");
        for (i = 0; i < roll.length; i++) {
            if(roll[i]!=0)
            {              
            System.out.print(name[i]+"         ");
            System.out.print(roll[i]);
            System.out.println(" ");
            }
        }
    }
}


Applet program to implement basic arithmetic operations( Add,multiply);

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Applet67 extends Applet implements Action Listener
{
Label l1,l2,l3;
Text Field t1,t2,t3,;
Button  b1,b2,b3,b4,b5;
public void init()
{
l1=new Label ("number 1");
l2=new Label("number 2");
l3=new Label("Result");
b1=new button ("Add");
b2=new button("multiply");
b3=new Button("Clear");
b4=new Button("Red");
b5=new Button("blue");
t1=new TextField("15");
t2=new TextField("15");
t3=new TextFieldss("15");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
ad(b2);
add(b3);
add(l3);
add(t3);
add(b4);
add(b5);
b1.addActionListener(this);
b2.addActionlistener(this);
b3.addActionlistener(this);
b4.addActionListener(this);
b5.addActionListener(this);
SetBackground(Color.green);
}
public void actionPerformed(Action Event ae)
{
int n1,n2,n3;
if(ae.getSource()=b1)
{
n1=Integer.parseInt(t1.getText());
n2=Integer.parseInt(t2.getText());
n3=n1+n2
t3.SetText(String.value Of(n3));
}
if(ae.getSource()=b2)
{
n1=Integer.parseInt(t1.getText());
n2=Integer.parseInt(t2.getText());
n3=n1*n2;
t3.SetText(String value of(n3));
}
if(ae.getsource()==b3)
{
t1.SetText("");
t2.SetText("");
t3.SetText("");
}
if(ae.getSource()==b4)
{
SetBackground(Color.red);
}
if(ae.getSource()==b5)
{
SetBackground(Color.Blue);
}
}
}
/*<applet code=Applet67
 height=20 width=220>
</applet>
*/









































applet program: Implementing Card layout and panel

import java.awt.*;
import java.awt.event.*;
public class Card1 extends Frame implements ActionListener
{
CardLayout cl;
Panel p1,p2,p3,p4,p5;
Button b1,b2,b3,b4,b5,b6;
Card1()
{
b1=new Button("Next");
b2=new Button("Previous");
b3=new Button("Next");
b4=new Button("Previous");
b5=new Button("Next");
b6=new Button("End");

p1=new Panel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.yellow);
p1.add(b1);

p2=new Panel();
p2.setLayout(new FlowLayout());
p2.setBackground(Color.pink);
p2.add(b2);
p2.add(b3);

p3=new Panel();
p3.setLayout(new FlowLayout());
p3.setBackground(Color.green);
p3.add(b4);
p3.add(b5);

p4=new Panel();
p4.setLayout(new FlowLayout());
p4.setBackground(Color.cyan);
p4.add(b6);

p5=new Panel();
cl=new CardLayout();
p5.setLayout(cl);
p5.add(p1,"First");
p5.add(p2,"Second");
p5.add(p3,"Third");
p5.add(p4,"Fourth");

add(p5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
cl.next(p5);
if(ae.getSource()==b2)
cl.previous(p5);
if(ae.getSource()==b3)
cl.next(p5);
if(ae.getSource()==b4)
cl.previous(p5);
if(ae.getSource()==b5)
cl.next(p5);
if(ae.getSource()==b6)
cl.first(p5);

}
public static void main(String ar[])
{
Card1 c1=new Card1();
c1.setSize(400,200);
c1.setVisible(true);
}
}

Applet program : implement KeyListener and ActionListener

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyLis extends Applet implements KeyListener,ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1;
public void init()
{
l1=new Label("Name  ");
l2=new Label("Age     ");
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Clear");
setBackground(Color.cyan);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
t1.addKeyListener(this);
t2.addKeyListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
t1.setText("");
t2.setText("");
}
}
public void keyPressed(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{
int ch;
if(ke.getSource()==t1)
{
ch=ke.getKeyChar();
//System.out.println(ch);
if(( ch < 65 || ch >90) &&(ch<97 ||ch >122 )&&(ch!=32))
{
t1.setText("");
showStatus("Only alphabets and spaces allowed ... ");
}
else
{
showStatus("");
}
}
if(ke.getSource()==t2)
{
ch=ke.getKeyChar();
if(ch<48 || ch >57)
{
t2.setText("");
showStatus("Only numbers allowed .. ");
}
else
{
showStatus("");
}
}
}
public void keyTyped(KeyEvent ke)
{}
}
/*
<applet code=KeyLis.java height=200 width=200></applet>
*/

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);
}
}
}

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);
}
}
}