Homework 1
This first homework is due on Tuesday October 1st (with the
possibility to hand-in your homework on Thursday October 3rd - last firm
deadline - The solutions will be posted in BlackBoard on Friday October 4th).
This homework is to be done alone or in a group of two students. If
you decide to do it by 2 you have to follow the rules of 'Extreme
Mathematical Thinking' and 'Pair Programming' (to be discussed in
class).
Students have to hand-in a hard-copy with their answers and the
listing of all their JAVA code. The written answers must be explained
clearly in a maximum of 10 lines. The JAVA code must be accessible
from matrix in the directory called CS361/HW1/. The JAVA code must be
clear and commented. All the explanations to access the code on matrix
must be provided in your hard-copy.
All the names (names of directory, methods, ...) provided in this
homework must be respected.
1. Dijkstra's algorithm for gcd is the following:
gcd(m,n) =
m if m = n
gcd(m-n, n) if m > n
gcd(m, n-m) if m < n
a. Is this definition well-formed? Explain.
b. Is this definition well-defined? Explain.
c. Compute gcd(20,25)? Show each step.
d. Is this definition tail recursive?
e. Give an iterative definition of the definition of gcd given above.
2. Test the evaluation of recursive definition of functions
(innermost, outermost or simultaneous) of at least 3 programming
languages. Explain how you proceed.
3. Discuss the interest of the existence of the possibility to define
recursive functions in a programming language.
4. This exercise refers to Node.java given here (Explanations are available here).
- A recursive method that returns the length of a list. The
signature of listLengthRec is the following:
public static int listLengthRec(Node head)
b. We consider a list, L, of 3 objects of type Circle with the following
instanciation of instance variables: (1.0, 2.1, 2), (0.3, 5.0, 3) and
(2.0, 4.1, 4).
class Circle {
public double center_x, center_y;
public double radius;
public int window_number;
}
Draw the state of the memory at each step of the evaluation of:
Node.listCopy(L). (listCopy is a method of the Node Class).
The parameters, arguments and local variables must appear on your drawing.
c. i. What is a shallow clone of an object? What is a deep clone of an object?
ii. Add to the Node class a clone method that implements the clone method of the Object
class. Clone is a protected method in the Object
class. (For an example of clone method look at the Location.java
discussed in class - the definition of a clone method implementing the
clone method of the Object class follows always the same pattern).
The signature of clone is the following:
public Object clone()
iii. Draw the state of the memory at each step of the evaluation of:
Node.clone(L) (with the clone method you defined).
iv. Is the clone method you defined a shallow clone or a deep clone
method? If it is a shallow clone method, try to implement a deep clone
method or discuss the difficulty of implementing a deep clone
method.
v. Discuss the differences between a shallow clone, a deep clone and
listCopy.
5. How would you prove by a JAVA program that the field
resolution is done at compile time and the method resolution is done
dynamically in JAVA? Provide the JAVA code and explain.
6. Exercises 2.1 and 2.2 page 46.