Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Assignment 1 due this Friday, 7pm.

Similar presentations


Presentation on theme: "CISC124 Assignment 1 due this Friday, 7pm."— Presentation transcript:

1 CISC124 Assignment 1 due this Friday, 7pm.
Fall 2018 CISC124 2/21/2019 CISC124 Assignment 1 due this Friday, 7pm. QWIC Tutorial Tonight at 8pm in Mac-Corry D201. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Building modular code at the method level.
Start Numeric Representation. Fall 2018 CISC124 - Prof. McLeod

3 Building Modular Code You should already know the advantages to modularity: Easier to build. Easier to test. Easier to debug. Easier to modify. Easier to share. Building objects just takes modular design to the next level. But for now, let’s focus on methods (or functions). Fall 2018 CISC124 - Prof. McLeod

4 Designing Methods Methods are written to avoid repeating code. So, make sure you only have one method to do the “one thing”. Methods should be short: How short is “short”? One to ten lines? If you can satisfy all the other rules and the code still explains itself, then the method is short enough. Fall 2018 CISC124 - Prof. McLeod

5 Designing Methods, Cont.
Methods should only do one thing and do it well. Yah, but how can we define “one thing”? Write the most abstract description of what the method does – does it still have the word “and” in it? Look for loosely coupled sections within a method if it seems too large – these are natural dividing points. Sometimes the coder puts an empty line between sections as an unconscious admission of loose coupling. Fall 2018 CISC124 - Prof. McLeod

6 Designing Methods, Cont.
Keep all code within the method at the same level of abstraction. If you find yourself writing detailed code right beside high level code, you are not following this rule. Your program should be readable as a top-down narrative. The most abstract methods will be at the top of the program, leading to the least abstract methods further down. (BTW, I’m bad and I don’t always follow this rule.) Fall 2018 CISC124 - Prof. McLeod

7 Designing Methods, Cont.
Use less than three parameters wherever possible – the best number is zero! Try to use parameters for input only. Flag parameters are ugly – they are saying that the method does at least two things. If needed, multiple parameters can be grouped into an object or list, provided they share a theme. For example: drawCircle(Point centrePoint, int radius) is better than drawCircle(int centreX, int centreY, int radius) Fall 2018 CISC124 - Prof. McLeod

8 Designing Methods, Cont.
Check to see how readable the method name is when it has its parameter list – does it read like a sentence? (verb then noun?). Fall 2018 CISC124 - Prof. McLeod

9 Designing Methods, Cont.
The method should not spawn any side effects. Such as changing the contents of variables passed by reference. Or spawning off another process that is at the same level of abstraction as the method itself. The method should only invoke methods that are at a lower level of abstraction than itself. A method should either do something or answer something, not both. Fall 2018 CISC124 - Prof. McLeod

10 How to Write Good Methods
It is not easy to do, especially if you are used to writing much larger methods. Be prepared to write and then re-write your code several times. Each time, you will probably be breaking larger methods into smaller ones. Each time you re-write (or refactor) the code it gets better (tidier, easier to read, and often shorter!). Will a multiple method program be faster than a single method version that does the same thing? Fall 2018 CISC124 - Prof. McLeod

11 Assignment 1 Lectures have covered everything you need to know for the assignment. Start the assignment before your lab this week… Fall 2018 CISC124 - Prof. McLeod

12 Numeric Representation - Topics
Base 10 numbers in binary and hexadecimal. Two’s complement and IEEE754, for the storage of numbers in RAM. Source and effects of roundoff error. Convergence and increasing the accuracy of summations. Fall 2018 CISC124 - Prof. McLeod

13 Numeric Representation
In base 2 (digits either 0 or 1): r=2, a binary number: ( )2= 1×25+1×24+0×23+1×22+0×21+1×20 +1×2-1 +1×2-2 =53.75 (in base 10) “r” is the “radix” or the base of the number Fall 2018 CISC124 - Prof. McLeod

14 Numeric Representation - Cont.
Hexadecimal Numbers: a base-16 system with 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F: For example: r = 16, a hex number: (B65F)16 = 11×163+6×162+5×161+15×160 = Fall 2018 CISC124 - Prof. McLeod

15 Numeric Representation - Cont.
The above series show how you can convert from binary or hex to decimal. How to convert from decimal to one of the other bases?: integral part: divide by r and keep the remainder. decimal part: multiply by r and keep the carry “r” is the base - either 2 or 16 Fall 2018 CISC124 - Prof. McLeod

16 Numeric Representation - Cont.
For example, convert to binary: So, is Divisor(r) Dividend Remainder (quotient) least significant digit most significant digit Fall 2018 CISC124 - Prof. McLeod

17 Numeric Representation - Cont.
For the “0.4510” part: So, is is: ( )2 Multiplier(r) Multiplicand Carry (product) ... Fall 2018 CISC124 - Prof. McLeod

18 Aside - Roundoff Error Consider 0.1, for example:
(0.1)10 = ( …)2 What happens to the part of a real number that cannot be stored? It is lost - the number is either truncated or rounded. The “lost part” is called the Roundoff Error. Fall 2018 CISC124 - Prof. McLeod

19 Numeric Representation - Cont.
Converting between binary and hex is much easier - done by “grouping” the numbers: For example: (2C6B.F06)16 = (?)2 ( C B F )16 ( )2 Fall 2018 CISC124 - Prof. McLeod

20 Numeric Representation - Cont.
And going from binary to hex: ( )2 = (?)16 ( )2 ( D D )16 Fall 2018 CISC124 - Prof. McLeod

21 Storage of Integers An “un-signed” 8 digit (one byte) binary number can range from to is 0 in base 10. is 1x20 + 1x21 + 1x22 + … + 1x27 = 255, base 10. Fall 2018 CISC124 - Prof. McLeod

22 Storage of Integers - Cont.
So, how can a negative binary number be stored? One way is to use the Two’s Complement system of storage: Make the most significant bit a negative number: So, the lowest “signed” binary 8 digit number is now: , which is -1x27, or -128 base 10. Fall 2018 CISC124 - Prof. McLeod

23 Storage of Integers - Cont.
Two’s Complement System for 1 byte: binary base 10 -128 -127 -1 1 127 Fall 2018 CISC124 - Prof. McLeod

24 Storage of Integers - Cont.
For example, the binary number is 1x20 + 1x22 + 1x24 - 1x27 = = -107 base 10 Now you can see how the primitive integer type, byte, ranges from -128 to 127. Fall 2018 CISC124 - Prof. McLeod

25 Storage of Integers - Cont.
Suppose we wish to add 1 to the largest byte value: This would be equivalent to adding 1 to 127 in base 10 - the result would normally be 128. In base 2, using two’s compliment, the result of the addition is , which is -128 in base 10! So integer numbers wrap around, in the case of overflow - no warning is given in Java! Fall 2018 CISC124 - Prof. McLeod

26 Storage of Integers - Cont.
An int is stored in 4 bytes using “two’s complement”. An int ranges from: to or to in base 10 Fall 2018 CISC124 - Prof. McLeod

27 Storage of Integers - Example
Example – calculate n! as an int and as a long. What happens when overflow occurs? How do you calculate a large factorial? How about the BigInteger class! See FactorialDemo.java Fall 2018 CISC124 - Prof. McLeod

28 Storage of Real Numbers
The system used to store real numbers in Java complies with the IEEE standard number 754. Like an int, a float is stored in 4 bytes or 32 bits. These bits consist of 23 bits for the mantissa, 8 bits for the exponent, with one sign bit: exponent mantissa sign bit Fall 2018 CISC124 - Prof. McLeod

29 Storage of Real Numbers - Cont.
The sign bit, s, can be 0 for positive and 1 for negative. The exponent, e, is unsigned. The standard says the exponent is biased by 127 and that the values 0 and 255 are reserved. So the exponent can range from -126 to E is the unbiased value and e is the biased value (E = e - 127). If e is not a reserved value then assume that there is a 1 to the left of the decimal point, which is followed by the mantissa, f to yield the “significand”. The mantissa is always less than 1 this way. Fall 2018 CISC124 - Prof. McLeod

30 Storage of Real Numbers - Cont.
So a value is stored as: value = (-1)s  1.f  2E For example if s = 0, e = 125 and f = E = 125 – 127 = -2 The significand is value = 1   2-2 Fall 2018 CISC124 - Prof. McLeod

31 Storage of Real Numbers - Cont.
Or, value = 0.011, which is The maximum float would be for : s = 0 e = 254 f = We can use a calculator like: Fall 2018 CISC124 - Prof. McLeod

32 Storage of Real Numbers - Cont.
Gives: E38, which is the constant: Float.MAX_VALUE. Changing s to 1 gives E38 The smallest, non-zero normalized value has e = 1 and f = , which gives E-38, which is Float.MIN_NORMAL. Fall 2018 CISC124 - Prof. McLeod

33 Storage of Real Numbers - Cont.
These are all “normalized” numbers because we are not using the reserved exponent values, 0 and 255. A “denormalized” (or “subnormal”) number has e = 0. This implies a 0 to the left of the decimal point. So: value = (-1)s  0.f  2-126 Fall 2018 CISC124 - Prof. McLeod

34 Storage of Real Numbers - Cont.
This allows the range of float values to be extended to slightly lower values (to 1.4  , Float.MIN_VALUE). If e and f are 0, then you get -0 or +0 depending on s. If e is 255 and f is 0 then you get –Infinity or +Infinity depending on s. (Displayed to the console as –inf or inf. If e is 255 and f is not 0, then you get NaN. Shown as nan. Fall 2018 CISC124 - Prof. McLeod

35 Aside – Why Denormalized Numbers?
Without these small numbers, if you made a float any smaller than 1.2E-38, you would “flush to zero”. The smaller increments of denormalized numbers allows a more “gradual” underflow. So “they” say… Fall 2018 CISC124 - Prof. McLeod


Download ppt "CISC124 Assignment 1 due this Friday, 7pm."

Similar presentations


Ads by Google