First project - PowerPoint PPT Presentation

About This Presentation
Title:

First project

Description:

First project Create a JApplet with 3 textfields (2 for input and one for the answer), appropriate labels, and buttons for plus, minus and times. – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 18
Provided by: higg8
Category:
Tags: first | funny | project | slides

less

Transcript and Presenter's Notes

Title: First project


1
First project
  • Create a JApplet with 3 textfields (2 for input
    and one for the answer), appropriate labels, and
    buttons for plus, minus and times.
  • Your JApplet will perform binary int
    computations , - and
  • For negative answers, do not show the twos
    complement value, instead, negate it and show the
    answer in the appropriate textfield with a minus
    sign in front.
  • See slides which follow regarding twos
    complement representation.

2
Signed binary ints
  • There are many possible signed int
    representations. We discuss 3 here, including
    the one your computer uses.
  • Signed ints are positive or negative values. So
    the representation must include information on
    the sign of the number. In all 3 representations
    we discuss, the high order bit is reserved to
    indicated the sign 0positive, 1negative.
    Notice, we have used one bit for the sign, so the
    magnitude of the value we can represent in a
    given number of bits is decreased by a power of
    2.
  • Remember the bit on the left is the msb (most
    significant bit) and the bit on the right is the
    lsb (least significant bit), whatever wordsize
    you are using.
  • 10011000 is 8 bits, a byte. Which is the msb?
    Which bit is the lsb?

3
Sign-magnitude
  • A simple way to represent signed ints might be to
    use the msb for the sign and the rest of the bits
    for the magnitude.
  • 10001010 is 8 bits, with msb1, so the number is
    negative. The value or magnitude is 10, so this
    is 10.
  • 11111111 and 01111111 are respectively the
    smallest and largest possible values in 8 bits.
    What values (in decimal) are they?
  • Why is arithmetic difficult to do in the
    sign-magnitude representation?
  • Because of these problems this representation is
    not used in modern digital computers.

4
Ones complement
  • This representation has some things in common
    with sign-magnitude All the positive numbers
    look the same. Again, if the high order bit is
    a 0 then the number is positive, and the rest of
    the bits represent the value.
  • 00101100 is a positive value in 8-bit ones
    complement, and would be the same value in
    sign-magnitude. What value?
  • 11010011 is the negative representation for the
    same value. Negation is performed by flipping
    bits.
  • Arithmetic works somewhat in ones complement
    with some limitations and a curious problem. Can
    you find the problem?

5
Twos Complement
  • Your computer uses twos complement to represent
    signed int values.
  • Positive values look the same as in
    sign-magnitude and ones complement.
  • The process of negation (negating positive values
    to get negative, or negating negative values to
    get positive) is performed in two steps,
    correcting the funny arithmetic quirk of ones
    complement arithmetic.
  • Negate a value by flipping bits, then adding a 1.
  • 01101101 represents what positive value?
  • 10010010 is the flip-bits, and 10010011 is the
    negation of that value.
  • 01101101
  • 10010011
  • _________
  • 00000000 

6
Twos Complement
  • Arithmetic in twos complement works, as long as
    you dont add two positives or two negatives
    together which yield a sum too big for the
    wordsize. This would cause a carry into the sign
    bit, making a positive sum negative, for example.
  • Practice adding some positive and negatives
    together.
  • An extra value. In an 8 bit representation who
    is 10000000? It looks like it should be negative,
    what happens when you flip the bits and add 1?
  • In twos complement, there is always one more
    negative value than positive, and in 8 bits, the
    range is 128127 and in 16 bits
  • -3276832767

7
Adding two binary values
  • Adding numbers in any base works the same way.
    If x and y and sum are arrays of size SIZE
    filled with values base b then the following
    algorithm computes the sum of the values in x,
    and y. This algorithm assumes x and y are padded
    on the left with 0s.
  • int tot,carry 0
  • for(int k0kltSIZEk)
  • totxkykcarry
  • sumktotb//b is the base
  • carrytot/b

8
More remarks on your first project
  • You will get input as two Strings.
  • You need to get the binary information out of
    these and put it into int arrays, remembering to
    pad the extra space in the array with zeros.
  • An array is declared in Java like this
  • int xnew int20//or whatever size you want
  • You can declare it and allocate it later so you
    could make the global declaration
  • int x
  • And in another method, init() or
    actionPerformed() allocate the array
  • xnew int20
  • A complication. String subscripting starts on
    the left with 0 and goes up to StringName.length()
    -1. You will need to flip over or reverse the
    order of the bits in the Strings as you copy them
    into your arrays. Array subscripts go from 0 to
    arraylength-1

9
Methods for your project
  • Write the following methods
  • Convert takes a String and returns the int array
    the way you want it
  • Sumup adds two binary numbers (arrays) and
    returns the array sum. Do not call this method
    add, there already is an important Component
    method in Java named add.
  • Flipbits takes an int array parameter and
    returns the flipped bits version of the array
  • Shift Shifts the contents of a binary array by
    one and returns this new array (the old one2)

10
About java methods (functions)
  • Java does not support reference parameters. All
    parameters are value parameters.
  • Java allows a function return type to be int,
    float, double, int, or any classtype or
    datatype.
  • Consider then the syntax for method operation in
    you first project.
  • Suppose you have two int arrays, x and y. To make
    y be the flipbits of x, assuming you have a
    method called flipBits
  • yflipBits(x)
  • Somewhere in this method must appear the
    statement return with an array argument.
  • Discuss how to do addition, subtraction and
    multiplication with the methods suggested on a
    previous slide.

11
Fractions
  • A fractional value f, is one where 0ltflt1. If
    flt1 in one base, then obviously it is less than
    one in every base. The decimal fraction .5 can
    be written in binary as .1
  • Note that to the right of the decimal point, a
    digit d in position p in base b represents dbp,
    where the first position to the right of the
    point is 1. In binary, .1 represents 12-1
    1/2
  • It is possible to convert fractions to/from base
    b to decimal.

12
Converting Fractions Decimal to Another Base
  • Consider converting the base 10 fraction .6842 to
    base two. Recall the binary fraction .1 1/2
    1/8 in binary is .001 and is equal to the decimal
    fraction .125
  • .6842 .5 .125 .0592 .101 (2) ?
  • We could continue in this fashion guessing at the
    answer and whittling away but there is a much
    simpler way.
  • .6842 (10) .b1b2b3b4 (2)
  • Clearly we need values for each bit b in the
    answer. If we multiply both sides of the above
    equation by 2, on the right side the decimal
    point shifts right 1 position
  • 2.6842 (10) b1 . b2b3b4 (2). 2.6842
    1.3684 so b1 1.
  • Multiply .3684 by 2 to get 0.7368 so b20.
    Continue
  • 2.7368 1.4736 so b3 1. The next three
    iterations would give b4 0, b5 1 and b6 1 so
    our answer so far is .6842 (10) .101011 (2)

13
Converting Fractions Decimal to Another Base
  • .6842 (10) .101011 (2)
  • What is the error in this approximation?
  • How many binary bits should we give in an
    accurate answer?
  • Can you devise a method to convert fractions in
    base 10 to any other base?
  • Conversion decimal to binary can be done by
    repeatedly multiplying the decimal fraction by 2,
    peeling off the bit that appears each time to
    the left of the decimal point and making it the
    next bit of the answer, and continuing until you
    have enough bits.

14
Example
  • .4298 what in binary?
  • .42982 0.9596 so the first bit of the answer
    is a 0.
  • .95962 1.9182 so the second bit is a 1
  • .91822 1.8364 so the third bit is a 1
  • .83642 1.6728 so the next bit is also a 1.
  • You can see that the next bit will be a 1 and the
    bit after a 0.
  • So part of the binary fraction is .011110.
  • How many bits should we try to get to achieve the
    same precision that 4 decimal digits has? Is
    there a formal mathematical way of saying it?

15
Converting Fractions Decimal to Another Base
  • Assuming a Java interface with TextFields named
    input (a base 10 fraction), baseval (the new
    base) and output (where the answer should appear)
    here is a java code fragment to do fraction
    conversions
  • String answer.//start answer with decimal
    point
  • double fraction Double.parseDouble(input.getText(
    ))
  • int base Integer.parseInt(baseval.getText())
  • for (int j0jlt????j)//still need to figure
    out how many digits
  • int temp (int) fractionbase
  • int digit templt1?0temp
  • fraction fractionbase-digit
  • answerdigit
  • output.setText(answer)

16
A java method that manipulates arrays
  • Although this example is not exactly useful for
    your program, it does show how to write a method
    that manipulates returns an array argument.
    Suppose an int array values contains a bunch of
    decimal values and we are interested only in
    knowing whether each value is even or odd. I
    might want a boolean array of the same size
    called parity. My plan might be to put true in a
    position of parity if that position of values
    contains an even int and false if it is odd.

17
Code comments
  • //maybe in the global area appear the
    declarations
  • int values
  • boolean parity
  • //maybe in init or somewhere else the arrays are
    allocated
  • valuesnew int40
  • parity new boolean40
  • //we assume values has been filled, then
    somewhere or other is the function call
  • paritygetParity(values)
  • //somewhere appears the function definition
  • boolean getParity(int x)
  • boolean tempnew booleanx.length
  • for(int k0kltx.lengthk)
  • tempkxk20
  • return temp
Write a Comment
User Comments (0)
About PowerShow.com