Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Practical 2 CS2PLC Programming Language Concepts

Assessed: Yes (At start of Practical 3 in week4)

Practical 2

Theme

writing and understanding numeric expressions

experimenting with numeric types in Java, Ada and Lisp

simulating some features of the Ada and Lisp numeric data types in Java

Key Concepts: expression trees, operator precedence, integer overflow, floating point overflow and underflow

1) Start up and essential configuration (for details see Practical 1 or Learning resources on BB)

a) Open Blackboard quiz Practical 1” (in the practical session)

2) Translation expressions between PLs

a)    Correct a Java expression to correspond to a Lisp expression. The Lisp expression is the following:

( +

1 (*

(+

a

b)

(-

a

(/

1

b))

))

which is equivalent to

(+ 1 (* (+ a b) (- a (/ 1 b))))

It can be also found in file lisp/exp1.lisp, which can be used to evaluate it for certain values of a and b. Use the decimal dot even when inputting whole numbers (eg 1.0).

The Java expression that should be (but is not) equivalent to this Lisp expression is on line 65 in file java/Exp1.java.

(Please read README-java on how to compile and run the program in one step.)

When you make a correction to the Java expression, test it for several values of a and b, using the main method in Exp1.java and compare the outcome with the results of evaluating the Lisp expression.

QUIZ. Answer quiz question 1 related to this task.



Practical 2 CS2PLC Programming Language Concepts

Assessed: Yes (At start of Practical 3 in week4)

Note: Lisp determines numeric types at run-time and thus the expression may be   evaluated using fractions as well as floating-point numbers. If you input integers or fractions (eg 1/2), Lisp will use fractions throughout the expression because there are no floating-point numbers inside it. If one or both of the input numbers are        specified as decimal fractions (eg 0.5), then the expression will be evaluated using floating-point numbers.

Such behaviour cannot be achieved in Java easily because the types of method               parameters, return values and variables are fixed at compilation time. The same             applies to C and Ada. Python and Prolog behave similarly to Lisp in this aspect. Haskell is like Java, C, Ada in that the compiler fixes types of variables but these types are inferred automatically and often are generic. In this case the Haskell

3) Explore numeric types in sample programs

a) Observe floating-point rounding and floating-point overflow.

The programs Overflow.java, overflow.adb and overflow.lisp contain functions to compute the power nm for positive integers n and m. (Some compute not only the power nm but also the sequence of lower powers n0, n1, n2, … , nm1, nm).

The power nm  could be a very large number as soon as m grows a bit, easily causing an overflow.

All programs are set up to ask the user for two numbers m and n, and then ask the user to specify one of three different tasks, the first of which is the power nm . The other two functions will be used in later tasks.

Run the three programs repeatedly to compute the floating-point powers 735, 745, 750 and 755 (selecting task  = 1) and compare the results of the three programs.

Also, try running the same program with n being your 9-digit student number.

Important: When comparing the results, ignore formatting differences and                     differences in the number of digits displayed, as long as at least 5 digits are                      displayed. Also, when the program prints the intermediate powers n1, n2, … , nm, focus only on the last number nm . To find the exact value corresponding to your student number you can use high precision floating point calculators available online.

QUIZ. Answer quiz question 2 and 3 related to this task.

b)    Devise an assertion that will detect floating-point overflow and underflow in Java. Method geom_fp computes partial sums of the geometric series:

1 + + + + + ⋯ +

As the powers get bigger and bigger, the terms 1/(nm) become smaller and smaller very fast. Using floating-point arithmetic, the very small numbers are rounded to 0. This occurrence is called an underflow.

Complete the assertions on lines 115 (underflow) and 89 (overflow) to correctly detect these conditions. (Recall that an assertion in Java states a property which should always return true — if the property fails, an exception will be thrown,


eg assert x >= 0 : "x is negative"assert x >= 0 : "x is negative".)

Important: Test that the modified code actually detects overflow correctly. Java   assertions are disabled by default . If you compile and execute the program using   the javac and java commands, you need to execute the program as follows: java -ea Overflow to enable assertions. The supplied script enables assertions.

QUIZ. Answer quiz question 4 and 5 related to this task.

c) Observe large integers in Java, Ada and Lisp.

It is not hard to find n and m for which the power is larger than what an int in Java and My_Num2 in Ada can hold. Lisp uses an unlimited integer type by default and thus can    compute the power for much larger parameters than the Ada and Java versions.

Run the Java, Lisp and Ada versions of the integer power program and try it on small input numbers as well as for computing the powers 73, 76, 79 and 712 .

QUIZ. Answer quiz question 6 and 7 related to this task.

d) (OPTIONAL) Devise an assertion that will detect integer overflow.

Uncomment and complete the assertion on line 66 in Overflow .java so that it correctly detects all integer overflows in this function. Also take care that the assertion does not signal an overflow where there is none.

HINT: This is a fair