import java.util.*;
class Fib
{
public static void main(String args[])
{
Scanner a1=new Scanner(System.in);
System.out.println("enter the limit to generate series");
int a =a1.nextInt();
int f0=0,f1=1,f2,i;
System.out.println("Series is as follows:");
System.out.println(f0+ "\n" +f1);
for(i=1;i<=a-2;i++)
{
f2=f0+f1;
f0=f1;
f1=f2;
System.out.println(f2);
}
}
}
Output:-
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac Fib.java
C:\java\bin>java Fib
enter the limit to generate series
10
Series is as follows:
0
1
1
2
3
5
8
13
21
34
Showing posts with label JAVA HELPLINE. Show all posts
Showing posts with label JAVA HELPLINE. Show all posts
Program to show the concept of inheritance using super keyword.
class A
{
int length;
int breadth;
void A1(int x, int y)
{
length=x;
breadth=y;
}
}
class B extends A
{
int height;
int volume(int x,int y,int z)
{
super.A1(x,y);
height=z;
return (length*breadth*height);
}
}
class Inher
{
public static void main(String ar[])
{
B h=new B();
int volume1=h.volume(2,1,3);
System.out.println("volume is="+volume1);
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Inher.java
C:\java\bin>java Inher
volume is=6
C:\java\bin>
{
int length;
int breadth;
void A1(int x, int y)
{
length=x;
breadth=y;
}
}
class B extends A
{
int height;
int volume(int x,int y,int z)
{
super.A1(x,y);
height=z;
return (length*breadth*height);
}
}
class Inher
{
public static void main(String ar[])
{
B h=new B();
int volume1=h.volume(2,1,3);
System.out.println("volume is="+volume1);
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Inher.java
C:\java\bin>java Inher
volume is=6
C:\java\bin>
Program to print pyramid of prime numbers.
import java.util.*;
class Pyd
{
public static void main(String ar[])
{
int m,k=2,flag=0,row;
for(row=1;row<=3;row++)
{
for(int i=1;i<=3-row;i++)
{
System.out.print(" ");
}
m=1;
while(m<=row)
{
flag=0;
for(int i=2;i<=k/2;i++)
{
if(k%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(k+" ");
m++;
}
k++;
}
System.out.println();
}
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Pyd.java
C:\java\bin>java Pyd
2
3 5
7 11 13
class Pyd
{
public static void main(String ar[])
{
int m,k=2,flag=0,row;
for(row=1;row<=3;row++)
{
for(int i=1;i<=3-row;i++)
{
System.out.print(" ");
}
m=1;
while(m<=row)
{
flag=0;
for(int i=2;i<=k/2;i++)
{
if(k%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(k+" ");
m++;
}
k++;
}
System.out.println();
}
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Pyd.java
C:\java\bin>java Pyd
2
3 5
7 11 13
Program to show interthread communication by producer and consumer problem.
class Q
{
int n;
boolean valueSet=false;
synchronized int get()
{
while(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("got: "+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n=n;
valueSet=true;
System.out.println("put :"+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class Synch
{
public static void main(String arg [])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("press control c to stop");
}
}
Output:-
got: 5803
put :5804
got: 5804
put :5805
got: 5805
put :5806
got: 5806
put :5807
got: 5807
put :5808
got: 5808
put :5809
got: 5809
put :5810
got: 5810
put :5811
got: 5811
put :5812
got: 5812
put :5813
got: 5813
put :5814
got: 5814
put :5815
got: 5815
put :5816
got: 5816
put :5817
got: 5817
put :5818
got: 5818
put :5819
got: 5819
put :5820
got: 5820
put :5821
got: 5821
put :5822
got: 5822
put :5823
got: 5823
C:\java\bin>
{
int n;
boolean valueSet=false;
synchronized int get()
{
while(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("got: "+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n=n;
valueSet=true;
System.out.println("put :"+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class Synch
{
public static void main(String arg [])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("press control c to stop");
}
}
Output:-
got: 5803
put :5804
got: 5804
put :5805
got: 5805
put :5806
got: 5806
put :5807
got: 5807
put :5808
got: 5808
put :5809
got: 5809
put :5810
got: 5810
put :5811
got: 5811
put :5812
got: 5812
put :5813
got: 5813
put :5814
got: 5814
put :5815
got: 5815
put :5816
got: 5816
put :5817
got: 5817
put :5818
got: 5818
put :5819
got: 5819
put :5820
got: 5820
put :5821
got: 5821
put :5822
got: 5822
put :5823
got: 5823
C:\java\bin>
Program to print spiral through applet.
import java.awt.*;
import java.applet.*;
//
public class spiral extends Applet
{
public void paint(Graphics g)
{
int x1=350,y1=350,x2=400,y2=350,y3,x3,x4,y4,i;
g.drawLine(x1,y1,x2,y2);
x3=x2;
g.drawLine(x2,y2,x3,y3=y2+45);
y4=y3;
x4=x1-25;
g.drawLine(x3,y3,x4,y4);
for(i=1;i<10;i++)
{
x1=x4;
y1=y1-25;
g.drawLine(x4,y4,x1,y1);
y2=y1;
x2=x2+25;
g.drawLine(x1,y1,x2,y2);
x3=x2;
y3=y3+25;
g.drawLine(x2,y2,x3,y3);
y4=y3;x4=x1-25;
g.drawLine(x3,y3,x4,y4);
}
}
}
import java.applet.*;
//
public class spiral extends Applet
{
public void paint(Graphics g)
{
int x1=350,y1=350,x2=400,y2=350,y3,x3,x4,y4,i;
g.drawLine(x1,y1,x2,y2);
x3=x2;
g.drawLine(x2,y2,x3,y3=y2+45);
y4=y3;
x4=x1-25;
g.drawLine(x3,y3,x4,y4);
for(i=1;i<10;i++)
{
x1=x4;
y1=y1-25;
g.drawLine(x4,y4,x1,y1);
y2=y1;
x2=x2+25;
g.drawLine(x1,y1,x2,y2);
x3=x2;
y3=y3+25;
g.drawLine(x2,y2,x3,y3);
y4=y3;x4=x1-25;
g.drawLine(x3,y3,x4,y4);
}
}
}
Program to find reverse of a string but returning in same order
import java.util.*;
class Reverse
{
public static void main(String fj[])
{
int b=0,j=-2,j1;
Scanner s1=new Scanner(System.in);
System.out.println("\nEnter the string");
String str=s1.nextLine();
int v=0;
System.out.println("\n\n");
for(int i=0;i {
int r=str.charAt(i); if((((int)r)==32)||(i==(str.length()-1)))
{
j++;
for(j1=j;j1>=b;j1--)
{
System.out.print(str.charAt(j1));
}
System.out.print(" ");
j++;
b=j;
}
else
{
j++;
}
}
System.out.println("\n\n");
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Reverse.java
C:\java\bin>java Reverse
Enter the string
MEENAKSHI SHARMA
IHSKANEEM AMRAHS
class Reverse
{
public static void main(String fj[])
{
int b=0,j=-2,j1;
Scanner s1=new Scanner(System.in);
System.out.println("\nEnter the string");
String str=s1.nextLine();
int v=0;
System.out.println("\n\n");
for(int i=0;i
int r=str.charAt(i); if((((int)r)==32)||(i==(str.length()-1)))
{
j++;
for(j1=j;j1>=b;j1--)
{
System.out.print(str.charAt(j1));
}
System.out.print(" ");
j++;
b=j;
}
else
{
j++;
}
}
System.out.println("\n\n");
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd java
C:\java>cd bin
C:\java\bin>javac Reverse.java
C:\java\bin>java Reverse
Enter the string
MEENAKSHI SHARMA
IHSKANEEM AMRAHS
Program to show concept of overloading.
import java.util.*;
class Overl
{
int a;
double area(double r)
{
return 3.14*r*r;
}
double area(double l,double b)
{
return l*b;
}
int area(int a)
{
return a*a;
}
}
class Main
{
public static void main(String ar[])
{
Overl o1=new Overl();
double c,d;
int n;
c=o1.area(5.2);
System.out.println("area of circle is="+c);
d=o1.area(2.3,2.3);
System.out.println("area of rectangle is="+d);
n=o1.area(4);
System.out.println("area of square is="+n);
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac Main.java
C:\java\bin>java Main
area of circle is=84.90560000000002
area of rectangle is=5.289999999999999
area of square is=16
C:\java\bin>
class Overl
{
int a;
double area(double r)
{
return 3.14*r*r;
}
double area(double l,double b)
{
return l*b;
}
int area(int a)
{
return a*a;
}
}
class Main
{
public static void main(String ar[])
{
Overl o1=new Overl();
double c,d;
int n;
c=o1.area(5.2);
System.out.println("area of circle is="+c);
d=o1.area(2.3,2.3);
System.out.println("area of rectangle is="+d);
n=o1.area(4);
System.out.println("area of square is="+n);
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac Main.java
C:\java\bin>java Main
area of circle is=84.90560000000002
area of rectangle is=5.289999999999999
area of square is=16
C:\java\bin>
Program to create your own exception class
import java.lang.Exception;
import java.util.*;
class MyExp extends Exception
{
MyExp(String message)
{
super(message);
}
}
class My {
public static void main(String ar[])
{
System.out.println("Enter value of x and y"); Scanner a=new Scanner(System.in);
float x=a.nextFloat();
float y=a.nextFloat();
try
{
float z=x/y;
if(z<0.1)
{
throw new MyExp("Number is too small");
}
}
catch(MyExp e)
{
System.out.println("My Exception is caught");
System.out.println(e.getMessage());
}
finally
{
System.out.println("Hello mca");
}
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac My.java
C:\java\bin>java My
Enter value of x and y
1.0
11.0
My Exception is caught
Number is too small
Hello mca
C:\java\bin>
import java.util.*;
class MyExp extends Exception
{
MyExp(String message)
{
super(message);
}
}
class My {
public static void main(String ar[])
{
System.out.println("Enter value of x and y"); Scanner a=new Scanner(System.in);
float x=a.nextFloat();
float y=a.nextFloat();
try
{
float z=x/y;
if(z<0.1)
{
throw new MyExp("Number is too small");
}
}
catch(MyExp e)
{
System.out.println("My Exception is caught");
System.out.println(e.getMessage());
}
finally
{
System.out.println("Hello mca");
}
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac My.java
C:\java\bin>java My
Enter value of x and y
1.0
11.0
My Exception is caught
Number is too small
Hello mca
C:\java\bin>
Program to print histogram of numbers
import java.util.*;
class Histogram
{
public static void main(String arg[])
{
System.out.println("Enter any number");
Scanner a1= new Scanner(System.in);
int n= a1.nextInt();
int i=0,j,k,r;
int ar[]=new int[50];
while(n>0)
{
r=n%10;
ar[i]=r;
i++;
n=n/10;
}
for(j=ar[i];j<=0;j--)
{
k=ar[i];
for(int l=0;l
System.out.print("*");
}
System.out.println("");
i--;
}
}
}
Output:-
C:\Documents and Settings\Administrator>cd\
C:\>cd\java\bin
C:\java\bin>javac Histogram.java
C:\java\bin>java Histogram
Enter any number
342
***
****
**
Java Buzzwords or Java features
Hello friends,
In previous article i discuss introduction of Java language.In this article i discuss about features of this language which makes this different from others.
Features:-
1.Simple
2.Object oriented.
3.Robust
4.Multithreaded
5.Architecture neutral
6 Interpreted
7.High performance.
8.Distributed.
9.Dynamic.
Simple:-
Java was designed to be easy for the professional programmer to learn and use effectively.If you are exoerienced in c++ then you have to put a little effort only to get perfection in Java because croticle topics of c++ avoided by Java such as pointers ,operator overloading etc.
Object- oriented:-
Everything is an Object in java.Object model in Java is simple and easy to extend,while simple types,such as integers,are kept as high -performance non objects.
Robust:-
To better understand how Java is robust,consider two of the main reasons for program failure:memory management mistakes and mishandled exceptional conditions.Memory management can be difficult in traditional programming environments like c,c++,the programmer must manually allocate and free all dynamic memory.This sometimes leads to problems,because programmer will either forget to free memory that has been previously allocated or,worse ,try to free some memory that another part of their code is still using.Java virtually eliminates these problems by managing memory allocation and deallocation for you.Exceptional conditions in traditional environments often arise in situations such as "division by zero or file not found",and they must be managed withclumsy and hard to read constructs.Java helps in this area by providing object oriented exception handling.In well written java program ,all run time errors can and should be managed by program.
Multithreaded:-
Java was designed to meet the real world requirement of creating interactive ,networked programs.To accomplish this Java support multithreaded programming which allows you to write programs that do many things simultaniously.
Architecture neural:-
A central issue for the java designers was that of code longevity and portability.One of the main problems facing programmers is that no guarrontee exists that if you write a program today,it will run tomorrow even on the same machine.Then java designer design virtual machine .The main goal of that machine was"write once,run anywhere,any time,forever."
Portability:-
Java is a portable language.Portable means it can run on every hardware because virtual machine convert its code such that it can handle every hardware.
Interpretor:-
J va not only y use compilerp but also use interpretor which convert code into bytecode by virtual machine for portability.
High proformance-
Due to run the bytecode at anywher at anytime its performance increase.
Distributed:-
Java user can run code t remote area by following distributebd environment.
Dynamic:-
Java language is basically dynamically run.Object change at run time.
Friends ,it is something about java features.i think you learn something from it.
Thanks.
In previous article i discuss introduction of Java language.In this article i discuss about features of this language which makes this different from others.
Features:-
1.Simple
2.Object oriented.
3.Robust
4.Multithreaded
5.Architecture neutral
6 Interpreted
7.High performance.
8.Distributed.
9.Dynamic.
Simple:-
Java was designed to be easy for the professional programmer to learn and use effectively.If you are exoerienced in c++ then you have to put a little effort only to get perfection in Java because croticle topics of c++ avoided by Java such as pointers ,operator overloading etc.
Object- oriented:-
Everything is an Object in java.Object model in Java is simple and easy to extend,while simple types,such as integers,are kept as high -performance non objects.
Robust:-
To better understand how Java is robust,consider two of the main reasons for program failure:memory management mistakes and mishandled exceptional conditions.Memory management can be difficult in traditional programming environments like c,c++,the programmer must manually allocate and free all dynamic memory.This sometimes leads to problems,because programmer will either forget to free memory that has been previously allocated or,worse ,try to free some memory that another part of their code is still using.Java virtually eliminates these problems by managing memory allocation and deallocation for you.Exceptional conditions in traditional environments often arise in situations such as "division by zero or file not found",and they must be managed withclumsy and hard to read constructs.Java helps in this area by providing object oriented exception handling.In well written java program ,all run time errors can and should be managed by program.
Multithreaded:-
Java was designed to meet the real world requirement of creating interactive ,networked programs.To accomplish this Java support multithreaded programming which allows you to write programs that do many things simultaniously.
Architecture neural:-
A central issue for the java designers was that of code longevity and portability.One of the main problems facing programmers is that no guarrontee exists that if you write a program today,it will run tomorrow even on the same machine.Then java designer design virtual machine .The main goal of that machine was"write once,run anywhere,any time,forever."
Portability:-
Java is a portable language.Portable means it can run on every hardware because virtual machine convert its code such that it can handle every hardware.
Interpretor:-
J va not only y use compilerp but also use interpretor which convert code into bytecode by virtual machine for portability.
High proformance-
Due to run the bytecode at anywher at anytime its performance increase.
Distributed:-
Java user can run code t remote area by following distributebd environment.
Dynamic:-
Java language is basically dynamically run.Object change at run time.
Friends ,it is something about java features.i think you learn something from it.
Thanks.
Introduction to Java programming language
Hello friends,
Today is Java world means this language use frequently by software developers.It takes features of c and c++ language.Critical features of c and c++ ignored by Java.most of the features of Java inherited from c and c++.Java was developed by James Gosling,Patrick Naughton,Chris Warth,Ed Frank and Mike Sheridan at Sun Microsofts,inc. In 1991.It took 18 months to dvelop the first working version.This language was initially called "Oak" but was renamed "Java" in 1995.The name Java was also contain some reasons.Java is a cofee name which was taken by developers during development of this language.Java was developed by Gosling for designing a Star operating system.Originally Java not created for internet but it was for creating software embedded in various consumer electronics devices,such as microwave ovens and remote controls.The trouble with c and c++ is that they are designed to be compiled for a specific target.Although it is posible to compile a c++ program for just about any type of Cpu,to do so requires a full c++ compiler targeted for that cpu.The problem is that compilers are expensive and time consuming to create.An easier and more cost efficient solution was needed.In an attempt to find sucha solution Gosling and others began work on a portable ,plateform- independent language that could be used to produce code that would run on a variety of CPUs under differing environments.This efforts ultimately led to the creation of Java.I will discuss features if Java language in next article.
THANKS.
Stay in touch.
Today is Java world means this language use frequently by software developers.It takes features of c and c++ language.Critical features of c and c++ ignored by Java.most of the features of Java inherited from c and c++.Java was developed by James Gosling,Patrick Naughton,Chris Warth,Ed Frank and Mike Sheridan at Sun Microsofts,inc. In 1991.It took 18 months to dvelop the first working version.This language was initially called "Oak" but was renamed "Java" in 1995.The name Java was also contain some reasons.Java is a cofee name which was taken by developers during development of this language.Java was developed by Gosling for designing a Star operating system.Originally Java not created for internet but it was for creating software embedded in various consumer electronics devices,such as microwave ovens and remote controls.The trouble with c and c++ is that they are designed to be compiled for a specific target.Although it is posible to compile a c++ program for just about any type of Cpu,to do so requires a full c++ compiler targeted for that cpu.The problem is that compilers are expensive and time consuming to create.An easier and more cost efficient solution was needed.In an attempt to find sucha solution Gosling and others began work on a portable ,plateform- independent language that could be used to produce code that would run on a variety of CPUs under differing environments.This efforts ultimately led to the creation of Java.I will discuss features if Java language in next article.
THANKS.
Stay in touch.
Subscribe to:
Posts (Atom)