Selaa lähdekoodia

maggie.md

A description of how the BinaryConverter class works
mpierse 8 vuotta sitten
vanhempi
commit
e84db38a6b
1 muutettua tiedostoa jossa 3 lisäystä ja 0 poistoa
  1. 3
    0
      maggie.md

+ 3
- 0
maggie.md Näytä tiedosto

@@ -0,0 +1,3 @@
1
+    The binary converter class prints to the console decimal numbers from -5 to 32. The main method of the class starts with a for loop that determins the range of numbers to be printed in decimal (-5 to 32). It then prints to the counsole three lines for every number in that range: Line1: a decimal number then its binary conversion found using a method written in the class. Line2: just the decimal number to be converted. Line 3: the decimal number then its binary conversion found using a method from the Integer class.
2
+    The second method toBinary() converts a decimal number to binary. We create a boolean variable that stores whether the decimal number is positive or negative, then we use the absolute value of the decimal number. This allows us to convert the number as a positive number, then add a negative sign back in at the end of the method of necessary with a conditional statement and the stored boolean value. The actual number conversion takes place in a while loop that checks whether or not there is a remainder of 1 or 0 when you divide the decimal number by 2. The resulting digit is added to a string. The decimal number is then divided by 2 and contines to loop until the decimal number is equal to 1 or 0. We then run an assert statement to check that only 0s and 1s were concated to the string. We then add the final 1 or zero to the string that is stored in the base10Num variable. We then run the result (binary string) through a method from this class that double checks that the binary number only consists of 0s and 1s. Lastly we return to the boolean storing the positive or negative value, and add a "-" to the string is the number is suppose to be negative.
3
+    The third method loops through the characters in the binary string and asserts that each character is a 0 or 1 and there are no null values.