Transcript Lec04-11

CS 121 – Intro to Programming:Java - Lecture 11
Announcements
Inheritance Owl assignment due 11/30 at 11 am
Programming assignment six due Friday 12/3
Honors section write-up now in place
public class JobTimer {
public void doJob(){
// has empty body - does nothing!
}
public void runJob() {
System.gc(); // clean up workspace
long S1 = System.currentTimeMillis();
doJob();
long S2 = System.currentTimeMillis();
long runTime = (S2 - S1);
System.out.println("running time in milliseconds: " + runTime);
}
}
public class RootTimer extends JobTimer{
// we'll see how long it takes to find square roots // we'll find square root of 2 1000 times, then take
// the average
public void doJob(){
double r;
for(int j = 0; j < 10000; j++)
r = Math.sqrt(2); // doing 10000 square roots!
}
public static void main(String[] args){
RootTimer r = new RootTimer();// make one so I can call
r.runJob();
}
}
œœ´œ ----jGRASP exec: java RootTimer
œœßœrunning time in milliseconds: 3
œœßœ
œœ©œ ----jGRASP: operation complete.
So: 10000 calculations of the square root of 2 took 3 milliseconds
Now: 10000000 times doing: mulitply, add, sqrtœœ
œœ´œ ----jGRASP exec: java RootTimer
œœßœrunning time in milliseconds: 45 (235*456)
œœßœ
œœ©œ ----jGRASP: operation complete.
œœœœ
œœ´œ ----jGRASP exec: java RootTimer
œœßœrunning time in milliseconds: 32 (235+456)
-jGRASP exec: java RootTimer
œœßœrunning time in milliseconds: 2081 Math.sqrt(222)
The class Object (7.2)
It’s the base class for everything - every object is, ultimately, a
kind of Object.
Object has three methods that - by inheritance - are part of
every class
toString - we’ve seen this. We override it all the time…
equals- we’ll override this also..
clone - does clones an object - we won’t discuss it..
Object class is important because we can - for example - sort
an array of any kind of Object with a single sorting method (as
long as we say how to compare pairs of the appropriate kind of
object - thus sorting students by last name results in a different
ordering than sorting students by age)
Recall that the element package has a class called Pt,
and if you say
Pt p = new Pt(5,32);
you’ve created a new point object, located at (5,32)
Suppose we have an array of Pt objects and we want to
sort them by x coordinate…
Java has a sort of universal comparison mechanism for
comparing any pairs of objects of the same kind. It’s
called the Comparable interface (text, p 300)
import element.*;
public class Pnt extends Pt implements java.lang.Comparable{
public Pnt(int x, int y){ super(x,y);}
public int compareTo(Object other){
// compare by x coordinate
Pnt p = (Pnt) other;
if (this.x() < p.x())
return -1;
else if (this.x() == p.x())
return 0;
else
return 1;
}
}
public class Sorting{
public static void main(String[] a){
Pnt[] pts = new Pnt[8];
int x, y;
for(int j = 0; j < 8; j++){
x = (int) (Math.random() *100);
y = (int) (Math.random() *100);
pts[j] = new Pnt(x,y);
}
for (int j = 0; j < 8; j++)
System.out.println(pts[j]);
java.util.Arrays.sort(pts);
System.out.println(" sorted..");
for (int j = 0; j < 8; j++)
System.out.println(pts[j]);
}
}
œœßœ<Point: (21,15)>
œœßœ<Point: (98,24)>
œœßœ<Point: (26,53)>
œœßœ<Point: (7,16)>
œœßœ<Point: (79,74)>
œœßœ<Point: (61,91)>
œœßœ<Point: (10,32)>
œœßœ<Point: (40,87)>
œœßœ sorted..
œœßœ<Point: (7,16)>
œœßœ<Point: (10,32)>
œœßœ<Point: (21,15)>
œœßœ<Point: (26,53)>
œœßœ<Point: (40,87)>
œœßœ<Point: (61,91)>
œœßœ<Point: (79,74)>
œœßœ<Point: (98,24)>
A complicated application - Rectangle Layout
Who cares?
Glass people, plywood people, metals people,
etc
I want to talk about one of the simplest rectangle
packing algorithms:
shelf packing
9
8
4
1
5
2
6
3
7
Given a set of ~ 30 to 50 randomly generated
rectangles (can’t rotate) and given a fixed
rectangular holder region, place the rectangles in
the region using a shelf-packing algorithm, draw
the placement picture, and report statistics on
%coverage, # of rectangles placed, # unplaced.