Transcript powerpoint

Lecture 26 More on Guis and
inner and anonymous classes
Reflection re fractions
It is indeed too odd for words
that half's three quarters of two thirds.
Problems
Problems worthy of attack
prove their worth by hitting back.
T.T.T.
When you feel how depressingly slowly you climb,
it's well to remember that Things Take Time.
A PSYCHOLOGICAL TIP
Whenever you're called on to make up your mind,
and you're hampered by not having any,
the best way to solve the dilemma, you'll find,
is simply by spinning a penny.
No - not so that chance shall decide the affair
while you're passively standing there moping;
but the moment the penny is up in the air,
you suddenly know what you're hoping
The above are a few of the 10,000 “grooks” by scientistphilosopher-poet-etc. Piet Hein, translated from Danish.
Look up “grook” in google to find many more.
1
A6: Comments
We clarify some points on A6.
Method getLinks should return an instance of
class Links, not an instance of class Linker.
It was a typo on the handout.
2
A6: Recursive def of a webpage
3. The graph is defined as follows (this is a
recursive definition!):
(a) webpage s1 is a node of the graph.
(b) if
(1) A webpage p1 is a node of the graph,
(2) p1 contains a link p2,
(3) s2 is a prefix of p2,
(4) p2 has protocol “http” or “file”, and
(5) p2 is an html page
then webpage p2 is a node of the graph.
s2: http://www.cornell.edu
s1: http://www.cornell.edu
Its links:
s3: http://www.yahoo.com
s4: http://www.cornell.edu/x.html
s5: http://www.cornell.edu/pic.jpg
s6: y.html
By part
(a), s1 is a
node of
the graph
3
Peano’s axioms for the natural numbers
1. Zero is a number.
2. If a is a number, the successor of
a is a number.
3. Zero is not the successor of a
number.
4. Two numbers of which the
successors are equal are
themselves equal.
5. (Induction axiom.) If a set S of
numbers contains zero and also
the successor of every number in
S, then every number is in S.
Since Zero is a number, so is its successor,
call it One. Since One is a number, so is its
successor , call it Two, …
4
A6: recursive definition of a webpage
(b) if
(1) A webpage p1 is a node of the graph,
(2) p1 contains a link p2,
(3) s2 is a prefix of p2,
(4) p2 has protocol “http” or “file”, and
(5) p2 is an html page
then webpage p2 is a node of the graph.
By part (a), s1 is a node of the graph.
By part (b), so are s4 and s6 –not the others;
s2: http://www.cornell.edu
s1: http://www.cornell.edu
Its links:
s3: http://www.yahoo.com
s4: http://www.cornell.edu/x.html
s5: http://www.cornell.edu/pic.jpg
s6: y.html
5
A6: good links and malformed links
URL uweb. Suppose it is a node of graph.
Suppose lnk is a link on it.
URL ulnk=
new URL(uweb, lnk.toLowerCase ());
yes, do this, since the handout said so.
Might mess up if link is on a Unix
server, which is case sensitive.
• Throws a MalformedURLException? The
link is malformed. Add lnk to malLinks.
• No MalformedURLException? The link is
legal. Add ulnk.toString to goodLinks.
Try this in our DrJava interactions pane:
u= new URL(“http://www.yahoo.com”);
v= new URL(“what://www.yahoo.com”);
6
What’s an html page? page 2 of handout:
Use this method to determines whether URL
ulnk is an “html page”:
(1) if ulnk does not have s2 as a prefix, return
false.
(2) If ulnk does not have protocol http or file,
return false.
(3) If ulnk ends in .html or .htm, return true.
(4) If ulnk ends in one of the following, return
false: .jpg, .jpeg, .gif, .pdf, .ps, .txt, .css, .png,
.bmp, .doc, .rtf, .exe, .swf.
(5) Return true
Reason for (4). These things are known not
to be html pages.
Point (4). May cause you to have a nodes of
the graph some things that are not really
webpages. But there ARE other html pages
other thatn those that end in .html or .html.
Dynamically generated pages. (e.g. .shtml.)
7
Inner and anonymous classes
You know that an inner class can reference
fields of the class in which it appears. This
property is why we want inner classes.
Why do we name things?
People, dogs, classes
Because we want to refer to them often.
Do you give a name to a particular flower?
The pencil you are holding --Do you call it
Patrick, or Penelope? No, because you don’t
have to refer to it.
Things that are never referred to, or
referred to just once, don’t need a name.
In Java, a class that is referred to only once
can be written as an anonymous class.
8
public class ButtonDemo3 extends JFrame
implements ActionListener {
private JButton westButton= new JButton("west");
private JButton eastButton= new JButton("east");
/** Constructor: inv frame with title t, two buttons */
public ButtonDemo3(String t) {
super(t);
Container cp= getContentPane();
cp.add(westButton,BorderLayout.WEST);
cp.add(eastButton,BorderLayout.EAST);
westButton.setEnabled(false);
eastButton.setEnabled(true);
westButton.addActionListener(this);
eastButton.addActionListener(new BeListener()); }
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled();
listener for
eastButton.setEnabled(!b);
eastButton
westButton.setEnabled(b); }
}
private class BeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled(); listener for
eastButton.setEnabled(!b);
westButton,
westButton.setEnabled(b); in its own inner class
}
}
9
Instead of this:
eastButton.addActionListener(new BeListener());
…
}
private class BeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled();
eastButton.setEnabled(!b);
westButton.setEnabled(b);
}
}
instance of an anonymous
Do this:
class that implements
ActionListener
eastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled();
eastButton.setEnabled(!b);
westButton.setEnabled(b);
}
}
);
…
}
10
Unflattened view of a class
public class Out {
public static int x;
private int y;
public void m() {}
class In {
int z;
void m2() {z= y; }
}
}
Files in the directory
Out.java
Out.class
Out$In.class
Out’s file drawer
x
a0
a2
Out
m() y
In’s file drawer
a1
z
m2()
Out
m() y
In’s file drawer
a3
In
z
m2()
In
11
Flattened view of a class
public class Out {
We can produce the
public static int x;
flattened view from
private int y;
the unflattened view,
public void m() {}
and vice versa.
class In {
Therefore, they have
int z;
the same information
void m2() {z= y; }
}
Each In object and In’s file drawer
}
has an extra scope box to say what
object it resides in
Out’s file drawer
x
a0
m() y
a2
Out
In Out$In
Out
m() y
In Out$In
In’s file drawer, named Out$In
a1
z
m2()
a3
Out$In
a0
z
m2()
Out
Out$In
a2
12