Escolar Documentos
Profissional Documentos
Cultura Documentos
Tutorial #2
Question 1
a) Draw a simple diagram of memory (does not have to be exact) to illustrate the following:
String s = "Hi" + " There";
s = s.toUpper();
s = s + " Jamaica";
StringBuffer s4;
StringBuffer s2 = "There";
s4 = s2;
s2.add("fore");
System.out.println(s4);
Show the ID of each string as it is created. Note which strings are garbage collected.
b)
Question 2
Write a program that gets two points (x1,y1) and (x2,y2) from the user (where these are real
numbers) and prints the distance between them, where distance = square root of [ (x2-x1)^2 +
(y2-y1)^2 ]
Invoking static or class methodsthese are methods that are invoked using the class name rather than an
object name. The methods in the Math class are static methods (basically because we don't need different
instances of Math whereas there are lots of different String objects). Examples are
Math.sqrt(2)
and
Math.pow(3, 2)
Importing the appropriate packagesusually when you use classes from a library you need to put the
import declaration at the top of your program. The exception is for classes defined in the java.lang package
(this includes String and Math) which is automatically imported into every Java program.