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

Practical 3 CS2PLC Programming Language Concepts

Assessed: Yes (At start of Practical 4 in week5)

Practical 3

Theme

– Enumerated types, symbolic values, their formatting and parsing

– Boolean expressions and conditionals

– Unicode characters and encodings

Key concepts: enumerated values and types, Boolean expressions, conditionals, guards, simple formatting and parsing, Unicode, UTF-16 encoding

3.1. Start up and essential configuration

a) Open the Practical 3 code using your chosen method.

– If you reuse your previous workspace, pull the most recent code from GitHub, eg in the terminal as follows:

> cd PLC2022

> git commit -a -m "my P2 code"

> git pull

b) Open the associated Blackboard quiz.

3.2. Comparing usage of symbolic values of an enumerated type in Java, Lisp and Haskell

a) Explore three programs that work with symbolic values. The three programs to explore are:

java/ErrorsEnum.java

lisp/errors .lisp

haskell/errorsEnum.hs

Each program does the same when executed: It asks for a kind of error (eg fp_overflow) and then outputs the result of that kind of error (eg INFINITY). P Quiz. Quiz. Complete questions 1–3 related to this task.

b) Modify each of the three programs so that it does the reverse.

After the modifications, each program asks for a result (eg infinity) and outputs a kind of error that leads to this result (eg FP_OVERFLOW).

Important. Keep a copy of the original files and produce a summary of your changes in Mergely and get a unique URL for your diff using the sharebutton. Beware that you get       different URLs for different diffs, not re-using the same instance of Mergely for multiple diffs, overwriting the previous diff.


Important. Make as few changes to the programs as possible. Nevertheless, you should adjust output messages, variable names, and function names so that they correctly reflect the             changes.

Quiz. Complete questions 4–6 related to this task.

3.3. Comparing usage of Booleans in C, Java and Haskell

a) Fix given broken Java expression. After fixing, the expression should be equivalent to the given Haskell sequence of guarded expressions.

The fix should be a very small change, not a rewrite.

The Haskell code is the following:

error2Result err | err == FP_Rounding | err == FP_Overflow | err == FP_Underflow | otherwise

= ABitDifferent

= Infinity

= Zero

= VeryDifferent

(This code can be also found in file haskell/errorsBool.hs .) The

Java expression that you should fix is on lines 39–45 in file

java/ErrorsBool.java.

Quiz. Answer question 7 related to this task.

b) Translating C numerical operators to Java Boolean operators

Consider the following C code:

printf("Input = %d, output = %d.\n", j, (j>0)*j + (j<0)*(-j));

(This code can be also found in file c/boolFunction.c, which can be used to evaluate it for different values of j .)

Some of the numerical operators above include the results of comparisons. Java does not support this.

Your task is to write a Java version of the above C line and insert it on line 36 in file java/BoolFunction.java.

If your translation is correct, the Java program will behave exactly as the C program. Quiz. Answer question 8 related to this task.

Hint . Recall that in C Booleans are modelled as integers, usually 0 and 1.

Comparison operators always return 0 or 1. It may be tempting for C programmers to write   code such 1-(x==y) when they realise that certain arithmetic operators are equivalent to         certain logical functions. Java forces the programmer to do this properly with safer and more intuitive Boolean operators (eg, 1-(x==y) becomes !(x==y)).