Sei sulla pagina 1di 2

COMP1161 Object-Oriented Programming

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)

What is the difference between a String and a StringBuffer?


Declaring a variable to be a reference to an objectfor example, the following declares the variable
quotation to be a reference to a String object:
String quotation;
Declaring a variable to be a reference to an object and creating the object (instantiating it) using the new
operatorfor example,
String quotation = new String("I think, therefore I am.");
Random generator = new Random();
Invoking a method to operate on an object using the dot operatorfor example,
quotation.length()
invokes the length method which returns the length of the quotation String or
quotation.toLowerCase()
returns a new string that is the same as quotation except all letters are lower case. These invocations would
be used in a program in a place appropriate for an integer (in the first case) or a String (in the second case)
such as an assignment statement or a println statement.
quotation.indexOf('I ')
returns the position of the first I in the string, which is 0 as an integer
int posb1 = quotation.indexOf(' ')
assigns posb1 the position of the first blank character, which is index 1
int posb2 = quotation.indexOf(' ', posb1+1)
returns the position of the first blank after index 1, which is ____
int posb3 = quotation.indexOf(' ', posb2+1)
returns the position of the next blank after the position of the second blank, which is ____
quotation.indexOf("think")
returns the starting position of the first occurrence of the string "think", which is ____.
quotation.substring(2,6)
returns a string starting at index 2 and ending at 6-1, which is the string ______.
How would we get it to return the word "there"?
What does the following return?
quotation.substring(0,0)
What does the following return?
quotation.substring(posb2, posb3)

How would we get it to return the word without the blanks?


quotation.replace(' ', ':')
returns a new string with all occurrences of blank replaced by ':'

show how the quotation string is stored


show how this string is stored: "I think\tI
Show output of
quotation.substring(0,1)
quotation.substring(3,3)
quotation.substring(3,4)

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)

(which returns the square root of 2)


(which returns 32)

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.

Potrebbero piacerti anche