Spring 2001 CS100M Exercise E13: Object Lesson Due: hard copy handed in at the *start* of lecture, Tuesday, April 24. no late assignments will be accepted. =============================================================================== goals =============================================================================== this exercise is to make sure you start to learn how to trace Java programs: + $public$ = everyone is allowed to see the variable or method + $private$ = only code within the same class is allowed to see the variable or method + $static$ = exactly one copy, inside the class + non-static = exactly one copy per object inside the class + $new CLASS()$ = create a new object inside class $CLASS$ and return a reference to it + $COND ? YES : NO$ = if $COND$ is true, then value $YES$, else value $NO$ + value = a scalar (primitive) type or a reference to an object + copying a reference does *not* copy the object it points to + in java, there is never a reference pointing to a variable + earlier rules still apply, e.g. + draw activation records in the same scope as where you found the method + if you don't see an identifier in the current scope, go to the next enclosing scope note: the 4/24 lecture will go over the solutions to this quiz. =============================================================================== instructions =============================================================================== trace the program below (E13 is the main class) using box scope diagrams: + if a line of code is illegal, pretend it does not exist + record output + run the code to double-check your answer (comment out illegal lines of code) -- you should also make changes to the code and make note of the results to see if you understand them or are confused by them submission + turn in a printout of the output from running the program + turn in your hand tracing, but keep a copy for reference during lecture + you must turn in hard copy at the start of lecture -- no late assignments will be accepted =============================================================================== code =============================================================================== /* complex numbers $a + i * b$ */ public class Complex { private double a, b; /* $a$ = real part, $b$ = imaginary part */ /* create complex number $real + i * imaginary$ */ public static Complex makeComplex(double real, double imaginary) { Complex c = new Complex(); c.a = real; c.b = imaginary; System.out.println(str(a) + "i" + str(b)); return c; } /* sum of self with $c$ */ public Complex add(Complex c) { return makeComplex(a + c.a, b + c.b); } /* product of self with $c$ */ public Complex mul(Complex c) { return makeComplex(a * c.a - b * c.b, a * c.b + b * c.a); } /* description of self */ public String toString() { String s = b == 1 ? "i" : b == -1 ? "-i" : b < 0 ? "-" + str(-b) + "i" : "+" + str(b) + "i"; return b == 0 ? str(a) + s : s; } /* string representation of $n$ */ private String str(double n) { int i = (int) n; return i == n ? "" + i : "" + n; } } public class E13 { public Complex a = Complex.makeComplex(1.0, 2.0); public static Complex b = Complex.makeComplex(1.0, -2.0); public static void main(String[] args) { b.b++; Complex c = b.makeComplex(0.0, 1.0); Complex d = c; c = b; b = d; d = c.mul(b); E13 e = new E13(); System.out.println(b.add(c).toString()); System.out.println(a.toString()); System.out.println(e.a.toString()); System.out.println(b.toString()); System.out.println(e.b.toString()); System.out.println(c.toString()); System.out.println(d.toString()); } }