class MyExp extends Exception
{
public MyExp(String msg)
{
System.out.println(msg);
}
}
public class throws1 {
static int mtd(int a,int b) throws MyExp
{
if(b==0) throw new MyExp("Example for throw b") ;
else
if(a==0) throw new MyExp("Example for throw a") ;
return a/b;
}
public static void main(String args[])
{
try
{
System.out.print(mtd(10,0));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts
Wednesday, March 11, 2009
Tuesday, March 3, 2009
Constructor Example
public class constructor_ex {
public constructor_ex()
{
System.out.print("Constructor example");
}
public static void main(String args[])
{
constructor_ex obj=new constructor_ex();
}
}
/*constructor is a method in a class ,that has same name as that of a class.
* constructor don't have return type ,not even void.
* They automatically intialize objects with default values , when they are created.
* They are invoked using new operator .
* They can't be inherited .
* "This" and "super" can be used .
*/
public constructor_ex()
{
System.out.print("Constructor example");
}
public static void main(String args[])
{
constructor_ex obj=new constructor_ex();
}
}
/*constructor is a method in a class ,that has same name as that of a class.
* constructor don't have return type ,not even void.
* They automatically intialize objects with default values , when they are created.
* They are invoked using new operator .
* They can't be inherited .
* "This" and "super" can be used .
*/
Monday, February 23, 2009
Creating a thread by implementing "Runnable" Interface
// Save as "thread_runnable.java"
class t1 implements Runnable
{
public void run() {
for(int i=0;i<=10;i++){
System.out.println("Values -->"+i);
}
}
}
public class thread_runnable {
public static void main(String args[])
{
t1 obj=new t1();
Thread t=new Thread(obj);
t.start();
}
}
class t1 implements Runnable
{
public void run() {
for(int i=0;i<=10;i++){
System.out.println("Values -->"+i);
}
}
}
public class thread_runnable {
public static void main(String args[])
{
t1 obj=new t1();
Thread t=new Thread(obj);
t.start();
}
}
Friday, February 13, 2009
Thread Example
//Create New Thread Example - thread1.java
class ta extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.print(i);
}
}
}
public class thread1 {
public static void main(String args[])
{
ta obj=new ta();
obj.start();
}
}
class ta extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.print(i);
}
}
}
public class thread1 {
public static void main(String args[])
{
ta obj=new ta();
obj.start();
}
}
Static Block Example
//Static Block Example - save as "static_block.java"
public class static_block {
private static int static_var;
static
{
System.out.println(static_var);
System.exit(1);
}
}
public class static_block {
private static int static_var;
static
{
System.out.println(static_var);
System.exit(1);
}
}
Abstract Class Example
//Abstract Class Example - save prg as "abstract1 .java"
abstract class z {
abstract void mtd();
public void concrete_mtd()
{
System.out.print("Concrete Method");
}
}
abstract class z {
abstract void mtd();
public void concrete_mtd()
{
System.out.print("Concrete Method");
}
}
class x extends z
{
public void mtd()
{
System.out.print("Abstract Method Implementation");
}
}
public class abstract1
{
public static void main(String args[])
{
x obj=new x();
obj.concrete_mtd();
}
}
Method Overriding
//Method Overriding - save prog as "overrid.java" //
//To call super class methods remove the comment at super.mtd() in the prg
import java.io.*;
class oa
{
public void mtd()
{
System.out.print("super class");
}
}
class ob extends oa
{
public void mtd()
{
//super.mtd();
System.out.print("sub class");
}
}
public class overrid {
public static void main(String args[])
{
ob obj=new ob();
obj.mtd();
}
}
//To call super class methods remove the comment at super.mtd() in the prg
import java.io.*;
class oa
{
public void mtd()
{
System.out.print("super class");
}
}
class ob extends oa
{
public void mtd()
{
//super.mtd();
System.out.print("sub class");
}
}
public class overrid {
public static void main(String args[])
{
ob obj=new ob();
obj.mtd();
}
}
Multiple main methods in One Application
//Multiple main methods in One Application - Save this program as "multiplemain.java"
class ma
{
public static void main(String args[])
{
System.out.print("ma");
}
}
class mb
{
public static void main(String args[])
{
System.out.print("mb");
}
}
public class multiplemain {
public static void main(String args[])
{
System.out.print("main");
}
}
class ma
{
public static void main(String args[])
{
System.out.print("ma");
}
}
class mb
{
public static void main(String args[])
{
System.out.print("mb");
}
}
public class multiplemain {
public static void main(String args[])
{
System.out.print("main");
}
}
Generate Random Numbers
//Generate Random Numbers - Save this programe as "testrandom.java"
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
public class testrandom {
public static void main(String args[])
{
HashMap hm = new HashMap();
ArrayList al= new ArrayList();
int arr[]=new int[10];
for(int i= 0 ; i< arr.length;i++)
{
arr[i]=i;
hm.put(new Random().nextInt(), i);
}
Collection coll = hm.values();
Iterator it= coll.iterator();
while(it.hasNext())
{
System.out.println("Random Values - >"+it.next());
}
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
public class testrandom {
public static void main(String args[])
{
HashMap hm = new HashMap();
ArrayList al= new ArrayList();
int arr[]=new int[10];
for(int i= 0 ; i< arr.length;i++)
{
arr[i]=i;
hm.put(new Random().nextInt(), i);
}
Collection coll = hm.values();
Iterator it= coll.iterator();
while(it.hasNext())
{
System.out.println("Random Values - >"+it.next());
}
}
}
palindrome Example
//palindrome Example - Save this program as "palindrome.java"
class pa
{
public boolean mtd(String str)
{
int left=0 ;
int right = str.length()-1;
while ( left < right)
{
if(str.charAt(left) != str.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}
}
public class palindrome {
public static void main(String args[])
{
pa obj = new pa();
System.out.print("-->"+obj.mtd("madam"));
}
}
Reverse Of a Number Using Stack
//Reverse Of a Number Using Stack
import java.util.Stack;
public class StactDemo {
public static void main(String args[])
{
Stack lifo=new Stack();
for(int i=0 ; i < 10; i++)
{
System.out.println("push operation -->"+lifo.push(i));
}
while( !lifo.empty())
{
System.out.println("pop operation -->"+lifo.pop());
}
}
}
import java.util.Stack;
public class StactDemo {
public static void main(String args[])
{
Stack lifo=new Stack();
for(int i=0 ; i < 10; i++)
{
System.out.println("push operation -->"+lifo.push(i));
}
while( !lifo.empty())
{
System.out.println("pop operation -->"+lifo.pop());
}
}
}
Reverse Of a Number
/*Reverse Of a Number*/
public class reverse_no {
public static void main(String args[])
{
int arr[]=new int[10];
int no=123456789;
for(int i=0;i<10;i++)
{
if(no != 0)
{
arr[i]=no%10;
no=no/10;
System.out.print(arr[i]);
}
}
}
}
public class reverse_no {
public static void main(String args[])
{
int arr[]=new int[10];
int no=123456789;
for(int i=0;i<10;i++)
{
if(no != 0)
{
arr[i]=no%10;
no=no/10;
System.out.print(arr[i]);
}
}
}
}
Reverse Of a String
/*Reverse Of a String */
class reverse
{
public void reverse123(String str)
{
String str1="";
for(int i=str.length()-1;i>=0;i--)
{
str1=str1+str.charAt(i);
}
System.out.print(str1);
}
}
public class reverse_string {
public static void main(String args[])
{
reverse obj=new reverse();
obj.reverse123("Bangalore");
}
}
class reverse
{
public void reverse123(String str)
{
String str1="";
for(int i=str.length()-1;i>=0;i--)
{
str1=str1+str.charAt(i);
}
System.out.print(str1);
}
}
public class reverse_string {
public static void main(String args[])
{
reverse obj=new reverse();
obj.reverse123("Bangalore");
}
}
Subscribe to:
Posts (Atom)
