Program
import java.util.Scanner;
public class Bin2DecIterative
{
public static void main(String[] args)
{
String inStr;
boolean done = false;
Scanner in = new Scanner(System.in);
while (!done)
{
System.out.print("Enter a binary string or 'q' to quit: ");
inStr = in.nextLine();
if (inStr.equals("q"))
{
System.out.println("Bye!");
done = true;
}
else if (!isValidBinStr(inStr))
{
System.out.println("Error: Invalid binary string: \"" + inStr + "\", try again.");
}
else
{
System.out.println("The equivalent decimal number for \"" + inStr + "\" is " + bin2Dec(inStr));
}
}
}
public static boolean isValidBinStr(String binStr)
{
for (int i = 0; i < binStr.length(); ++i)
{
char binChar = binStr.charAt(i);
if (binChar != '0' && binChar != '1' && binChar != ' ')
{
return false;
}
}
return true;
}
public static int bin2Dec(String binStr)
{
int binStrLen = binStr.length();
int dec = 0;
for (int exp = 0; exp < binStrLen ; ++exp)
{
char binChar = binStr.charAt(binStrLen - 1 - exp);
if (binChar == '1')
{
dec += (int)Math.pow(2, exp);
}
}
return dec;
}
}
Program
import java.util.Scanner;
public class Hex2Dec
{
public static void main(String[] args)
{
String hexStr;
int hexStrLen;
int dec = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a Hexadecimal string: ");
hexStr = in.next();
hexStrLen = hexStr.length();
for (int exp = 0; exp < hexStrLen; ++exp)
{
char hexChar = hexStr.charAt(hexStrLen - 1 - exp);
int factor = (int)Math.pow(16, exp);
if (hexChar >= '1' && hexChar <= '9')
{
dec += (hexChar - '0') * factor;
}
else if (hexChar >= 'a' && hexChar <= 'f')
{
dec += (hexChar - 'a' + 10) * factor;
}
else if (hexChar >= 'A' && hexChar <= 'F')
{
dec += (hexChar - 'A' + 10) * factor;
}
else
{
System.out.println("Error: Invalid hex string \"" + hexStr + "\"");
System.exit(1);
}
}
System.out.println("The equivalent decimal for \"" + hexStr + "\" is " + dec);
}
}
Program
import java.util.Scanner;
public class Dec2Hex
{
public static void main(String[] args)
{
int dec;
String hexStr = "";
int radix = 16;
char[] hexChar = {'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
Scanner in = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
dec = in.nextInt();
while (dec > 0)
{
int hexDigit = dec % radix;
hexStr = hexChar[hexDigit] + hexStr;
dec = dec / radix;
}
System.out.println("The equivalent hexadecimal number is " + hexStr);
}
}
Program
import java.util.Scanner;
public class Hex2Bin
{
public static void main(String[] args)
{
String hexStr;
int hexStrLen;
String binStr ="";
String[] binStrs
= {"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
Scanner in = new Scanner(System.in);
System.out.print("Enter a Hexadecimal string: ");
hexStr = in.next();
hexStrLen = hexStr.length();
for (int pos = 0; pos < hexStrLen; ++pos)
{
char hexChar = hexStr.charAt(pos);
if (hexChar >= '0' && hexChar <= '9')
{
binStr += binStrs[hexChar-'0'];
}
else if (hexChar >= 'a' && hexChar <= 'f')
{
binStr += binStrs[hexChar-'a'+10];
}
else if (hexChar >= 'A' && hexChar <= 'F')
{
binStr += binStrs[hexChar-'A'+10];
}
else
{
System.err.println("Error: Invalid Hex string \"" + hexStr + "\"");
System.exit(1);
}
}
System.out.println("The equivalent binary for \"" + hexStr + "\" is \"" + binStr + "\"");
}
}
Guess a Number
import java.util.Scanner;
public class NumberGuess
{
public static void main(String[] args)
{
int secretNumber;
int numberIn;
int trialNumber = 0;
boolean done = false;
math.random() generates a double in [0.0, 1.0)
secretNumber = (int)(Math.random()*100);
Scanner in = new Scanner(System.in);
while (!done)
{
++trialNumber;
System.out.print("Enter your guess (between 0 and 99): ");
numberIn = in.nextInt();
if (numberIn == secretNumber)
{
System.out.println("Congratulation");
done = true;
}
else if (numberIn < secretNumber)
{
System.out.println("Try higher");
}
else
{
System.out.println("Try lower");
}
}
System.out.println("You got in " + trialNumber +" trials");
}
}
Bitwise Logical Operations
public class TestBitwiseOp
{
public static void main(String[] args)
{
int x = 0xAAAA_5555;
int y = 0x5555_1111;
System.out.printf("%d%n", x);
System.out.printf("%d%n", y);
System.out.printf("%08X%n", ~x);
System.out.printf("%08X%n", x & y);
System.out.printf("%08X%n", x | y);
System.out.printf("%08X%n", x ^ y);
}
}
Program
public class BitShiftTest
{
public static void main(String[] args)
{
int x = 0xAAAA5555;
int y = 0x55551111;
System.out.printf("%d%n", x);
System.out.printf("%d%n", y);
System.out.printf("%08X%n", x<<1 span="" style="mso-spacerun: yes;"> 1>
System.out.printf("%08X%n", x>>1);
System.out.printf("%d%n", x>>1);
System.out.printf("%08X%n", y>>1);
System.out.printf("%08d%n", y>>1);
System.out.printf("%08X%n", x>>>1);
System.out.printf("%d%n", x>>>1);
System.out.printf("%08X%n", y>>>1);
System.out.printf("%d%n", y>>>1);
int i1 = 12345;
System.out.println("i1 divides by 2 is " + (i1 >> 1));
System.out.println("i1 divides by 4 is " + (i1 >> 2));
System.out.println("i1 divides by 8 is " + (i1 >> 3));
int i2 = -12345;
System.out.println("i2 divides by 2 is " + (i2 >> 1));
System.out.println("i2 divides by 4 is " + (i2 >> 2));
System.out.println("i2 divides by 8 is " + (i2 >> 3));
}
}
No comments:
Post a Comment