190 likes | 205 Views
Final Review (lectures 8-16). TA: Truman Email: sylam@cs.ucsd.edu. File. File System Directory, File, Permission (read, write, execute) Permission: ls –l filename [in linux] Text vs Binary Human readable, ascii encoding Non-human readable..etc Very imprecise definition though. Examples
E N D
Final Review(lectures 8-16) • TA: Truman • Email: sylam@cs.ucsd.edu
File • File System • Directory, File, Permission (read, write, execute) • Permission: ls –l filename [in linux] • Text vs Binary • Human readable, ascii encoding • Non-human readable..etc • Very imprecise definition though. • Examples • myNotes.txt • mp3 • .exe • .class
File API • http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html • createNewFile() • Exists() • isDirectory() • …etc
File I/O • Be able to read (input) and write (output) • FileInputStream(String filename) • FileOutputStream(String filename) • Scanner class (input) • Scanner s = new Scanner(new FileInputStream(“input.txt”); • S.nextInt()…etc • PrintWriter (output) • PrintWrite p = new PrintWriter(“output.txt”); • P.print()…etc • never throw I/O exceptions (use checkError()) • Print() vs println() • Buffering.
File I/O • DataInputStream, DataOutputStream • Random access file • Seek(long location)… • Streams provide one-way access • Once it’s read, can’t go back • Read/Write Objects • Needs to implement Serializable • Override writeObject(), readObject(). • ObjectInputStream, ObjectOutputStream • Examples • ObjectInputStream in = new ObjectInputStream(new FileInputStream(“filename”); • In.readObject(); //cast to the right class
GUI vs Web page • How to create GUI/Web pages? • Basic idea: design the layout, then add stuff into it. • Designing the layout in GUI is very similar to designing a web page • For web pages, use HTML table • For GUI, use LayoutManager
Swing • Simplest example • JFrame j = new JFrame(“test”); j.setVisible(); • JFrame vs JPanel • Both are containers – can contain other components • Create a window application (Jframe) • Add JPanel into it. • Every container can have its own layout • JFrame • setVisible() • setDefaultCloseOperation() • EXIT_ON_CLOSE • HIDE_ON_CLOSE • DISPOSE_ON_CLOSE • DO_NOTHING_ON_CLOSE • LayoutManager • FlowLayout, BorderLayout, GridLayout • setMenuBar • Other components: JLabel, JTextfiedl, JCheckbox…etc
One example • create a JFrame • setLayout(new BorderLayout()); GridLayout(), FlowLayout();…etc • setTitle, setSize, set…, add([component]); • implement ActionListener • public actionPerformed(ActionEvent e) • e.getSource, e.getActionCommand()…etc • addWindowListener([subclass of WindowAdapter]); • setMenuBar(), JMenuBar, JMenu, JMenuItem • JLabel, JButton, JTextField • setText • JButton: setEnabled(true/false)
Interface • What’s an interface? • It specifies what methods a class needs to have. • If your class implements an interface, need to implement all the methods • Why need it? • Leads to better design • Less code change • Can an interface “inherit” another interface? • Can you define variables inside the interface? Note: you usually hear something like: “let’s agree on the interface…”
Listener • What is it? • An object that satisfies the listener interface • NO multiple inheritance, but can implement multiple interfaces • ActionListener, WindowListener, MouseListener…etc • An object can be a listener, meaning that it’s listening to some EVENTs, so that when the event occurs, the object can handle it. • So what’s an event??
Event • Low level • Mouse moved, pressed, • Key pressed…etc • Semantic • User defined • Button pressed • Checkbox changed…etc • You can even define your own events!
Inner class • What’s the main difference between inner class and normal one? • Local inner class • Defined in a method, local to that method only • Can ONLY access final local variables • Anonymous class • Without providing a name • Pros: • Cons:
WindowAdapter • Relationship to WindowListener? • Why use window adapter? • Pros & cons?
Draw stuff on JPanel • paintComponent(Graphic g) • When is this called? • Becomes visible • Resized • Repaint is called. • Do you call it directly? • Call Repaint()
Design patterns • Model View Controller • Observer pattern
Observer pattern • The Observer pattern defines an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically. • Subject == model
Java supports it! • Model class extends Observable • Observer class implements “Observer” interface • When data changed, call • setChanged() • Notify()