Tute Java PL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Tutorial 2: Java Programming Language

1 Description
In this tutorial, you will practise with the Java programming language syntax through a series of
exercises.

2 Tasks
1. Use the syntax tree to explain the grammar of the following Java code. Be as specific as you can
about the name of the nonterminal (LHS) that matches each code element.

private static float min(float a, float b) {


return a <= b ? a : b;
}

2. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

static float min(int[] a) {


// TODO: implements this
}

3. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

static int nextCount(int i) {


int j;
{ j = i + 1;
}
return j;
}

4. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

private static float min(float a, float b) {


float min;
min = a <= b ? a : b;
return min;
}

PR2 Page 1 2019


5. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

private static float min(float a, float b) {


float min;
if (a <= b) {
min = a;
} else {
min = b;
}

return min;
}

6. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

static int search(int[] a, int x) {


for (int i = 0; i < a.length; i++) {
if (x == a[i+0]) return i;
}
return -1;
}

7. Use the grammar syntax tree to explain the grammar of the following Java code. Be as specific
as you can about the name of the nonterminal (LHS) that matches each code element.

public static void main(String[] args) {


int[] a = {1, 1, 2, 3, 5, 8, 13};
int x = TextIO.getInt();
int isAt = search(a, x);
System.out.printf("%d is in %s at %d", x, a, isAt);
}

8. Are the following productions correct? Explain why or why not.


If yes, what do you think is a benefit of this definition?
If no, how do you fix them?

MethodDeclaration:
MethodModifiers MethodHeader MethodBody

MethodModifiers:
{MethodModifier}

PR2 Page 2 2019


9. Use the BinaryIntegerLiteral production of IntegerLiteral (§3.10.1) to determine if each of the
following literals are correct. Briefly state why or why not.

0B001100010
0b001_110_001L
1b0_0_1L
0B012L
1B0L
0B0 0 1L

3 Submission
Submit your work to the home work submission box of this tutorial on the FIT portal. Name the file as
follows: student-id_class_wkno.zip, where student-id is your student id, class is the code of the class
that you attend, and wkno is the week number.

PR2 Page 3 2019

You might also like