xonecas.io
92
Nu

NumberConversion / commit 8b9f439

uploading old code

xonecas2011-05-11 8b9f43983994d6b3c077ad4a6dfcf0d8d800656a

4 files changed

NumberConversion.class+0−0
Binary file, not shown.
NumberConversion.java+84−0
@@ -0,0 +1,84 @@
1+// sean caetano martin
2+// seancaetanomartin@gmail.com
3+// cis 255 hy
4+// NumberConversion
5+// converts decimal numbers to binary, hex, octal.
6+// assign 1
7+// Wednesday, February 02 2011
8+
9+public class NumberConversion {
10+
11+ public static String convertToBinary (int decimal) {
12+
13+ // local variables
14+ int remainder = 0;
15+ String binary = "";
16+
17+ // division by 2 with remainder
18+ // continue while the quotient != 0
19+ do {
20+
21+ // save the integer value of the remiander (1 || 0)
22+ remainder = decimal % 2;
23+ // divide the number by 2 and capture the quotient
24+ decimal = decimal / 2;
25+
26+ // build our binary number in reverse order
27+ binary = remainder + binary;
28+
29+ } while (decimal != 0);
30+
31+ // return our binary number
32+ return binary;
33+
34+ }
35+
36+ public static String convertToOctal (int decimal) {
37+
38+ // local variables
39+ int remainder = 0;
40+ String octal = "";
41+
42+ // division by 8 with remainder
43+ // while the quotient != 0
44+ do {
45+
46+ // save the integer value of the remainder
47+ remainder = decimal % 8;
48+ // save the quotient
49+ decimal = decimal / 8;
50+
51+ // build the octal number
52+ octal = remainder + octal;
53+
54+ } while (decimal != 0);
55+
56+ // return the octal number
57+ return octal;
58+ }
59+
60+ public static String convertToHexadecimal (int decimal) {
61+
62+ // local variables
63+ int remainder = 0;
64+ String hex = "";
65+ String hexValues = "0123456789ABCDEF";
66+
67+ // division by 16 with remainder
68+ // while the quotient != 0
69+ do {
70+
71+ // save the integer value of the remainder
72+ remainder = decimal % 16;
73+ // save the quotient
74+ decimal = decimal / 16;
75+
76+ // build the hex number
77+ hex = hexValues.charAt(remainder) + hex;
78+
79+ } while (decimal != 0);
80+
81+ // return the hex number
82+ return hex;
83+ }
84+}
NumberConversionTest.class+0−0
Binary file, not shown.
NumberConversionTest.java+35−0
@@ -0,0 +1,35 @@
1+// sean caetano martin
2+// seancaetanomartin@gmail.com
3+// cis 255 hy
4+// NumberConversionTest
5+// converts decimal numbers to binary, hex, octal.
6+// assign 1
7+// Wednesday, February 02 2011
8+
9+public class NumberConversionTest {
10+
11+ public static void main (String[] args) {
12+
13+ // local variables
14+ int looper = 0;
15+ String binary = "";
16+ String octal = "";
17+ String hex = "";
18+
19+ // print header
20+ System.out.print("\nProgrammed by Sean Caetano Martin\n"+
21+ "Decimal Binary Octal Hexadecimal\n");
22+
23+ // loop and print all the numbers
24+ while (looper++ != 256) {
25+ System.out.printf("%-15d%-15s%-15s%-15s\n",
26+ looper,
27+ NumberConversion.convertToBinary(looper),
28+ NumberConversion.convertToOctal(looper),
29+ NumberConversion.convertToHexadecimal(looper)
30+ );
31+ }
32+ // clean output
33+ System.out.print("\n");
34+ }
35+}