public class BinaryConversion
{
public static void main(String args[])
{
if (args.length == 0)
{
System.out.println("Usage: java BinaryConversion [possitive integer]");
System.exit(1);
}
try
{
int number = (int) Double.parseDouble(args[0]);
System.out.println(convert(number));
}
catch (IllegalArgumentException e)
{
System.err.println("Error: Invalid Input");
System.exit(1);
}
}
public static String convert(int n)
{
String num = "";
while (n>0)
{
num = ""+n%2+""+num;
n = n >> 1;
}
return num;
}
}
public class BitShift
{
public static void main(String[] args)
{
int x = Integer.parseInt(args[0]) >> 1;
System.out.println("x/2 = "+x);
}
}
public class CoinToss
{
public static void main(String[] args)
{
int heads = 3;
int tails = 5;
System.out.println("Number of heads:" + heads);
System.out.println("Number of tails:" + tails);
double percentHeads = (heads*100) / 8;
System.out.println("Percent heads:" + percentHeads);
double percentTails = (tails*100) / 8;
System.out.println("Percent tails:" + percentTails);
}
}
public class LetterPermutation
{
public static void main(String args[])
{
String[] word = new String[(int)Math.pow(2,args[0].length())];
String[] letter = new String[args[0].length()];
for ( int i = 0 ; i < Math.pow(2,args[0].length()) ; i++ )
word[i] = "";
for ( int i = 0 ; i < letter.length ; i++ )
letter[i] = args[0].substring(i,i+1).toLowerCase();
for ( int i = 0 ; i < args[0].length() ; i++ )
{
for ( int j = 0 ; j < Math.pow(2,args[0].length()) ; j++ )
{
if (j% Math.pow(2,(i+1)) < Math.pow(2,i))
word[j] = word[j]+letter[i].toUpperCase();
else
word[j] = word[j]+letter[i];
}
}
for (int i = 0 ; i < Math.pow(2,args[0].length()) ; i++ )
System.out.println(word[i]);
}
}
/**
*A simple find an replace function.
*@author
*WarMage (warmage_666@hotmail.com)
**/
package com.gurusnetwork.warmage.regex;
public class SimpleRegEx
{
/**
*Main Method used as a command line test.
*@param find
*String to search for.
*@param replace
*String to replace the term searched for.
*@param data
*String to preform the find/replace on.
*
Precondition:
*A single work find string in args[0] a single word replace string
*in args[1] the data located in args[2] until end.
*
Postcondition:
*Print out a the data String after find/replace has be implemented.
**/
public static void main(String[] args)
{
String data = args[2];
if (args.length < 3)
{
System.out.println(
"Usage: java SimpleRegEx [find String] [replace String] [data String]");
System.exit(1);
}
for (int i = 3 ; i < args.length ; i++)
data = data + " " + args[i];
System.out.println("result = "+replace(args[0],args[1],data));
}
/**
*Method that does the find/replace.
*@param find
*String to search for.
*@param replace
*String to replace the term searched for.
*@param data
*String to preform the find/replace on.
*
Precondition:
*Three Strings have been input.
*
Postcondition:
*Return the data String after find/replace has be implemented.
**/
public static String replace(String find, String replace, String data)
{
int x = data.indexOf(find);
while(x != -1)
{
data = new String(data.substring(0,x)
+ replace
+ data.substring(x+find.length()));
x = data.indexOf(find);
}
return data;
}
}
public class TwoDecimals
{
public static void main(String[] args)
{
double x = 1.33333333;
double y = 1.579;
double z = 1.0756;
System.out.println("x = "+twoDecimals(x));
System.out.println("y = "+twoDecimals(y));
System.out.println("z = "+twoDecimals(z));
}
public static double twoDecimals(double num)
{
String result = "";
int base = (int)Math.floor(num);
int decimal = (int)Math.round( (num - base)*100);
if (decimal < 10)
result = ""+base+".0"+decimal+"";
else
result = ""+base+"."+decimal+"";
return Double.parseDouble(result);
}
}