Character Encoding Handling doubles Pepper Character encoding schemes
Character Encoding & Handling doubles Pepper
Character encoding schemes • EBCDIC – older with jumps in alphabet • ASCII 1967 (7 bit)– Handled English, – ASCII extended (8 bit) – handled special characters such as ñ and £ • UNICODE (16 bits) – all language – not Klingon, Egyption hieroglyphics (sometimes uses fewer bits)
Print Unicode table Create a for loop to run 340 times Convert count to char with a cast char x = (char) count; Print "When the number is " + count + " the character is " + x Be sure to set unlimited buffering on your Blue. J window.
character printing code public class characters { public static void main() { for (int count = 1; count < 500; count++){ char x = (char) count; System. out. println("When the number is " + count + " the character is " + x); }}}
Doubles – not exact • Stored binary, so. 41 is actually a repeating sequence starting with. 01101000111101011100001010001111 http: //www. mathsisfun. com/binary-decimalhexadecimal-converter. html To explain how decimal portions convert: http: //www. mathsisfun. com/base-conversion -method. html
Converting 41/100 to binary Place Equation Result 1 Note decimal . 2 . 41*2 =. 82 0 3 . 82*2 = 1. 64 1 4 . 64*2 = 1. 28 1 5 . 28*2 =. 56 0 Result: a repeating pattern of: . 01101000111101011100001010001111
doubles – check equal • small differences mean little errors creep in. • Math. abs(dollars 1 - dollars 2) <. 001 • ex: Write a program to Multiply. 41 *. 41 and compare it to. 1681 – print the values so you see the diff – try an == comparison to see it fail – try the test for no more than. 001 off
double – check equal public class testdouble { public static void main() { double x =. 41*. 41; double y =. 1681; System. out. println("x is " + x + " and y is " + y); if (x == y) { System. out. println("match with equal sign"); } else { System. out. println("no match with equal sign"); } if (Math. abs(x-y)<. 001) { System. out. println("match"); } else { System. out. println("no match"); } }}
printing double • System. out. printf("x is %. 2 f ", x); OR • import java. text. Decimal. Format; • Decimal. Format two. Dec = new Decimal. Format("0. 00"); • System. out. println("x is " + two. Dec. format(x));
More on Decimal Formatter Decimal. Formatter object knows: How to print decimals Package to import: import java. text. Decimal. Format; How to create a Decimal. Formatter for your numbers: Decimal. Format percentage 2 = new Decimal. Format(“ 0. 00%”); How to use a Decimal. Formatter to print to your screen: System. out. println(percentage 2. format(. 308)); // will print 30. 80% Different format options: 0 = required position # = show it if you have it % = make it a % (mult by 100) E = E notation (scientific) Now you change your program to print x with only 2 decimal places
Printf printf command: Just like println, but uses variables inside string start % and end character. %, then size of whole number, then decimal places and then type d = int f = float s = string c = char
- Slides: 11