0% found this document useful (0 votes)
20 views4 pages

Poplquiz 2

Uploaded by

vanditkharod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Poplquiz 2

Uploaded by

vanditkharod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ans 5: 1.

Evaluation of (render another-figure)

• another-figure is an instance of the class figure.

• The render generic function is invoked with another-figure as an argument.

• The applicable method is Method #1:

lisp

(defmethod render ((x figure))

(vertical-stroke

(slot-value x 'height)

(slot-value x 'width)))

• This method retrieves the height and width slots of another-figure, which were initialized as
10.0 and 10.0 respectively, when the instance was created with (make-instance 'figure :width
10.0 :height 10.0).

• The vertical-stroke function is then called with height = 10.0 and width = 10.0, which prints
the values as a list:

scss

(10.0 10.0)

• Expected Output:

lisp

(10.0 10.0)

2. Evaluation of (render some-figure)

• some-figure is an instance of the class paint-figure.

• The render generic function is invoked with some-figure as an argument.

• The applicable methods for render are:

o Method #3 (specific to paint-figure)

o Method #2 (a :before method for hue-mixture)

o Method #1 (for figure, which is a superclass of paint-figure)

• The CLOS method combination rules determine that:

1. The :before method (Method #2) runs first.

2. The primary method (Method #3) executes.

3. The primary method can invoke the next applicable method using call-next-method.

Step-by-Step Execution:
1. Method #2 (Before Method for hue-mixture):

o The set-brush-hue function is called with the cyan, magenta, and yellow values of
some-figure. The values are:

▪ cyan = 60, magenta = 0 (default), yellow = 55.

o set-brush-hue is not defined in the given code, so an error will occur unless it's
defined elsewhere.

o If set-brush-hue is defined, the method runs without outputting anything (as the
provided code snippet doesn't print or return a value from set-brush-hue).

2. Method #3 (Primary Method for paint-figure):

o The clearp slot is checked for some-figure, which is set to nil (:clearp nil).

o Since clearp is nil, the unless condition passes, and call-next-method is invoked.

3. Method #1 (Primary Method for figure):

o The height and width of some-figure are retrieved (height = 84, width = 38), and
vertical-stroke is called with these values.

o The vertical-stroke function prints the values as a list:

scss

(84 38)

• Expected Output:

lisp

(84 38)

Final Observations:

• For (render another-figure), the output will be:

scss

(10.0 10.0)

• For (render some-figure), the output will depend on whether set-brush-hue is defined:

o If set-brush-hue is undefined, an error will occur when Method #2 runs.

o If set-brush-hue is defined, the output will be:

scss

(84 38)
Ans2: The provided program has several components, but the root cause for throwing a
CloneNotSupportedException lies in the clone() method of MyClass and the improper handling of
the cloning process. Let's carefully examine the code to identify the issue.

Key Observations in MyClass:

1. MyClass implements Cloneable:

o This indicates that MyClass supports the cloning mechanism via the clone() method.
However, simply implementing Cloneable does not guarantee a proper cloning
process. The Object class's clone() method needs to be explicitly invoked.

2. clone() Method Implementation:

java

Copy code

protected MyClass clone() throws CloneNotSupportedException {

MyClass mc = (MyClass) super.clone(); // Invokes `Object.clone()`

mc.data = new Object(); // Reinitializes `data`

mc.data = data; // Assigns the original data value to `mc.data`

return mc;

o The method calls super.clone() to perform a shallow copy of the object.

o However, there is no issue directly in this method, as super.clone() works if Cloneable


is implemented correctly and Object.clone() is called.

Where the Exception Might Be Thrown:

ArrayList Cloning

• In the main method:

Java

Copy code

for (int i = 0; i < size; i++)

l3.add(new MyClass(l1.get(size - i - 1)));

This adds new instances of MyClass to l3, but it does not directly invoke the clone() method. Hence,
this block will not cause the exception.

Cloning Issue in ArrayList<MyClass> l2:

• Likely Issue: Somewhere in the code (potentially missing from the snippet), the clone()
method is invoked directly or indirectly on MyClass or ArrayList<MyClass> objects.
Specifically, if l2 involves cloning objects and the clone() method in MyClass is not properly
handling deep or shallow copies, a CloneNotSupportedException will be thrown.

Reason for CloneNotSupportedException:

• The Object.clone() method (invoked via super.clone()) checks whether the object
implements Cloneable. If the Cloneable interface is not implemented, it throws a
CloneNotSupportedException.

• If the clone() method in MyClass is invoked on an instance of a class that does not
implement Cloneable, the exception will occur.

Likely Cause in This Program:

1. Custom Object (LoremIpsum) Not Cloneable:

o The MyClass has a member loremIpsum of type LoremIpsum. If LoremIpsum is not


Cloneable and is shallowly copied or reinitialized improperly in MyClass.clone(), the
CloneNotSupportedException will occur.

2. Improper Handling of data:

o The data member is reassigned with a new object after cloning:

java

mc.data = new Object();

mc.data = data;

o If data itself is an object that does not properly support cloning, it could lead to an
exception if its clone method is indirectly invoked.

You might also like