Tuesday, January 17, 2017

Program to generate otp with char and numeric.

import java.util.*;
class Otp3
{
public static void main(String...args)
{
String str="";
int Snum,num,count=0;
Random rn=new Random();

      for(;;)
{
Snum=rn.nextInt(100);
if(Snum>=65 && Snum<=90)
{
str=str+(char)Snum;
count++;
}
if(count==4)
{
break;
}
}
for(;;)
{
num=rn.nextInt(10000);
if(num>1000)
{
break;
}
}
str=str+num;
System.out.println("Your Otp is : "+str);
}
}

                                                                                                                                       

Output:


Monday, January 16, 2017

Program to display time in 12 and 24 hours formats.

import java.util.*;
class Watch
{
int second,minute,hour;
void store()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the time in 24 format: ");
System.out.print("Hours : ");
do
{
hour=sc.nextInt();
if(hour > 23 || hour < 0)
{
System.out.println("Oop! Hours must be between 0 to 23");
System.out.print("Hours : ");
}
}while(hour > 23 || hour < 0);
System.out.print("Minutes : ");
do
{
minute=sc.nextInt();
if(minute > 59 || minute < 0)
{
System.out.println("Oop! Minutes must be between 0 to 59");
System.out.print("Minutes : ");
}
}while(minute > 59 || minute < 0);
System.out.print("Seconds : ");
do
{
second=sc.nextInt();
if(second >59 || second < 0)
{
System.out.println("Oop!Seconds must be between 0 to 59");
System.out.print("Seconds : ");
}
}while(second > 59 || second < 0);
}
void show_time12F()
{
if(hour > 12)
{
System.out.println(" 12 Format Time -> "+(hour-12)+":"+minute+":"+second+" PM");
}
else if (hour == 12)
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" PM");
}
else
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" AM");
}
}
void show_time24F()
{
System.out.println(" 24 Format Time -> "+hour+":"+minute+":"+second);
if(hour==0)
{
System.out.println("It's Mid Night!!!!!!\n");
}
else if(hour>1 && hour<11)
{
System.out.println("It's Morning!!!!!!\n");
}
else if(hour>11 && hour<12)
{
System.out.println("It's Morning!!!!!!\n");
}
else
{
System.out.println("It's Evening!!!!!!\n");
}
}
public static void main(String...args)
{
Watch w1=new Watch();
w1.store();
w1.show_time12F();
w1.show_time24F();
}
}

                                                                                                                          

Output:


Program to display student report card.

import java.util.*;
class Student
{
int rollno,physics,chem,math;
String name,email;
public static void main(String...args)
{
Student s1=new Student();
Scanner sc=new Scanner(System.in);
System.out.print("Roll no. : ");
s1.rollno=sc.nextInt();
System.out.print("Name : ");
s1.name=sc.next();
System.out.print("Email : ");
s1.email=sc.next();
System.out.println("Enter the Marks following...");
System.out.print("Physics : ");
s1.physics=sc.nextInt();
System.out.print("Chemistry : ");
s1.chem=sc.nextInt();
System.out.print("Math : ");
s1.math=sc.nextInt();

int total=s1.physics+s1.chem+s1.math;

int avg=(total*100)/300;

String grade;
if(avg<33)
{
grade="Fail";
}
else if ( avg >=34 && avg <= 44 )
{
grade="III";
}
else if ( avg >= 45 && avg <= 59 )
{
grade="II";
}
else if ( avg >=60 && avg <= 74 )
{
grade="I";
}
else
{
grade="Distinction";
}

//Report card..
System.out.println("\n***Report Card***");
System.out.println("Roll no. : "+s1.rollno);
System.out.println("Name : "+s1.name);
System.out.println("Email : "+s1.email);
System.out.println("Physics : "+s1.physics);
System.out.println("Chemistry: "+s1.chem);
System.out.println("Math : "+s1.math);
System.out.println("Total : "+total);
System.out.println("Average : "+avg);
System.out.println("Grade : "+grade);


}
}

                                                                                                                               

Output:


Friday, January 13, 2017

Program to display clock in console window

import java.util.*;
class SystemTime
{

public static void main(String...args) throws Exception
{
System.out.println("type ctrl and c to quit");
for(;;)
{
//Create the object.
Date d=new Date();
//Display the System time and date.
System.out.print(d);
// hold the next time.
Thread.sleep(500);
//To overide the previous time and move cursor to starting point.
System.out.print("\r");
Thread.sleep(500);
}
}
}

                                                                                                                                  

Output:


Program to generate floating point random number.

import java.util.*;

public class RandomDemo2 {
   public static void main( String...args )
   {
// create random object
Random randomno = new Random();

//Float value
System.out.println("Float value between 0.0 and 1.0: "+randomno.nextFloat());

//Double value
System.out.println("Double value between 0.0 and 1.0: "+randomno.nextDouble());

// check next Gaussian value
System.out.println("Next Gaussian value: " + randomno.nextGaussian());
   }    
}

                                                                                                                                          

Output:


Thursday, January 12, 2017

Program to generate 6 digits Random Number(Positive Only).

import java.util.*;
class RandomNum
{
public static void main(String...args)
{
//Random object created.
Random rn=new Random();

//Random method called for integer values with fixed value.
int num=rn.nextInt(1000000);

//show Random with 6 digits number.
if(num>100000)
{
System.out.println("Your Random No. is : "+num);
}
else
{
System.out.println("Retry!");
}

}
}

                                                                                                                                   

Output:


Program to generate random integer number(Positive or Negative).

import java.util.*;
class RandomDemo1
{
public static void main(String...args)
{
//Random object created.
Random rn=new Random();

//Random method called for integer values.
int num=rn.nextInt();

//show the Random number.
System.out.println("Random number is : "+num);
}
}
                                                                                                                                                 
Output:


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: