Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. State the Java code to declare an array of three Listing objects named data t

ID: 3877604 • Letter: 1

Question

1.State the Java code to declare an array of three Listing objects named data that are initialized with the no-parameter constructor.

2. Draw a picture of the storage allocated in the previous exercise. Assume the array is stored at location 20, and the three objects are stored at locations 60, 70, and 50.

3.Give the Java invocation to clone the object objectA (using the method discussed in the previous exercise) and store a reference to the clone in the variable: newListing.

4. Two objects, objectA and objectB, are objects in the class Listing. The object objectA is copied to objectB. How many objects exist after the copy, if the copy is performed as: a shallow copy

Explanation / Answer

Hi,

lets first create the Listing class, please note that since we are going to use clone in the futher parts, Listing class needs to implement cloneable interface.

class Listing implements Cloneable

{

Listing(); //no parameter constructor.
     

    public Object clone() throws CloneNotSupportedException

    {

        return super.clone();

    }

}
Now, to declare an array of the above class like,

[0xFFFFFF70] data[2]

[0xFFFFFF60] data[1]

[0xFFFFFF50] data[0]

[0xFFFFFF20] data - pointing to start of heap

c. Listing newListing = (Listing) objectA.clone() // here objectA is an object of Listing class.
this way we can clone the object i.e the copy the object and assign it to a reference variable.
d. Shallow copy means a bit wise copy of the object i.e all the fields are copied to the new object and the new object will copy to the same location as that of the input, so if a shallow copy is performed, no of objects will be 3 as a new copy object is created.

Thumbs up if this was helpful, otherwise let me know in comments. Good Day.