Download presentation
Presentation is loading. Please wait.
Published byLisa Osborne Modified over 9 years ago
1
Character Encoding & Handling doubles Pepper
2
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)
3
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 BlueJ window.
4
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); }}}
5
Doubles – not exact Stored binary, so.41 is actually a repeating sequence starting with.01101000111101011100001010001111 http://www.mathsisfun.com/binary-decimal- hexadecimal-converter.html To explain how decimal portions convert: http://www.mathsisfun.com/base- conversion-method.html
6
Converting 41/100 to binary PlaceEquationResult 1Note decimal. 2.41*2 =.820 3.82*2 = 1.641 4.64*2 = 1.281 5.28*2 =.560 Result: a repeating pattern of:.01101000111101011100001010001111
7
doubles – check equal small differences mean little errors creep in. Math.abs(dollars1 - dollars2) <.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
8
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"); } }}
9
printing double System.out.printf("x is %.2f ", x); OR import java.text.DecimalFormat; DecimalFormat twoDec = new DecimalFormat("0.00"); System.out.println("x is " + twoDec.format(x));
10
More on Decimal Formatter DecimalFormatter object knows: How to print decimals Package to import: import java.text.DecimalFormat; How to create a DecimalFormatter for your numbers: DecimalFormat percentage2 = new DecimalFormat(“0.00%”); How to use a DecimalFormatter to print to your screen: System.out.println(percentage2.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
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.