SimpleRectangle
that return the area
and the perimeter of the rectangle (add two methods, one for each property).
SimpleRectangle
so that
rectangles are anchored in the plane (assume integer coordinates).
Add a new constructor that accepts
four parameters and provide default values in the original two-parameter
constructor.
Math.min
and Math.max
to make calculations.
Develop some test cases with anticipated results to see if your method works correctly.
equals
method to test if one rectangle is
equal to another:
java.awt.Rectangle
object (assume your method is a static method in a class
named RMath). You'll need to consult the API provided.
java.awt.Rectangle
, return the
rectangle that is their intersection, use the intersection
method rather than createIntersection
.
BigFactorial.java
determine how
many digits there are in 76! (call a method that returns the
result without writing new code).
What is the big-Oh complexity of for an N-element priority queue of removeMin and of peekMin? Justify.
/** * Removes and returns a minimal element of this pq * @return a minimal element after removing it */ public Object removeMin() { Object min = peekMin(); items.remove(min); return min; } /** * Returns a minimal element of this pq * @return a minimal element */ public Object peekMin() { int minIndex = 0; for (int i = 1; i < items.size(); i++) { Comparable c = (Comparable) items.get(i); if (c.compareTo(items.get(minIndex)) < 0) { minIndex = i; } } return items.get(minIndex); }
/** * Removes and returns a minimal element of this pq * @return a minimal element after removing it */ public Object removeMin() { int minIndex = 0; for(int k=1; k < items.size(); k++) { Comparable c = (Comparable) items.get(k); if (c.compareTo(items.get(minIndex)) < 0) { minIndex = k; } } Object retval = items.get(minIndex); items.set(minIndex,items.get(items.size()-1)); items.remove(items.size()-1); return retval; }
/** * Sorts list into ascending/non-decreasing order. */ public void sort(ArrayList list) { PriorityQueue pq = new ArrayPriorityQueue(); int size = list.size(); for(int k=0; k < size; k++) { pq.add(list.get(k)); } for(int k=0; k < size; k++) { list.set(k, pq.removeMin()); } }