Java Program Tuesday, Nov 23 2010 

Compute minimum of two numbers using conditional operator

 

class minnumber

{

public static void main(String args[])

{

//Taking value as command line arguments

int i=Integer.parseInt(args[0]);

int j=Integer.parseInt(args[1]);

int result=(i<j)?i:j;

System.out.println(“The minimum of the two numbers is:” +result);

}

}

Java Basic Programs Tuesday, Nov 23 2010 

1) Prints the Welcome

public class simple

{

public static void main(String args[])

{

System.out.println(“Welcome!”);

}

}

 

Output:

Welcome

 

2) Calling the greet method from another class

public class Greeting

{

/* Defining Methods*/

public void greet()

{

System.out.println(“Hi! Welcome”);

}

}

 

public class TestGreeting

{

public static void main(String args[])

{

/* Creating objects */

Greeting hello = new Greeting();

/* Calling the method using objects  from another class*/

hello.greet();

}

}

 

 

Output:

Hi! Welcome

 

3)Compute the biggest number among three numbers

 

class biggestnumber

{

public static void main(String args[])

{

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

int c = Integer.parseInt(args[2]);

if((a>b)&&(a>c))

System.out.println(“The biggest among the three numbers is:” +a);

else if((b>a)&&(b>c))

System.out.println(“The biggest among the three numbers is:” +b);

else

System.out.println(“The biggest among the three numbers is:” +c);

}

}

 

Output:

java biggestnumber 23 30 20

The biggest among the three numbers is:30

 

 

4) Prints the values stored in the primitive types and string

public class sample

{

public static void main(String args[])

{

int a=10;

float b=10000.0F;

String name= “Vignesh karthick”;

System.out.println(“Integer a:” +a);

System.out.println(“Float b:” +b);

System.out.println(“String name:” + name);

}

}

Output:

 

Integer a:10

Float b:10000.0

String name:Vignesh karthick

 

5) Computation distance light travels using long variables.

 

class Light

{

public static void main(String args[])

{

int lightspeed;

long days;

long seconds;

long distance;

// approximate speed of light in miles per second

lightspeed = 186000;

days = 1000; // specify number of days here

seconds = days * 24 * 60 * 60; // convert to seconds

distance = lightspeed * seconds; // compute distance

System.out.print(“In ” + days);

System.out.print(” days light will travel about “);

System.out.println(distance + ” miles.”);

}

}

Output:

In 1000 days light will travel about 16070400000000 miles.

 

6) Factorial of a given number

import java.io.*;

class factorial

{

 

public static void main(String args[])throws IOException

{

int i,n,f=1;

DataInputStream in=new DataInputStream(System.in);

System.out.println(“Enter the number:”);

n=Integer.parseInt(in.readLine());

System.out.println(“The factorial of” +n+ “is” );

for(i=1;i<=n;i++)

{

f=f*i;

}

System.out.println(f);

}

}

 

 

Output:

Enter the number:

5

The factorial of 5 is

120

 

 

7) Swapping of 2 numbers with third variable

class SwappingWithThirdVariable

{

public static void main(String args[])

{

int num1=50;

int num2=20;

System.out.println(“Before Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

swap(num1,num2);

}

public static void swap(int num1,int num2)

{

int temp;

temp=num1;

num1=num2;

num2=temp;

System.out.println(“After Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

}

}

Output:

Before Swapping

num1:50

num2:20

After Swapping

num1:20

num2:50

 

8) Swapping of 2 numbers without third variable

 

class SwappingWithOutThirdVariable

{

public static void main(String args[])

{

int num1=10;

int num2=20;

System.out.println(“Before Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

swap(num1,num2);

}

public static void swap(int num1,int num2)

{

num1=num1+num2;

num2=num1-num2;

num1=num1-num2;

System.out.println(“After Swapping”);

System.out.println(“num1:” + num1);

System.out.println(“num2:” + num2);

}

}

Output:

 

Before Swapping

num1:10

num2:20

After Swapping

num1:20

num2:10

 

 

9) Fibonnaci Series for a given number of times

import java.io.*;

class fibonacci

{

public static void main(String args[])throws IOException

{

int f1=-1,f2=1,f3,i,n;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the limit:”);

n=Integer.parseInt(in.readLine());

System.out.println(“Fibonacci Series are:”);

for(i=1;i<=n;i++)

{

f3=f1+f2;

f1=f2;

f2=f3;

System.out.println(f3);

}

}

}

 

 

 

Output:

Enter the limit:

7

Fibonacci Series are:

0

1

1

2

3

5

8

 

10) Calculation of Simple Interest

import java.io.*;

class simpleinterest

{

public static void main(String args[])throws IOException

{

int p,n;

float r,si,amt;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the principle amount:”);

p=Integer.parseInt(in.readLine());

System.out.println(“Enter the no. of years:”);

n=Integer.parseInt(in.readLine());

System.out.println(“Enter the rate of interest:”);

r=Float.parseFloat(in.readLine());

si=(p*n*r)/100;

amt=p+si;

System.out.println(“Simple Interest:” +si);

System.out.println(“Amount:” +amt);

}

}

 

 

Output:

 

Enter the principle amount:

1000

Enter the no. of years:

2

Enter the rate of interest:

2.5

Simple Interest:50.0

Amount:1050.0

 

11) Computation for Area of a Circle

 

class DoubleDemo

{

public static void main(String args[]) {

double pi, r, a;

r = 10.8; // radius of circle

pi = 3.1416; // pi, approximately

a = pi * r * r; // compute area

System.out.println(“Area of circle is ” + a);

}

}

 

Output:

 

Area of circle is 366.436224

 

12) Check whether the given number is prime  or not

public static void main(String args[])throws IOException

{

int i,num,remainder;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Enter the number:”);

num=Integer.parseInt(in.readLine());

for(i=2;i<num;i++)

{

remainder=num%i;

if(remainder==0)

{

System.out.println(num + ” is not prime”);

break;

}

}

if(i==num)

{

System.out.println(num + ” is  prime”);

}

}

}

 

Output:

 

Enter the number:

12

12 is not prime

 

13) Compute rightangledtriangle

 

 

import java.io.*;

public class rightangledtriangle

{

public static void main(String[] args) throws IOException

{

DataInputStream in = new DataInputStream(System.in);

int n;

System.out.println(“Enter the no. of rows:”);

n=Integer.parseInt(in.readLine());

for(int i=1;i<=n;i++)

{

for(int j=1;j<=i;j++)

{

System.out.print(” “+i+” “);

}

System.out.print(“\n”);

}

}

}

 

Output:

Enter the no. of rows:

5

The right angled triangle:

1

2  2

3  3  3

4  4  4  4

5  5  5  5  5

 

 

 

Sample Java Program Friday, Nov 19 2010 

class p1
{
public static void main(String args[])
{
System.out.println(“Welcome”);
}
}

How to use List in java. Sunday, Mar 28 2010 

List Interface:

  • List interface is part of java.util package. List interface can add value elements by add(value) method.
  • List can implement Vector, ArrayList class.
  • List value can get by Iterator interface.
  • A List is an ordered Collection (sometimes called a sequence).
  • Lists may contain duplicate elements. In addition to the operations inherited from Collection.

List Example

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ListExample {

public static void main(String[] args) {

// List Example implement with ArrayList
List<String> ls=new ArrayList<String>();

ls.add(“one”);
ls.add(“Three”);
ls.add(“two”);
ls.add(“four”);

Iterator it=ls.iterator();

while(it.hasNext())
{
String value=(String)it.next();

System.out.println(“Value :”+value);
}
}
}

Output

List Value : O ne
List Value :Three
List Value :two
List Value :four


Web 2.0 Framework Thursday, Aug 13 2009 

“Web 2.0″ refers to the second generation of web development and web design that facilitates information sharing, interoperability, user-centered design[1] and collaboration on the World Wide Web.

Examples include social-networking sites, video-sharing sites, wikis, blogs, mashups and folksonomies.

Web2_framework_p1

CHARACTERISTICS
Participation
Every aspect of Web 2.0 is driven by participation. The transition to
Web 2.0 was enabled by the emergence of platforms such as blogging,
social networks, and free image and video uploading, that collectively
allowed extremely easy content creation and sharing by anyone.
Standards
Standards provide an essential platform for Web 2.0. Common
interfaces for accessing content and applications are the
glue that allow integration across the many elements of
the emergent web.
Decentralization
Web 2.0 is decentralized in its architecture,
participation, and usage. Power and flexibility emerges
from distributing applications and content over many
computers and systems, rather than maintaining
them on centralized systems.
Openness
The world of Web 2.0 has only become possible
through a spirit of openness whereby developers
and companies provide open, transparent access
to their applications and content.
Modularity
Web 2.0 is the antithesis of the monolothic. It emerges
from many, many components or modules that are
designed to link and integrate with others, together
building a whole that is greater than the sum of its parts.
User Control
A primary direction of Web 2.0 is for users to control
the content they create, the data captured about their web
activities, and their identity. This powerful trend is driven by
the clear desires of participants.
Identity
Identity is a critical element of both Web 2.0 and the future direction
of the internet. We can increasingly choose to represent our identities
however we please, across interactions, virtual worlds, and social networks.
We can also own and verify our real identities in transactions if we choose.

Zoom Effect Friday, Jun 19 2009 

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Zoom id=”mzoom”
zoomHeightTo=”{Number(zoomHeightInput.text)}”
zoomWidthTo=”{Number(zoomWidthInput.text)}”/>
<mx:Panel x=”0″ y=”0″ width=”100%” height=”100%” paddingLeft=”10″ paddingRight=”10″
paddingBottom=”10″ paddingTop=”10″ title=”Applying Zoom Effects Using Data Binding”>
<mx:ApplicationControlBar height=”90″ width=”317″ y=”39″ x=”257.5″>
<mx:VBox height=”100%” width=”100%”>
<mx:Label text=”ZoomHeight” width=”80″ height=”16″/>
<mx:Spacer width=”91″/>
<mx:Label text=”ZoomWidth” width=”76″ height=”16″/>
</mx:VBox>
<mx:Spacer height=”48″/>
<mx:VBox height=”100%” width=”100%”>
<mx:TextInput  text=”10.0″ id=”zoomHeightInput” height=”21″ width=”142″/>
<mx:TextInput  text=”10.0″ id=”zoomWidthInput”  height=”21″ width=”142″/>
</mx:VBox>
</mx:ApplicationControlBar>
<mx:Image id=”img” source=”flower2.jpg” width=”10.0″ height=”10.0″
rollOverEffect=”{mzoom}”/>
</mx:Panel>
</mx:Application>

Wiping Effect Friday, Jun 19 2009 

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>

<!–Defining Effects–>
<mx:WipeLeft id=”wplf”  duration=”5000″ />
<mx:WipeRight id=”wprg” duration=”5000″/>
<mx:Panel  title=”Trigger Effects” paddingBottom=”10″ paddingTop=”10″
paddingLeft=”10″ paddingRight=”10″ y=”0″ x=”10″ height=”100%” width=”100%”>
<mx:Button label=”Wiping Right”  mouseUpEffect=”{wprg}” />
<mx:Image id=”img1″ source=”flower2.jpg” height=”149″ width=”200″
mouseUpEffect=”{wprg}”/>
<mx:Button id=”b1″ label=”Wiping Left”  mouseDownEffect=”{wplf}”/>
<mx:Image id=”img” source=”flowers.jpg” height=”149″ width=”200″
mouseDownEffect=”{wplf}” />
<mx:ControlBar height=”94″>
</mx:ControlBar>
</mx:Panel>
</mx:Application>

Rotate Effect Friday, Jun 19 2009 

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Script>
<![CDATA[
private function applyEffect(event:Event):void
{
rotateEffect.target=event.currentTarget;
rotateEffect.originX=event.currentTarget.width/2;
rotateEffect.originY=event.currentTarget.height/2;
rotateEffect.play();
}

]]>
</mx:Script>
<mx:Rotate id=”rotateEffect”>
</mx:Rotate>
<mx:VBox id=”vbox” x=”107″ y=”55″ clipContent=”false”>
<mx:Image id=”img1″ source=”flowers.jpg” creationComplete=”applyEffect(event)”/>
</mx:VBox>
</mx:Application>

Resizing Effect in Flex Friday, Jun 19 2009 

<?xml version=”1.0″?>
<!– behaviors\ButtonResize.mxml –>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” >
<mx:Resize id=”myResizeUp”
widthTo=”500″ heightTo=”900″
duration=”200″/>
<mx:Resize id=”myResizeDown”
widthTo=”30″ heightTo=”60″
duration=”200″ />
<mx:Panel paddingBottom=”10″ paddingTop=”10″ paddingLeft=”10″ paddingRight=”10″
width=”100%” height=”100%” title=”Resizing the image” fontSize=”12″ >
<mx:Label text=”Expands and Reduces the size of the image when clicks on the image”
fontSize=”12″/>
<mx:ControlBar>
</mx:ControlBar>
<mx:Image id=”img” source=”flower2.jpg” height=”149″ width=”200″
mouseDownEffect=”{myResizeDown}” mouseUpEffect=”{myResizeUp}” />
</mx:Panel>
</mx:Application>

Pause Effect in Flex Friday, Jun 19 2009 

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.effects.easing.*;

]]>
</mx:Script>

<mx:Sequence id=”movePauseMove”>
<mx:Move   xBy=”350″ duration=”2000″ easingFunction=”Bounce.easeOut”/>
<mx:Pause duration=”2000″/>
<mx:Move  xBy=”-350″ duration=”2000″ easingFunction=”Bounce.easeIn”/>
</mx:Sequence>

<mx:Panel title=”Pause Effect Example” width=”877″ height=”609″
paddingTop=”10″ paddingLeft=”10″ paddingRight=”10″ paddingBottom=”10″>

<mx:Text width=”325″ color=”blue” fontSize=”12″
text=”Click the image to start the Sequence effect.
The effect pauses for 2 seconds between moves. “/>
<mx:Image
source=”pink_rose.jpg”
mouseDownEffect=”{movePauseMove}”/>

</mx:Panel>
</mx:Application>

Next Page »

Follow

Get every new post delivered to your Inbox.