Search This Blog

save java programs with".java" extension

examples :

helloworld.java
sample.java
match.java
send-mail.java

quick links

Abstract Demo

import java.util.Scanner;
/*1*/abstract class vehicle5
{
private int wheel;
vehicle5()
{
System.out.println("Some vehicles instanciated");
wheel=4;
}
void start()
{
System.out.println("Every vehicle starts like this");
}
abstract void move();
}
class car6 extends vehicle5
{
void move()
{
System.out.println("Exellent transportation");
}
}
class zeep extends vehicle5
{
void move()
{
System.out.println("Good transportation");
}
}
class AbstractExample {

public static void main(String[] args) {
vehicle5 v;
Scanner s = new Scanner(System.in);
System.out.println("What vehicle you want car/zeep");
String choice=s.next();
if(choice.equals("car"))
v=new car6();
/*if(choice.equals("zeep"))
v=new zeep();*/ //error
else
v=new zeep();
v.start();
v.move();
}
}

Thread Demo

// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

runnable example

class mythreadone implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
System.out.println(i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
}
}
}

class mythreadtwo implements Runnable
{
public void run()
{
for(int j=11;j<=20;j++)
System.out.println(j);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
}
}

}
class runnableexample {

public static void main(String[] args) {
Thread rt1=new Thread(new mythreadone());
Thread rt2=new Thread(new mythreadtwo());
rt1.start();
rt2.start();

}

}

priority example


class priorityexample {

public static void main(String[] args) {
Thread t1=Thread.currentThread();
System.out.println("this thread :"+t1.getName());
System.out.println("t1 priority :"+t1.getPriority());
Thread t2=new Thread();
System.out.println("t2 priority :"+t2.getPriority());
t1.setPriority(8);
Thread t3=new Thread();
System.out.println("t3 priority :"+t3.getPriority());

}

}

Multithreading banking application


class account
{
int accno;
float balance;
account(int accno,float balance)
{
this.accno=accno;
this.balance=balance;
}
void setbalance(float balance)
{
this.balance=balance;
}
float getbalance()
{
return this.balance;
}
void withdraw(float amount)
{
System.out.println("trying to withdraw (Rs) : "+amount+" from A/c : "+accno);
System.out.println("trying to get the balance info from A/c : "+accno);
//for(int i=1;i<20;i++)
// System.out.println(i);
float bal=getbalance();
System.out.println("balance in the A/c : "+accno+" is Rs "+bal);
if(bal>amount)
{
bal=bal-amount;
setbalance(bal);
System.out.println("please collect tha cash "+amount);
System.out.println("balance A/c "+accno+" update to Rs "+bal);
}
else
{
System.out.println("insufficient funds");
}

}
}
class accountthread extends Thread
{
account acc;
float amt;
accountthread(account acc,float amt)
{
this.acc=acc;
this.amt=amt;
}
public void run()
{
acc.withdraw(amt);
}
}

class multithreadbankingapplication {


public static void main(String[] args) throws Exception {
account a1=new account(1001,8000);
account a2=new account(1002,9000);
accountthread t1= new accountthread(a1,3000);
accountthread t2=new accountthread(a2,5000);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("final balance in the 1st account Rs"+a1.getbalance());
System.out.println("final balance in the 2nd account Rs"+a2.getbalance());

}

}

Multithread Demo

// Create multiple threads.
class NewThread1 implements Runnable {
String name; // name of thread
Thread t;
NewThread1(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread1("One"); // start threads
new NewThread1("Two");
new NewThread1("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}

Strings

StrTok Demo

import java.util.*;

class StrTokDemo {

public static void main(String[] args) {

StringTokenizer st = new StringTokenizer("Hello, World|of|Java");

// (Hello, World|of|Java); here give somewhere space and run.

while (st.hasMoreTokens())
   System.out.println("Token: " + st.nextToken());
}

}

BuferredReader

//display entered strings until you enter 'q'

import java.io.*;
class BuferredReaderdemo {
public static void main(String[] args) {
char c='o';
System.out.println("type any string :");
BufferedReader br  =  new BufferedReader(new InputStreamReader(System.in));

do{
try
{

c=(char)br.read();
}
catch(IOException e)
{
System.out.print(e);
}
System.out.println(c);
}while(c!='q' );
}
}

vararg test


class varargtest {

static int add (int ...a)
{
int sum=0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
return sum;
}
public static void main(String[] args) {
System.out.println("the sum of 2 numbers : "+add(10,70));
System.out.println("the sum of 3 numbers : "+add(110,60,40));
}

}

stringtokenizer

import java.util.StringTokenizer;

class stringtokenizertest {

public static void main(String[] args) {
String range = "123456789";
StringTokenizer str= new StringTokenizer (range, "5");
while (str.hasMoreTokens())
System.out.println(str.nextToken());
}

}

string append


class stringtest {

public static void main(String[] args) {

String s= new String("hello "); // immutable string created.
String str= s.concat("how are you");
System.out.println(s);
System.out.println(str);

StringBuffer sb= new StringBuffer("hello"); //mutable string created.
sb.append("how are you");
System.out.println(sb);

StringBuilder sbuilder= new StringBuilder("hello"); //mutable string created
sbuilder.append("how are you");
System.out.println(sbuilder);
}

}

static import test

import static java.lang.Integer.*;
import static java.lang.System.*;

class staticimporttest {

public static void main(String[] args) {
int a = parseInt("11");
out.println(a);
}

}

Scanner demo

import java.util.Scanner;
class Scanner1
{

public static void main(String args[])
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter the word : ");
String str1 = s1.next();
System.out.println("string 1 : "+str1);

Scanner s2 = new Scanner(System.in);
System.out.print("Enter the word : ");
String str2 = s2.next();
System.out.println("string 2 : "+str2);

if(str1.equals(str2))
System.out.println("it is pollindrome");
else
System.out.println("it is not");
}
}

nested class

class A
{
static class B
{

}
class C
{

}
}
class nestedclass
{
public static void main(String args[])
{
  A a = new A();//outer class instance created.
  A.B b = new A.B();//nested class instance.
  A.C c = new C();//inner class instance created.
}
}

equality

class A1
{
int a;
int b;
A1 (int a, int b)
{
this.a=a;
this.b=b;
}
boolean equals(A1 r)
{
boolean flag = false;
if(this.a == r.a && this.b == r.b)
flag = true;
return flag;
}// overriding equals method to make the objects.equals if their state is the same.
}
class equality {

public static void main(String[] args) {

A1 a1 = new A1(10,2);//change value here to check
A1 a2= new A1(10,2);//change value here to check

System.out.println("object1 : "+a1.hashCode());
System.out.println("object2 : "+a2.hashCode());
if(a1.equals(a2))
System.out.println("both object are equal");
else
System.out.println("not equal");
}

}

interface


interface I1
{
int marks=100;

}
interface I2
{
int cla=12;
}
class interface1 implements I1,I2
{
public static void main(String args[])
{
int c,b;
c=I1.marks+10;
b=I2.cla;
System.out.println("marks :"+c);
System.out.println("class :"+b);


}
}

output :
marks  :110
class    :12

while loop in java

class whileloop
{
public static void main(String args[])
{
int i=1;
while(i<=5)
{
System.out.println("welcone to world "+i);
i++;//increament
}

}
}

//first check the condition and next execute the loop

sum of dights with java

class sumofdigits
{
public static void main(String args[])
{
int n=222;
int sum=0,rem;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
System.out.println("\nsum of digits :"+sum);
System.out.println("\n");

}
}

for loop in java

class forloop
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
System.out.println("welcone to world "+i);
}

}