1. Suppose we have a Rectangle class that includes length and width attributes o
ID: 3538172 • Letter: 1
Question
1. Suppose we have a Rectangle class that includes length and width attributes of type int, both set by the constructor. Create a compareTo method for this class so that rectangle objects are ordered based on their a) Perimeter, and b) Area.
2. Give examples from the "real world" of unsorted lists, sorted lists, indexed lists, lists that permit duplicate elements, and lists that do not permit duplicate elements.
3. Someone suggests that, instead of shifting list elements to the left when an object is removed, the array location holding that object should just be set to null. Discuss the ramifications of such an approach for each of our three list types.
4. Can the linear search algorithm be encoded using recursion? If not, why not? If so, outline an approach and discuss its advantages and disadvantages. Share your thoughts with others in the class.
5. Expand the RefUnsortedList class (section 6.7) with a public method endInsert, which inserts an element at the end of the list. Do not add any instance variables to the class. The method signature is:
public void endInsert(T element)
Explanation / Answer
public class Rectangle { protected int length; protected int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } // Compare Perimeter of Rectangle P = 2(L + W) public int compareTo(Rectangle r) { int perimeter1 = 2*(this.length + this.width); int perimeter2 = 2*(r.length + r.width); if(perimeter1Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.