Anda di halaman 1dari 7
U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG School of Computer Science Java Tutorial

UNIVERSITY OF THE WITWATERSRAND, JOHANNESBURG

School of Computer Science

Java Tutorial 3

COMS2003: Application and Analysis of Algorithms II

1. Use of references

2012

Explain what happens in the following code:

String

s

=

"apple";

String

t

=

"banana";

String

u,

v;

u

=

"green";

 

v

=

new

String("red");

t

=

u

+

v;

t

=

new

String("pineapple");

u

=

new

String("Tuna

sandwich");

Show how this code executes, explaining how and when memory is allocated.

After the execution of this code, there is some memory that has been allocated that is no longer accessible. Explain. What happens to this memory.

2. Scope – the first time

Look at this code below:

public

class

see

{

static

int

i,j,x=3;

static

int

moggle

(int

x)

{

int

=

=

i

m

m;

x-5;

2*x+3*i;

i,

1

};

j =

return

7;

static

int

m-i;

doggle

(int

x)

{

int

m;

i

=

x-5;

m

=

2*x+3*i;

int

i;

i =

x

-

10;

j =

i

-

7;

return

m-i;

};

static

int

k;

public

static

void

main(String

args[])

{

int

i,

n;

int

l;

i

=

3;

j

=

5;

k

=

moggle(5);

l

=

doggle(8);

System.out.println("Values:

i

"

+

 

i

+

";

j:

"

+

j

+

";

k:"

+

k

+

";

l="

+l);

}

}

Identify each variable in the program and its scope.

For each use of a variable, identify which declaration is in scope.

What is the output of the program?

3. Parameter passing and methods

(a) Passing by value

int

comp(int

 

a,

b,

c)

{

int

ans;

 

ans

=

a>5

?

b

:

c;

if

(ans

+

a

<

10)

 
 

{a

=

a

+

b;

}

 

return

c+b;

 

}

2

int

x,

y;

x

=

1;

y

=

10;

y

=

comp(x,

y,

10);

What is the value of x and y when this code fragment completes?

(b) References and passing parameters

public

class

Example

{

int

t;

double

m;

static

void

change1(int

m)

{

 

float

t;

m

=

m

*

10;

}

static

void

change2(int

m

[])

{

 

m[1]

=

m[1]*10;

 

}

static

void

main

{

 
 

int

p

[]

=

{1,1,1,1,1};

 

change1(p[0]);

change2(p);

}

}

What are the values of p[0] and p[1]?

Why is there a difference between the values of p[0] and p[1]?

4. Classes and objects

(a)

What is a class? What is an object? What is the relationship?

(b)

What are the instance variables and methods of a class?

(c)

What is the effect of declaring instance variables and methods public or pri- vate?

3

(d)

Why do we often declare the instance variables of classes private?

(e)

Could we declare methods private? Would we want to do so?

(f) What does the identifier this mean? Give an example of its use

(g)

What is a constructor?

(h)

What is inheritance? Why is it useful?

5. Inheritance example Trace through the execution of the following program and say what its output is.

class

Animal

{

String

name;

String

identifier;

 

int

date_of_birth;

public

Animal

()

{};

public

Animal(String

n,

String

id,

int

year)

{

name

=

n;

identifier

=

id;

date_of_birth

=

year;

}

void

eats()

{

System.out.println(name

+

"

diet

unknown");

 

}

void

dob()

{

System.out.println("**ANIMAL**

"+name+"

born

in

"+date_of_birth);

}

String

ID

()

{

 

return

(name

+

":

("

+

identifier

+")");

}

}

class

Penguin

extends

Animal

{

String

kind;

public

Penguin

(String

n,

String

k,

int

year)

{

super(n,

"Penguin",

year);

4

 

kind

=

k;

}

void

eats()

{

System.out.println(name

+

"

eats

fish");

 

}

String

kind_of_penguin()

{

 

return

kind;

 

}

void

dob()

{

System.out.println

 

(kind

+

"

penguin

"+name+"

born

in

"+date_of_birth);

}

}

public

class

AnimalTest

{

public

static

void

main

(String

[]

args)

{

Animal

a,

b;

Penguin

p;

//

Look

at

use

of

constructors

 

a

=

new

Animal("Fred",

"Giraffe",

1992);

p

=

new

Penguin("Freda",

"King",

1997);

b

=

p;

//

can

invoke

method

normally

 

a.eats();

p.eats();

//can

System.out.println(p.ID());

use

inheritance

//

Note

DYNAMIC

binding

p.dob();

b.dob();

//

b

is

an

Animal

}

}

6. More questions Consider the example above. Suppose we had the following code:

5

Animal

a,

b;

Penguin

p;

//

Look

at

use

of

constructors

 

a

=

new

Animal("Fred",

"Giraffe",

1992);

p

=

new

Penguin("Freda",

"King",

1997);

b

=

p;

System.out.println("(p)

Penguin

is

"+p.kind_of_penguin());

System.out.println("(b)

Penguin

is

"+b.kind_of_penguin());

System.out.println("(a)

Animal

is

"+a.kind_of_penguin());

The last two lines will result in compile errors. Explain why. In particular, why is

System.out.println("(b)

Penguin

is

not allowed, when b refers to a penguin?

7. Complexity Classes

"+b.kind_of_penguin());

(a)

Suppose we know that the best case complexity of an algorithm is O(n 2 ). Is it possible for the algorithm to take O(n 3 ) for particular input sets? Is it possible for it to take O(n) time?

(b)

The complexity of a particular algorithm has been shown to be f (n) = 4n 2 + 3n 2. Prove that f (n) O(n 2 ).

(c)

There are two competing search algorithms. The cost of algorithm 1 is f (n) = 4n( 2 ) + 4n + 1 and the cost of the other is g(n) = 45n + 315. Which of these algorithms would we pick if we knew that we would only be working with very large data sets? Which one would we use on very small data sets?

(d)

f (n) = 15n + 21. Prove that f (n) O(n 2 ). Prove that f (n) Ω(n).

(e)

Which of the following sets is larger?

i. O(n) or O(n 3 )

ii. Ω(n) or Ω(3n + 2)

iii. Θ(n 2 ) or O(n 2 )

8. Algorithms

(a) What is the complexity of the linear search algorithm in the worst case? How would we go about calculating the average case complexity?

6

(b)

What is the complexity of the binary search algorithm in the worst case?

(c)

Is it possible for linear search to take less time to complete than binary search for particular input sets?

(d)

What is the purpose of experimental analysis? How would you perform an experimental analysis on the linear search algorithm?

(e)

What is the time complexity of the following algorithm in the best case? In the worst case?

public

void

calculateValue(int[]

items){

int

total=0;

 

for

(int

i=0;

i<items.size();i++){

for

(int

j=0;

j<items.size();j++){

 

total

=

total

+

(i

*

j);

 

}

}

}

 

7