This lab introduces you to Java by having you convert function definitions written in Cinnameg into Java. The lecture notes describe differences between the two languages. The following table is just a summary.
Cinnameg | Java | Remark |
---|---|---|
Integer Real Boolean String |
int double boolean String |
Types have different names. Details. |
x =/= y x `div` y x `mod` y a and b a or b not(a) |
x != y x / y x % y a && b a || b !(a) |
Most operators are the same in Cinnameg as in Java. Only the ones that are different are shown here. Details. |
Let x: Integer = 2. | int x = 2; | Write the type of the variable in front of the variable. End the statement with a semicolon instead of with a period. Details. |
Relet x = 2. | x = 2; | To change an existing variable, just omit the type. Details. |
Define f(x, y) = | public static int f(int x, double y) | Function headings look different. After public static write (1) the type of the answer, (2) the name of the function, and (3) the argument(3), with the type of each argument in front of the argument's name. In the example shown, f is a function that takes an integer x and a real number y. Details. |
Define f(y) = 3*y + 1 %Define |
public static int f(int y) { return 3*y + 1; } |
The body of a function must have braces around it. Use the word return to indicate what the answer is. Details. |
Define g(y) = r | Let a: Real = 2*y. Let r: Real = a*a. %Define |
public static double g(double y) { double a = 2*y; double r = a*a; return r; } |
Again, to give the answer, just use return. Details. |
Let n: Integer = 0. If w < 0 then Relet n = 1. else Relet n = w + 1. %If |
int n = 0; if(w < 0) { n = 1; } else { n = w + 1; } |
Special words like if use all lower case letters in Java.
The parentheses around the condition being tested are required.
If you do not want to do anything when the condition is false, just leave out else and what follows it. Details. |
Define f(x: Integer): Integer by case f(x) = 1 when x == 0 case f(x) = 2*f(x-1) - 1 %Define |
public static int f(int x) { if(x == 0) { return 1; } else { return 2*f(x-1) - 1; } } |
Use if-statements to handle multiple cases. Details. |
Let n = 0. Let r = 1. While n < 10 do Relet r = 2*r. Relet n = n + 1. %While |
int n = 0; int r = 1; while(n < 10) { r = 2*r; n = n + 1; } |
The parentheses around the condition are required. Details. |
[solve] Write a Java function definition that is equivalent to the following Cinnameg function definition.
Define grape(x: Integer): Integer by grape(x) = 2*x*x + 3*x - 5 %Define
[solve] Write a Java function definition that is equivalent to the following Cinnameg function definition.
Define sumDigits(n: Integer): Integer by case sumDigits(n) = 0 when n == 0 case sumDigits(n) = n `mod` 10 + sumDigits(n `div` 10) %Define
[solve] Write a Java function definition that is equivalent to the following Cinnameg function definition.
Define largest(x: Real, y: Real, z: Real): Real by largest(x,y,z) = m | Let m: Real = x. If m < y then Relet m = y. %If If m < z then Relet m = z. %If %Define
[solve] Write a Java function definition that is equivalent to the following Cinnameg function definition.
Define q(n: Integer): Integer by q(n) = count | Let count: Integer = 1. Let k: Integer = n. While k =/= 1 do If k `mod` 2 == 0 then Relet k = k `div` 2. else Relet k = 3 * k + 1. %If Relet count = count + 1. %While %Define
[solve] This exercise is a no brainer. Just copy the following Java program fragment into the box and run it to see what it does. (It starts at 2,000,000,000 and counts up until it hits a negative number. Then it prints that negative number and the number just before it.
int count = 2000000000; while(count >= 0) { count = count + 1; } System.out.println((count - 1) + " + 1 = " + count);