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 BlueJ 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 hexadecimal-converter.html To explain how decimal portions convert: conversion-method.html
Converting 41/100 to binary PlaceEquationResult 1Note decimal. 2.41*2 = *2 = *2 = *2 =.560 Result: a repeating pattern of:
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
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 %.2f ", x); OR import java.text.DecimalFormat; DecimalFormat twoDec = new DecimalFormat("0.00"); System.out.println("x is " + twoDec.format(x));
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
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