2013 Data Structure Homework Chapter 6 1. Show two advantages and one disadvantage of using an ArrayList object instead of an array object? Advantages: 1) automatic resizing; 2) methods to accomplish common tasks such as inserting, removing, and searching; 3) size automatically maintained. Disadvantages: 1) index operator, [ ], not available, and so get / set methods must be invoked to access / modify elements in the ArrayList object; 2) elements in an ArrayList object must be (references to) objects, whereas elements in an array object can be either primitive values or (references to) objects. 2. The one-parameter add method in the ArrayList class always returns true. Would it make sense to change the return type from Boolean to void? Explain. No, Because the ArrayList class implements the Collection interface, in which the return type for the one-parameter add method must be boolean. 3. For each of the following program segments, hypothesize if the segment would generate a compile-time error, a run-time exception, or neither. Then test your hypotheses with a main method that includes each segment. a. ArrayList<String> myList = new ArrayList<String>(); myList.add ("yes"); myList.add (7); Compile-time error: cannot find method add(int) b. ArrayList<Double> original = new ArrayList<Double>(); original.add (7); Compile-time error: cannot find method add(int) c. ArrayList<Integer> original = new ArrayList<Integer>(); double x = 7; original.add (x); Compile-time error: cannot find method add(double) d. ArrayList<String> newList = new ArrayList<String>(); newList.add ("yes"); Integer answer = (Integer)newList.get (0); Compile-time error: Inconvertible types Found: String Requried: Integer 4. Hypothesize what will happen when the following code fragment is run, and then test your hypothesis: ArrayList<String> original = new ArrayList<String>(); original.add ("yes"); ArrayList<Integer> copy = (ArrayList<Integer>)original.clone(); System.out.println (copy.get (0)); Hint: This exercise illustrates why the copy constructor is superior to the clone() method. ANS There is a compile-time warning that the call to clone results in an unchecked cast. Execution terminates normally, and the output is yes. 5. In the VeryLongInt class, find the Big-Oh estimate of the add method? ANS: for loop takes O(n) plus reverse O(n), so the answer is O(n) 6. In the VeryLongInt class, develop the pseudo-code of a multiply method with the specification below: Stores in this object the product of the pre-call value of this object and the value of otherVeryLong object. multiply(VeryLongInt otherVeryLong) 1. declare an ArrayList called digits declare an int length, as the size of otherVeryLong.digits declare a veryLongInt object called product construct veryLongInt productCopy 2. clear all the elements in digits then add 0 3. for i=0 to length 1. construct a veryLongInt product 2. using *mult() to multiply with the i-th element in otherVeryLong 3. for j=0 to length-i 1. add 0 to product.digits /************************************************************** ex. When we multiply 15 with 15, first, we use 15 to multiply with 1, and we got 15. Second, we multiply with 5 and got 75. We have to add a 0 to 15 before adding these two numbers together. Then the answer will be 150 plus 75 which equals 225. **************************************************************/ 4. add(product) *mult() is a method for a VeryLongInt veryLong to multiply with single number n. 1. if n = 0, veryLong = veryLong * 0 2. for i = 1 to n, sum = sum + veryLong 7. Find the worst time of the multiply method above. for loop takes O(n^2) plus several loops with O(n), so the worst time is O(n^2) 8. import java.util.ArrayList; class C1 implements Cloneable {int i=1; ArrayList<String> a =new ArrayList<String>(1); public C1() { a.add(""); } public static void main(String[] args) throws CloneNotSupportedException { C1 obj1 = new C1(); obj1.a.set(0,"A"); System.out.println (obj1.i+" "+obj1.a); C1 obj2 = (C1) obj1.clone(); obj2.i=2; obj2.a.set(0,"B"); System.out.println (obj1.i+" "+obj1.a); System.out.println (obj2.i+" "+obj2.a);}} // end main Show the output of the program above. ANS 1 [A] 1 [B] 2 [B]