Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

Java Programs

The document contains 7 code examples demonstrating different Java programming concepts: 1. A program that calculates the area and circumference of a circle by taking user input for radius. 2. A program that generates random numbers and sorts them in both integer and double arrays. 3. A program that counts the vowels, consonants, commas, periods and spaces in a user-input string. 4. A program demonstrating multi-threading by creating a new thread and printing alternating outputs between the main and child threads. 5. A program handling arithmetic exceptions from division by zero. 6. A program handling different exceptions like array index out of bounds, wrong data type etc. 7. A program implementing Fibonacci series

Uploaded by

NishaPauline
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Java Programs

The document contains 7 code examples demonstrating different Java programming concepts: 1. A program that calculates the area and circumference of a circle by taking user input for radius. 2. A program that generates random numbers and sorts them in both integer and double arrays. 3. A program that counts the vowels, consonants, commas, periods and spaces in a user-input string. 4. A program demonstrating multi-threading by creating a new thread and printing alternating outputs between the main and child threads. 5. A program handling arithmetic exceptions from division by zero. 6. A program handling different exceptions like array index out of bounds, wrong data type etc. 7. A program implementing Fibonacci series

Uploaded by

NishaPauline
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

AREA AND CIRCUMFERENCE OF THE CIRCLE:

import java.io.*;
class yuva
{
public static BufferedReader obj=new BufferedReader(new InputStreamReader (System.in));
public static void main(String[] args)throws IOException
{
float r,area,cir;
System.out.println("***************");
System.out.print("ENTER THE RADIUS:");
r=Float.parseFloat(obj.readLine());
cir=r*3.14f*2;
area=3.14f*r*r;
System.out.println();
System.out.println("\t\tAREA AND CIRCUMFERENCE OF THE CIRCLE:-");
System.out.println("\t\t************************************");
System.out.println();
System.out.println("RADIUS OF THE CIRCLE:"+r);
System.out.println();
System.out.println("AREA OF CIRClE:"+area);
System.out.println();
System.out.println("CIRCUM OF THE CIRCLE="+cir);
System.out.println();
}
}

2.SORTED ARRAY:

import java.util.Random;
import java.io.*;
class rd
{
void sort(int a[])
{
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.println("sorted Random number");
for(int i=0;i<5;i++)
{
System.out.println("\t\t" +a[i]);
}
}
void sort(double a[])
{
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
if(a[i]<a[j])
{
double temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.println("sorted Random array");
for(int i=0;i<5;i++)
{
System.out.println("\n\t\t" +a[i]);
}
}
void intgen()
{
Random r=new Random();
int b[]=new int[5];
System.out.println("\n\t\t The given random number");
for(int i=0;i<5;i++)
{
b[i]=r.nextInt();
System.out.println("\n\t\t" +b[i]);
}
sort(b);
}
void dbgen()
{
Random r=new Random();
double b[]=new double[5];
System.out.println("\n\t\t The given random numbers:");
for(int i=0;i<5;i++)
{
b[i]=r.nextDouble();
System.out.println("\n\t\t" +b[i]);
}
sort(b);
}
void gauss()
{
Random r=new Random();
double b[]=new double[5];
System.out.println("\n\t\t The given random numbers:");
for(int i=0;i<5;i++)
{
b[i]=r.nextGaussian();
System.out.println("\n\t\t" +b[i]);
}
sort(b);
}
}
class rdgen
{
public static void main(String args[])throws IOException
{
int ch;
rd r1=new rd();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\t\t sorting Random numbers");
System.out.println("\t\t **********************");
System.out.println("\t\t 1.Integer");
System.out.println("\t\t 2.Double");
System.out.println("\t\t 3.Gaussian");
System.out.println("\t\t Enter your choice:");
ch=Integer.parseInt(in.readLine());
System.out.println("\t\t The Entered choice is:" +ch);
switch(ch)
{
case 1:
r1.intgen();
break;
case 2:
r1.dbgen();
break;
case 3:
r1.gauss();
break;
}
}
while(ch<4);
}
}

3.VOWELS:

import java.io.*;
public class vowels
{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE STRING");
String S =in.readLine();
int len=S.length();
int vow=0,con=0,comma=0,space=0,per=0;
char ch[]=new char[len];
S.getChars(0,len,ch,0);
CharArrayReader input=new CharArrayReader(ch);
int i;
while((i=input.read())!=-1)
{
switch(i)
{
case'.':per=per+1;
break;
case' ':space=space+1;
break;
case',':comma=comma+1;
break;
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vow=vow+1;
break;
default:
con=con+1;
break;
}
}
System.out.println("\t STRING MANIPULATION");
System.out.println("\t*********************");
System.out.println("\t\t The given string is:"+S);
int v1=vow;
int c1=con;

int com1=comma;
int p1=per;
int s1=space;
if(v1==0)
System.out.println("\t\t no vowels exist");
System.out.println("\t\t no.of.vowels:"+vow);
if(c1==0)
System.out.println("\t\t no consonants exist");
else
System.out.println("\t\t no.of.consonants:"+con);
if(com1==0)
System.out.println("\t\t no comma's exist");
else
System.out.println("\t\t no.of.comma's:"+comma);
if(p1==0)
System.out.println("\t\t no periods exist");
else
System.out.println("\t\t no.of.periods:"+per);
if(s1==0)
System.out.println("\t\t no space exist");
else
System.out.println("\t\t no.of.space:"+space);
}
}

4.THREAD:

import java.io.*;
class NewThread extends Thread
{
          NewThread()
     {
                  /* create a new, second thread */
                  super("Demo Thread");
                  System.out.println("Child thread : " + this);
                  start();                // start the thread
     }
  

          /* this is the entry point for second thread */


          public void run()
     {
                  try
         {
                          for(int n=5; n>0; n--)
             {
                                  System.out.println("Child thread : " + n);
                                  Thread.sleep(500);
             }
         }
                  catch(InterruptedException e)
         {
                          System.out.println("Child interrupted..!!");
         }
         
                  System.out.println("Exiting child thread...");
     }
}   
class JavaProgram
{
          public static void main(String[] args)
     {
         
                  new NewThread();          // create a new thread
         
                  try
         {
                          for(int n=5; n>0; n--)
             {
                                  System.out.println("Main Thread : " + n);
                                  Thread.sleep(1000);
             }
         }
                  catch(InterruptedException e)
         {
                          System.out.println("Main thraed interrupted..!!");
         }
       
                System.out.println("Exiting main thread");
                 
     }
}

5.DIVISION BY ZERO:

import java.io.*;
class india
{
public static void main(String[] args)
{
int a=10;
int b=5;
int c=5;
int x,y;

try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("DIVISION BY ZERO");
System.out.println(".................");

}
y=a/(b+c);
System.out.println("\n\t y="+y);
}
}

6.

import java.io.*;
class me
{
public static void main(String args[])
{
int a[]={5,10};

int b=5;

try

{
int x=a[2]/b-a[1];
}
catch(ArithmeticException    e)
{
System.out.println("\n\t\t Division by zero");
System.out.println("**********************");
}
catch(ArrayIndexOutOfBoundsException        e)
{
System.out.println("\n\t Array Index error");
}
catch(ArrayStoreException    e)
{
System.out.println("wrong data type");
}
int y=a[1]/a[0];
System.out.println("\n\t y="+y);
}
}

7.FIBO:

class fibo extends Thread


{
int fib1,fib2;
public fibo(int fib1,int fib2)
{
this.fib1=fib1;
this.fib2=fib2;
}
public void run()
{
for(int i=0;i<5;i++)
{
int fib;
fib=fib1+fib2;
System.out.println("\n\t"+fib);
fib1=fib2;
fib2=fib;
try
{
sleep(500);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
public static void main (String[] args) {

System.out.println("\n\t    IMPLEMENTATION OF FIBONACCI SERIES");


System.out.println("\n\t*********************************");
fibo f1=new fibo(0,1);
fibo f2=new fibo(0,-1);
f1.start();
f2.start();
}
}

You might also like