Monday, July 4, 2016

Guess a number using Thread.

import java.util.*;
class GuessANumber extends Thread
{
   private int number;
   public GuessANumber(int number)
   {
      this.number = number;
   }
   public void run()
   {
      int counter = 0;
      int guess = 0;
      do
      {
 guess = (int) (Math.random() * 100 + 1);
         System.out.println(this.getName()+ " guesses " + guess);
     counter++;
  }while(guess != number);
  System.out.println("** Correct! " + this.getName()+ " in " + counter + " guesses.**");
  }
}

public class ThreadGuess
{
   public static void main(String [] args)
   {
  Scanner s=new Scanner(System.in);
      System.out.println("Enter the number:");
      int i=s.nextInt();
       GuessANumber T1 = new GuessANumber(i);
       T1.start();
   }
}

Output:

Tuesday, April 19, 2016

Create Thread by Extending Thread Class:

class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;

   ThreadDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }

   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }

}

public class TestThread2 {
   public static void main(String args[]) {

      ThreadDemo T1 = new ThreadDemo( "Thread-1");
      T1.start();

      ThreadDemo T2 = new ThreadDemo( "Thread-2");
      T2.start();
   }
}


Create Thread by Implementing Runnable Interface:


   class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;

   ThreadDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }

   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }

}

public class TestThread1 {
   public static void main(String args[]) {

      ThreadDemo T1 = new ThreadDemo( "Thread-1");
      T1.start();

      ThreadDemo T2 = new ThreadDemo( "Thread-2");
      T2.start();
   }
}

Wednesday, February 17, 2016

Program to add Scrolling List in a Frame.

import java.awt.*;
class ListAdd extends Frame
{
public static void main(String args[ ] )
{
List lt= new List( 5 , true);
lt.add ( "Window 98" );
lt.add ( "Window 2000" );
lt.add ( "Window XP" );
lt.add ( "Window NT" );
ListAdd f1=new ListAdd();
f1.setTitle("Adding a List in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(lt);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:

Tuesday, February 16, 2016

Program to add choice menu in Frame.

import java.awt.*;
class Choice_Test extends Frame
{
public static void main(String arg[])
{
Choice ch = new Choice ( );
ch.add ( "Apples" );
ch.add ( "Oranges" );
ch.add ( "Strawberries" );
ch.add ( "Bananas");
Choice_Test f1=new Choice_Test();
f1.setTitle("Adding a Choice menu in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(ch);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:


Monday, February 15, 2016

Program to add RadioButton in Frame.

import java.awt.*;

class RadioButton extends Frame
{
public static void main(String args[ ] )
{
CheckboxGroup og=new CheckboxGroup();
Checkbox male=new Checkbox("Male",og,true);
Checkbox female=new Checkbox("Female",og,false);
RadioButton f1 =new RadioButton();
f1.setTitle("Adding a RadioButton in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(male);
f1.add(female);
f1.setSize(500,300);
f1.setVisible(true);

}
}
Output:

Saturday, February 13, 2016

Program to add checkbox in Frame.

import java.awt.*;
class chkBox extends Frame
{
public static void main(String args[ ] )
{
Checkbox c1 =new Checkbox("Reading");
Checkbox c2=new Checkbox("Singing");
Checkbox c3=new Checkbox("Dancing");
c1.setState(true);
chkBox f1=new chkBox();
f1.setTitle("Adding a Checkbox in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(c1);
f1.add(c2);
f1.add(c3);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:

Friday, February 12, 2016

Program to add buttons in Frame.

import java.awt.*;
class btnAdd extends Frame
{
public static void main(String args[ ] )
{
Button b1=new Button("Add");
Button b2=new Button("Subtract");
Button b3=new Button("Mu|tip|y");
Button b4=new Button("Division");
btnAdd f1=new btnAdd();
f1 .setTitle("Adding Buttons in a Frame!!!");
f1 .setLayout(new FlowLayout());
f1 .add(b1 );
f1 .add(b2);
f1 .add(b3);
f1 .add(b4);
f1 .setSize(500,500);
f1 .setVisible(true );
}
}
Output:

Thursday, February 11, 2016

Program to use the various methods of TextArea.

import java.awt.*;
class txtAreaMethods extends Frame
{
public static void main(String args[] )
{
TextArea ta1 =new TextArea(5,30);
ta1.setText("The Quick Brown Fox Jumps Over The Lazy Dog");
ta1.insertText("-Java is Intenesting-",5);
ta1.replaceText("-Text Replaced-",38,50);
txtAreaMethods f1=new txtAreaMethods();
f1.setTitle("Adding a TextArea in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(ta1 );
f1.setSize(500,500);
f1.setVisible(true );
}
}
Output:

Program to add TextArea in Frame.

import java.awt.*;
class txtArea extends Frame
{
public static void main(String args[ ])
{
TextArea ta1 =new TextArea("Area to write Text",5,30);
txtArea f1 =new txtArea();
f1.setTitle("Adding a TextArea in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(ta1 );
f1.setSize(500,500);
f1.setVisible(true );
}
}
Output:

Tuesday, February 9, 2016

Program to use various methods of TextField.

import java.awt.*;
class txtFieldMethods extends Frame
{
public static void main(String args[])
{
TextField t1 =new TextField(50);
TextField t2=new TextField(50);
TextField t3=new TextField(50);
TextField t4=new TextField(50);
//To make a TextField Read Only
t1.setEditable(false);
//setting the initial Text of a TextField
t2.setText("Java is Interesting");
//Getting the text of a TextField and stone into a String Object
String str=t2.getText();
//setting the Copied text into nother TextField
t3.setText("Copied Suing is : "+str);
//setting llsking chlacter of a TextField
t4.setEchoCharacter('*');
txtFieldMethods f1=new txtFieldMethods();
f1.setTitle("Adding a TextField in a FIame!!!");
f1.setLayout(new FlowLayout());
f1.add(t1);
  f1.add(t2);
f1.add(t3);
f1.add(t4);
f1.setSize(500,500);
f1.setVisible(true );
}
}
Output:

Monday, February 8, 2016

Program to add a TextField in a Frame.

import java.awt.*;
class txtField extends Frame
{
public static void main(String args[])
{
TextField t1 =new TextField("Enter a value here!",50);
txtField f1 =new txtField();
f1.setTitle("Adding a TextField in a Frame!!!");
f1.add(t1);
f1.setSize(300,200);
f1.setVisible(true);
}
}
Output:

Sunday, February 7, 2016

Program to change backcolor and textcolor of label using color class.

import java.awt.*;
class Demo4_ColorLabel extends Frame
{
public static void main(String args[ ] )
{
Color c1 =new Color(200, 100,50);
Color c2=new Color(100,150,250);
Label l1 =new Label("Java is lnteresting!!!");
//setting text color of label
l1.setForeground(c1 );
//setting background color of a label
l1.setBackground(c2);
Demo4_ColorLabel f1 =new Demo4_ColorLabel();
f1.setTitle("Working with colors of a Label");
f1.add(l1);
f1.resize(400,200);
f1.show();
}
}
Output:

Program to use setText() and getText() methods of Label.

import java.awt.*;
class Demo3_Label extends Frame
{
public static void main(String args[ ] )
{
Label l1=new Label();
//Setting text of a label
l1.setText("Java is lnteresting!!!");
//Getting text of a label and set it to another label
String lbltext=l1.getText();
Label l2=new Label();
l2.setText(lbltext);
Demo3_Label f1=new Demo3_Label();
f1.setTitle("Adding a Label in a Frame!!!");
f1.add(l2);
f1.resize(300,200);
f1.show( );
}
}
Output:

Thursday, February 4, 2016

Program to set alignment of label in frame.

import java.awt.*;
class Demo_Label extends Frame
{
public static void main(String args[ ])
{
Label l1 =new Label(" Hello Java!!! ", Label.CENTER);
Demo_Label f1 =new Demo_Label();
f1.setTitle("Adding a Label in a Frame!!!");
f1.add(l1);
f1.resize(300,200);
f1.show( );
}
}
Output:

Wednesday, January 27, 2016

Program to create lalbel in frame.

import java.awt.*;
class Demo1_Label extends Frame
{
public static void main(String args[])
{
Label l1 =new Label(" Hello Java!!! ");
Demo1_Label f1 =new Demo1_Label();
f1.setTitle("Adding a Label in a Frame!!!");
f1.add(l1);
f1.resize(300,200);
f1.show( );
}
}
Output:

Friday, January 22, 2016

Program to create frame.

import java.awt.*;
class FrameDemo extends Frame
{
public FrameDemo(String title)
{
super(title); //calling constructor of base class Frame
    }
public static void main(String arg[])
{
FrameDemo f=new FrameDemo("I have been Framed!");
f.setSize(500,500);
f.setVisible(true);
}
}
Output: