Wrap up on computers and data - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Wrap up on computers and data

Description:

Where does the extra stuff come from? Try 31.01 0.01. Other 'Funny Math' Remember what we talked about in regards to encoding decimals. 2n 2n-1 2n-2 ... – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 21
Provided by: BenSc
Category:
Tags: computers | data | funny | stuff | wrap

less

Transcript and Presenter's Notes

Title: Wrap up on computers and data


1
Session 4
  • Wrap up on computers and data
  • Working with the primitives in Dr. Java

2
Announcements
  • Want to do at home what you did in lab yesterday?
  • Your textbook CD contains all of the files you
    need to do this, OR
  • You can also download everything off of the web
  • Java version 1.5.X
  • Dr. Java
  • This weeks topics will begin to look at how to
    manipulate data in a computer.
  • (A start to learning how to program a computer!)

3
Review Primitive Types
byte
short
int
long
boolean
4
Review Primitive Types
byte
short
int
long
boolean
5
Review Binary Addition
  • To add two decimal numbers you add the digits and
    if the total is greater than ten you carry the
    one into the next column
  • To add two binary numbers
  • 0 0 0
  • 0 1 and 1 0 1
  • 1 1 0 with a carry of 1 into the next column
    to the left
  • 00 10 111
  • 01 01 001
  • ---- --- ------
  • 01 11 1000
  • 00111001010
  • 01010101101
  • -------------------
  • 10001110111

6
Review 2s Compliment Notation
  • Computers actually only know how to add
  • So, how do they handle subtraction?
  • Computers subtract by adding a negative number
  • How do you represent a negative number in memory?
  • Positive numbers in 2s compliment are just the
    same as a binary number
  • For negative numbers reverse 0s and 1s and then
    add 1
  • All negative numbers have a one for the leftmost
    bit

7
Decimal Number Storage
  • How do you think a computer stores 3205.406?
  • It uses an IEEE 754 format
  • Stored as binary numbers in scientific notation
    -52.202 is -.52202 x 102
  • For example
  • 6.5 in decimal
  • 110.1 in binary
  • .1101 x 23 in binary with scientific notation
  • Stores 3 things
  • A bit for the sign
  • A number between 0 and 1 (the mantissa)
  • The power of 2 (the exponent 8 bits)

8
Decimal Number Storage
  • The problem with this structure
  • Many numbers actually dont encode well
  • Remember yesterday when I asked you to
  • It uses an IEEE 754 format
  • Stored as binary numbers in scientific notation
    -52.202 is -.52202 x 102
  • For example
  • 6.5 in decimal
  • 110.1 in binary
  • .1101 x 23 in binary with scientific notation
  • Stores 3 things
  • A bit for the sign
  • A number between 0 and 1 (the mantissa)
  • The power of 2 (the exponent 8 bits)

9
ASCII code
  • A simple conversion of the common American
    characters and computer commands to numbers.
  • A 65
  • a 97
  • 1 49
  • \t 11
  • See www.asciitable.com for more information

10
What is DrJava?
  • DrJava is a free integrated development
    environment for doing Java programming
  • From Rice University
  • It is written in Java
  • It has several window panes in it
  • For creating programs (definitions pane)
  • For trying out Java code (interactions pane)
  • Listing of open files (files pane)

11
Number literals in Java
  • By default, Java assumes that anything without a
    decimal is an int.
  • 2
  • 2
  • -5
  • -5
  • By default, Java assumes that anything with a
    decimal is a double.
  • 3.5
  • 3.5
  • -4.0
  • -4.0

12
Number literals in Java
  • Because of this, you have to be careful about
    size
  • 123456789
  • 123456789
  • 12345678901234567890
  • NumberFormatException For input string
    12345.
  • Integer.MAX_VALUE
  • 2147483647
  • Integer.MIN_VALUE
  • -2147483648
  • Integer.MAX_VALUE 1
  • -2147483648

13
Math Operators in Java ( / - )
  • But on their own, number literals are boring.
  • Java provides 5 binary mathematical operators
  • Here, binary means two numbers
  • Addition
  • 3 4
  • Multiplication
  • 3 4
  • Division
  • 3 / 4
  • Subtraction
  • 3 4
  • Modulo (Remainder)
  • 10 2 and 11 2

14
Math Operators Exercise
  • How would I perform each of the following in
    DrJava and what would be the answer?
  • Subtract 7 from 9
  • Add 7 to 3
  • Divide 3 by 2
  • Divide 4.6 by 2
  • Multiply 5 by 10
  • Find the remainder when you divide 10 by 3

15
Why is the result of 3 / 2 1?
  • Java is a strongly typed language
  • Each value has a type associated with it
  • Tells the computer how to interpret the number
  • It is an integer, floating point, letter, etc
  • The compiler determines the type if it isnt
    specified (literals)
  • 3 is an integer
  • 3.0 is a floating point number (has a fractional
    part)
  • The result of an operation is in the same type as
    the operands
  • 3 and 2 are integers so the answer is an integer 1

16
Casting
  • There are other ways to solve the problem of 3 /
    2 has a result of 1
  • You can make one of the values floating point by
    adding .0
  • 3.0 / 2
  • 3 / 2.0
  • The result type will then be floating point
  • Or you can cast one of the values to the
    primitive types float or double
  • (double) 3 / 2
  • 3 / (float) 2

17
Casting Exercise
  • Use casting to get the values right for splitting
    up a bill for 3 people of 19 dollars.

18
Other Funny Math
  • What is the result of
  • 79.95 4 1.07
  • Do it with a calculator and you get 342.186.
  • BUT, the value we got in lab yesterday was
  • 342.18600000000004
  • Where does the extra stuff come from?
  • Try 31.01 0.01

19
Other Funny Math
  • Remember what we talked about in regards to
    encoding decimals
  • 2n 2n-1 2n-2 22 21 20. 2-1 2-2 2-3
    2m-1 2m
  • There are a limited number of places you can
    encode.
  • Any leftovers get ignored which causes problems
    when you de-code

20
Operator Order
  • The default evaluation order is
  • Negation -
  • Multiplication
  • Division /
  • Modulo (remainder)
  • Addition
  • Subtraction -
  • The default order can be changed
  • By using parenthesis
  • (3 4) 2 versus 3 4 2
Write a Comment
User Comments (0)
About PowerShow.com