15. Define the equals method for QuadraticExp class, which overrides the method
ID: 3753993 • Letter: 1
Question
15. Define the equals method for QuadraticExp class, which overrides the method inherited from Object public class QuadraticExp implements cloneable private double a; private double b; private double c // equals method 16. Which statement is true for the Chapter 3 bag (with an array)? A. A programmer who uses the bag must call ensureCapacity as items are added, otherwise the bag might not be big enough for more tems. B. A programmer may optionally call ensureCapacity, but if he doesn't then the bag will grow as more items are added. 17. Who needs to know about the invariant of an ADT? A. Only the programmer who implements the class for the ADT B. Only the programmer who uses the class for the ADT C. Both the programmer who implements the class and the programmer w D. Neither the programmer who implements the class nor the programmer who uses the class ho uses the class. 18. For the class IntArrayBag that uses a single array implementation, how to implement a method such as add, remove, etc? What is the running time of the method? 19. How to use IntArrayBag class to create a bag, add an element to the bag, clone a bag etc?Explanation / Answer
15. public boolean equals(QuadraticExp e) { return (a == e.a) && (b == e.b) && (c == e.c); } 16. Optiuon B. ensureCapacity will be called by Bag itself. There is no explicit need for a call, but a user may do so. 17. A. Only the programmer who implements the ADT. The ADT takes care of making the invariant true always. Hence user don't need to know that. 18. In arrayBag, we can have a size variable, which tells how many elements are present in the bag. For removing the element, we can decrease the size and return the element. Similarly, For adding an element, we can add it to the end of the array. Add methods is O(1), while remove method is O(n). 19. IntArrayBag b = new IntArrayBag(); b.add(2); // add an element IntArrayBag c = b.clone(); // to clone
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.