Presentation is loading. Please wait.

Presentation is loading. Please wait.

Final Review (lectures 8-16) TA: Truman

Similar presentations


Presentation on theme: "Final Review (lectures 8-16) TA: Truman"— Presentation transcript:

1 Final Review (lectures 8-16) TA: Truman Email: sylam@cs.ucsd.edu

2 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

3 File API http://java.sun.com/j2se/1.5.0/docs/api/jav a/io/File.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/jav a/io/File.html createNewFile() Exists() isDirectory() …etc

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

5 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

6 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

7 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

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

9 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…”

10 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??

11 Event Low level –Mouse moved, pressed, –Key pressed…etc Semantic –User defined –Button pressed –Checkbox changed…etc You can even define your own events!

12 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:

13 WindowAdapter Relationship to WindowListener? Why use window adapter? Pros & cons?

14 Draw stuff on JPanel paintComponent(Graphic g) When is this called? –Becomes visible –Resized –Repaint is called. Do you call it directly? –Call Repaint()

15 Design patterns Model View Controller Observer pattern

16 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

17

18

19 Java supports it! Model class extends Observable Observer class implements “Observer” interface When data changed, call –setChanged() –Notify()


Download ppt "Final Review (lectures 8-16) TA: Truman"

Similar presentations


Ads by Google