Drawing ASTs for expressions: Correct Solution


For each of the Java expressions below:

Be sure that your tree accurately reflects how Java would evaluate the expression.

Part 1

Given the following variables:

int i = 2;
double gi = 2.0;
double ilsi = 1.0;
int oarte = 3;
int urwol = 2;
int si = 4;
int niper = 9;

...draw the AST and evaluation results for the following expression:

i * 3 + gi + ilsi - oarte - urwol - 9 * si - 7 - niper - 4 - 8

Solution

Things to double-check in your solution:


Part 2

Given the following variables:

double errhart = 3.0;
boolean hurmkop = true;
int sa = 8;
boolean ritos = true;
double lecio = 1.0;
int dosmthid = 3;
double notec = 2.0;
int nioss = 7;
boolean sadcun = false;
int ro = 7;
boolean ausk = true;

...draw the AST and evaluation results for the following expression:

2 != errhart != hurmkop == (sa == 1) || ritos || lecio == dosmthid || !(notec == 6) && !(5 == 2) || nioss >= 5 || sadcun != !(8 < ro) && !ausk

Solution

Things to double-check in your solution:


Part 3

Draw the AST and evaluation results for the following expression:

0 + 1 + 2 + 3 + "4" + 5 + "6" + "7" + 8 + 9 + 10 + 11 + 12

Solution

Things to double-check in your solution:


Related puzzles: