Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Coding – part 2 David Davenport Computer Eng. Dept.,

Similar presentations


Presentation on theme: "Java Coding – part 2 David Davenport Computer Eng. Dept.,"— Presentation transcript:

1 Java Coding – part 2 David Davenport Computer Eng. Dept.,
data representations David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Last updated: separated out the Communications/DataRepresentations part from original slides! Previously: 08/10/2015 (little tidy up & recreated slide messed up by PowerPoint 2013!) Previously: 02/11/2014 (added all communication stuff at end)

2 IMPORTANT… Students… Instructors…
This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

3 In more depth… Data & DATA Types

4 Communication… Communication requires:
+ something that can be in more than one state + being able to detect a change of state (flag up rather than down) + a prior agreement on what the message is (enemy approaching, come for a drink, etc.)

5 Communicating… One man multiple messages Many men multiple messages
How to communicate multiple messages + allow flag to be in multiple states (still one message at a time) (is there a limit to the number of messages?) + have multiple flags (considered separately or as one?) (if taken as single state how can you know when they are “ready”?) One other option… + one man, sends sequence of signals which are combined to form complete message (Problem of synchronization –start and end? Also really need at least three states?) One man multiple messages Many men multiple messages combine state for single message?

6 Communicating… Can use anything… Abstractions Up / down On / Off X / O
True / False 1 / 0 Man with flag… + held up or down + held at say 8 positions + held at say 200 positions? Need to be able to detect each possible state Need a pre-determined convention to give meaning to each state. Any physical state could be used, e.g. water bottle, voltage, magnetic field, … Minimal setup would be something with just two states + map physical state to abstract symbol, e.g. T/F. 0/1, +/-, ?/*, … Can you differentiate (compare & contrast) + analogue vs. digital + parallel vs. serial

7 Data Types For now, use only the following… Primitive Non-primitive
int (for numeric integer, e.g. 5, -27, 0, 510…) double (for numeric real, e.g. 5.75, 3.0, -2.6…) char (for any character, e.g. A, a, B, b, 3, ?, &, … ) boolean (for true / false only) Non-primitive String (for any sequence of zero or more characters e.g. “CS101”, “A”, “Well done!”, … ) Note: Use only these limited Java primitive types & String for now. Will introduce other primitive & non-primitive (object) types later (& discuss them).

8 Numeric representations
Number bases Base 2 - binary digits 0 & 1 2n values 0  (2n – 1) 000 001 010 011 100 101 110 111 00 01 10 11 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 1 Normally use base 10, all digits from 0 up to 9 i.e 10-1 + for any base, numbers have digits from 0 up to the base-1 Can also have number bases greater than 10, eg. 16… hexadecimal use “digits” 0..9, A, B, C, D, E, F n-bits gives 2n values, 0 through 2n – 1 What about negative numbers? + in base 10, just put + or – in front (only need one bit to represent it!) + so in base 2, could use msb as sign, so +/- 2n-1 But then have +/- zero Alternatives include 1’s complement and 2’s complement… see ??? Note: octal and hexadecimal often used as cognitive aid for binary!

9 Data Types Primitive Non-primitive
byte, short, int, long (numeric integer) float, double (numeric real) char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char) boolean - true / false Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values anything & everything else! (we will come to these shortly) Note: Use only these Java primitive types & String for now. Will introduce other non-primitive (object) types later.

10 Primitive Numeric Types
byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 -9 x 1018 ±3.4 x 10±38 ±1.7 x 10±308 Max Value 127 32,767 2,147,483,647 9 x 1018 7 significant digits 15 significant digits integer real min & max are not exactly same… why? (because only one zero!) Real values comprise mantissa & exponent mantissa 0 upto 1.0, exponent times 10 to power of … each one is effectively a binary number (like integer) Result is limited precision…. For example, if a type could only represent 4 significant digits, a value would become i.e. the other digits would be zero. Note that the next values would be , then , the previous ones would be , , , … So, always have potential error in representation of real values - not always obvious which ones are in error, since decimal input is converted to binary! Some values cannot be represented exactly in decimal either e.g. 2/3 written in decimal is … (forever; cannot write on paper!) same for PI Limits on values is a major “feature” –problem/concern– in computation! What happens when limits of representations are reached? - eg. try to add/subtract one more to integer values (see previous sequences of 4 bit binary numbers…) Answer: goes from max pos to max neg (& vice versa)! Division by zero? ~ In integer? In real?

11 Data Types Primitive Non-primitive
byte, short, int, long (numeric integer) float, double (numeric real) char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char) boolean - true / false Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values anything & everything else! (we will come to these shortly) Note: Use only these Java primitive types & String for now. Will introduce other non-primitive (object) types later & even create our own. Consider… ? Why have numeric & string – can write 27.5 as string! ? Why have integer & real types (eg. int & double) – can write 3.0! Remember Characters in Java are Unicode, not ASCII – discuss representations. need to standardize code if we are to communicate. ASCII is 7 bit, actually use 8 bits with additional code page for national characters… UNICODE is 16 bit, 65K characters… but not enough! Currently ~110,000 characters! Note: we introduced type restrictions as a way of avoiding/detecting mistakes in program, but these types are insufficient to handle most cases. Java allows us to define our own types, which we will do later!

12 Characters… Coding Size…? Standard Codes ASCII UNICODE 7 bit,
0100 — ‘+’ 0101 — ‘-’ 0110 — ‘*’ 0111 — ‘/’ 1000 — ‘0’ 1001 — ‘1’ 1010 — ‘2’ 1011 — ‘3’ 1100 — ‘ ’ 1101 — ‘x’ 1110 — ‘y’ 1111 — ‘z’ ASCII 7 bit, English only! UNICODE 16 bit All languages! Sharing information requires all parties use same code… ASCII – American Standard Code for Information Interchange UNICODE – first 128 characters same as ASCII (see unicode.org) ‘A’.. ‘Z’  26 ‘a’.. ‘z’  26 ‘0’.. ‘9’  10 punc.  ?? 62 26 = 64 27 = 128 28 = 256

13 Misc… Why so many numeric types? Error in reals? Typecasting
int into double, but not double into int! Overflow/underflow Division by zero Why not use String for everything? memory, processing time, error, … i = (int) d; also d = (double) i / j; // forces real division What happens if add one to maxint or subtract one from –maxint? (Try it!) Or multiply two large values, or divide small by large…? What happens if divide int’s by 0, doubles by zero? (Try it!)


Download ppt "Java Coding – part 2 David Davenport Computer Eng. Dept.,"

Similar presentations


Ads by Google