Monday, November 3, 2014

Java Variable pro



Java static member variable

public class StaticMemberExample
{
public static void main(String[] args)
{
ObjectCounter object1 = new ObjectCounter();
System.out.println(object1.getNumberOfObjects());
ObjectCounter object2 = new ObjectCounter();
System.out.println(object2.getNumberOfObjects());
  }
}
class ObjectCounter
{
static int counter=0;
public ObjectCounter()
{
counter++;
}
public int getNumberOfObjects()
{
return counter;
   }
}

Java static method

public class StaticMethodExample
{
public static void main(String[] args)
{
int result = MathUtility.add(1, 2);
System.out.println("(1+2) is : " + result);
   }
}
class MathUtility
{
public static int add(int first, int second)
{
return first + second;
   }
}     
 
Java Final variable

public class FinalVariableExample
{   
public static void main(String[] args)
{
 final int hoursInDay=24;
hoursInDay=12;         
System.out.println("Hours in 5 days = " + hoursInDay * 5);
  }
}

Generate Bouncing Lines Using Applet

import java.awt.*;
import java.applet.Applet;
public class BouncingLines extends Applet implements Runnable
          {
Thread runner = null;
final static int Width  = 200;
final static int Height = 100;

Image    image;
Graphics graphics;
int[] x1;
int[] y1;
int[] x2;
int[] y2;

int dx1 = 2 + (int)( 3 * Math.random() );
int dy1 = 2 + (int)( 3 * Math.random() );
int dx2 = 2 + (int)( 3 * Math.random() );
int dy2 = 2 + (int)( 3 * Math.random() );
static int first = 0;
final static int Lines = 50;

public void init()
{
x1 = new int[Lines];
y1 = new int[Lines];
x2 = new int[Lines];
y2 = new int[Lines];
x1[0] = (int)( Width  * Math.random() );
y1[0] = (int)( Height * Math.random() );
x2[0] = (int)( Width  * Math.random() );
y2[0] = (int)( Height * Math.random() );
for ( int i = 1; i < Lines; i++ )
 {
x1[i] = x1[0];
y1[i] = y1[0];
x2[i] = x2[0];
y2[i] = y2[0];
}
image    = createImage( Width, Height );
graphics = image.getGraphics();
public void start()
{
if ( runner == null )
{
runner = new Thread( this );
runner.start();
  }
public void stop()
{
if ( runner != null && runner.isAlive() )
runner.stop();
runner = null;
}
public void run()
{
  
while (runner != null)
{
repaint();
try
{
 Thread.sleep( 20 );
}
catch ( InterruptedException e )
{
}
}
}
public void paint( Graphics g )
{
update( g );
}
public void update( Graphics g )
{
graphics.setColor( Color.black );
graphics.fillRect( 0, 0, Width, Height );
for(int r=4;r<=9;r++)
{
 graphics.setColor( Color.green );
 int line = first;
for ( int i = 0; i < Lines; i++ )
{
 graphics.drawLine( x1[line], y1[line],
x2[line], y2[line] );
line++;
if ( line == Lines ) line = 0;
}
line = first;
first--;
if ( first < 0 ) first = Lines - 1;
  
x1[first] = x1[line];
y1[first] = y1[line];
x2[first] = x2[line];
y2[first] = y2[line];

if (x1[first] + dx2 < Width)
x1[first] += dx1;
else
dx1 = -(2 + (int)( 3 * Math.random() ));
if (x1[first] + dx1 >= 0)
x1[first] += dx1;
else
dx1 = 2 + (int)( 3 * Math.random() );
if (y1[first] + dy1 < Height)
y1[first] += dy1;
else
dy1 = -(2 + (int)( 3 * Math.random() ));
if (y1[first] + dy1 >= 0)
y1[first] += dy1;
else
dy1 = 2 + (int)( 3 * Math.random() );
if (x2[first] + dx2 < Width)
x2[first] += dx2;
else
dx2 = -(2 + (int)( 3 * Math.random() ));
if (x2[first] + dx2 >= 0)
x2[first] += dx2;
else
dx2 = 2 + (int)( 3 * Math.random() );
if (y2[first] + dy2 < Height)
y2[first] += dy2;
else
dy2 = -(2 + (int)( 3 * Math.random() ));
if (y2[first] + dy2 >= 0)
y2[first] += dy2;
else
dy2 = 2 + (int)( 3 * Math.random() );
g.drawImage( image, 0, 0, this );
     }
   }
}


Java Convert int Array to String

import java.util.Arrays;
public class ConvertIntArrayToStringExample
{
public static void main(String args[])
{
int[] intNumbers = new int[]{1, 2, 3, 4, 5};
StringBuffer sbfNumbers = new StringBuffer();
String strSeparator = " ";
if(intNumbers.length > 0)
{
sbfNumbers.append(intNumbers[0]);
for(int i=1; i < intNumbers.length; i++)
{
sbfNumbers.append(strSeparator).append(intNumbers[i]);
 }
}
System.out.println("int array converted to String using for loop");
System.out.println(sbfNumbers.toString());
String strNumbers = Arrays.toString(intNumbers);
System.out.println("String generated from Arrays.toString method: " + strNumbers);
strNumbers = strNumbers.replaceAll(", ", strSeparator).replace("[", "").replace("]", "");
System.out.println("Final String: " + strNumbers);
  }
}

No comments:

Post a Comment