1 / 12

Lecture 26 More on Guis and inner and anonymous classes

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,

lynch
Download Presentation

Lecture 26 More on Guis and inner and anonymous classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 26 More on Guis andinner 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 scientist-philosopher-poet-etc. Piet Hein, translated from Danish. Look up “grook” in google to find many more.

  2. 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. A6: Comments

  3. 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. A6: Recursive def of a webpage s2: http://www.cornell.edu By part (a), s1 is a node of the graph 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

  4. Zero is a number. If a is a number, the successor of a is a number. Zero is not the successor of a number. Two numbers of which the successors are equal are themselves equal. (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. Peano’s axioms for the natural numbers Since Zero is a number, so is its successor, call it One. Since One is a number, so is its successor , call it Two, …

  5. (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. A6: recursive definition of a webpage 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

  6. URL uweb. Suppose it is a node of graph. Suppose lnk is a link on it. URL ulnk= new URL(uweb, lnk.toLowerCase ()); • Throws a MalformedURLException? The link is malformed. Add lnk to malLinks. • No MalformedURLException? The link is legal. Add ulnk.toString to goodLinks. A6: good links and malformed links yes, do this, since the handout said so. Might mess up if link is on a Unix server, which is case sensitive. Try this in our DrJava interactions pane: u= new URL(“http://www.yahoo.com”); v= new URL(“what://www.yahoo.com”);

  7. 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 What’s an html page? page 2 of handout: 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.)

  8. 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.

  9. 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()); } publicvoid actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } privateclass BeListener implements ActionListener { publicvoid actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } } } listener for eastButton listener for westButton, in its own inner class

  10. Instead of this: eastButton.addActionListener(new BeListener()); … } privateclass BeListener implements ActionListener { publicvoid actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } } Do this: eastButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } } ); … } instance of an anonymous class that implements ActionListener

  11. m() y m() y x Out’s file drawer a0 a2 Out Out In’s file drawer In’s file drawer a1 a3 In In z m2() z m2() 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

  12. m() y m() y x Out’s file drawer a2 a0 Out Out In In Out$In Out$In In’s file drawer, named Out$In a3 a1 z m2() z m2() Out$In Out$In a2 a0 Flattened 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; } } } We can produce the flattened view from the unflattened view, and vice versa. Therefore, they have the same information Each In object and In’s file drawer has an extra scope box to say what object it resides in Out

More Related