1 / 21

FUNDAMENTAL DATA TYPES

FUNDAMENTAL DATA TYPES. CSC 171 LECTURE 4. How do Computers represent information?. Computers are switches Switches are “on” or “off” Suppose we want to represent something We can decide to interpret (semantics) an “on” switch as a “1” and an “off” switch as a “0”.

sunee
Download Presentation

FUNDAMENTAL DATA TYPES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4

  2. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to represent something We can decide to interpret (semantics) an “on” switch as a “1” and an “off” switch as a “0”

  3. How many possibilities? • 1 switch • 2 switches • 3 switches • … • “n” switches

  4. A “code” • Ceasar Cipher • Shift the letters • TED becomes VGF • Something is represented as something else

  5. Coding Letters • How many letters? • How many “switches” (bits)

  6. What is a number? 324 A number is a shorthand for a polynomial 324 “means” (3*100)+(2*10)+(4*1) Or : (3*102)+(2*101)+(4*100) Why do we use 10 numerals? {0,1,2,3,4,5,6,7,8,9}

  7. A possible code • Every letter is a 26 bit sequence • A is “10000000000000000000000000” • B is “01000000000000000000000000” • “TED” is : • 00000000000000000010000000 00001000000000000000000000 00010000000000000000000000

  8. A beter code • Use all possible combinations • TED becomes • 010110010000011 • 01011 00100 00011

  9. What is a number? 324 A number is a shorthand for a polynomial 324 “means” (3*100)+(2*10)+(4*1) Or : (3*102)+(2*101)+(4*100) Why do we use 10 numerals? {0,1,2,3,4,5,6,7,8,9}

  10. Binary Digits (bits) Since computers only have two states they count in base 2 (binary) 11012 11012= (1*8)+(1*4)+(0*2)+(1*1) Or = (1*23)+(1*22)+(0*21)+(1*20) Or = 1310 Note: 8 “bits” is a “byte”

  11. PAIRS EXCERCISE • Convert the following binary numbers into base 10 • 10101 • 00111 • 11010 • 01010 abcde = (a*24)+(b*23)+(c*22)+(d*21)+(e*20)

  12. And, of course • Convert the following base 10 numbers into binary • 7, 12, 5, 19 • abcde=(a*24)+(b*23)+(c*22)+(d*21)+(e*20)

  13. NUMBER TYPES IN JAVA • int : integers, no fractional part • 1 • -4 • 0 • double: floating point numbers • 0.5 • 0.0 • -3.1111

  14. Operations depend on type • “/” is the division operator • 7.0 / 4.0 yields 1.75 • 7 / 4 yields 1 • Note the “remainder” operator is useful for integers • 7 % 4 yields 3

  15. “PRIMITIVE” types in JAVA

  16. Assignment • public void addNickels(int count) {      nickels = nickels + count;   }

  17. Need to draw this

  18. Increment and decrement nickles++ is shorthand for nickles = nickles + 1; or Nickles += 1;

  19. Some shorthand options int count; count = 0; count++; count--; ++count; --count;

  20. Constant variables • Sort of like “jumbo shrimp” or “freezer burn”? • “Magic numbers” are bad style (to the blackboard, robin!)

  21. Static finals • Enable constants Math.PI • Enable functions Math.sqrt(x)