CS 9G – Java – Final Review - PowerPoint PPT Presentation

issac
cs 9g java final review n.
Skip this Video
Loading SlideShow in 5 Seconds..
CS 9G – Java – Final Review PowerPoint Presentation
Download Presentation
CS 9G – Java – Final Review

play fullscreen
1 / 77
Download Presentation
CS 9G – Java – Final Review
1076 Views
Download Presentation

CS 9G – Java – Final Review

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CS 9G – Java – Final Review • Welcome! My name is Jonathan Tsai • Monday, December 13, 2004 • About 24 hours left until final • Hope you enjoy the review session • Slides will be available online at • PowerPoint http://www.ocf.berkeley.edu/~jontsai/files/cs9g_final_review.ppt • PDF http://www.ocf.berkeley.edu/~jontsai/files/cs9g_final_review.pdf 1

  2. CS 9G – Java – Final Review • CS 9G Final (You should have already signed up) • Tuesday, December 14 • 8-11am, 12:30-3:30 or 5-8pm. All in the Bechtel Auditorium • For updated info check: http://www-inst.eecs.berkeley.edu/~selfpace • Worth 40% of your grade • Need about 70% in class to pass (I'm not sure about this exactly) 2

  3. CS 9G – Java – Final Review • What to expect on the final • Big ideas in Java, topics indicated in the following slides • Knowing exact parameters and names of functions in obscure methods that are rarely used is not important • But having the proper syntax is • Partial credit will be given for small errors • Okay, ready? Let's go! • If I go to fast, feel free to stop me and ask questions 3

  4. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 4

  5. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 5

  6. Adding Machine • Applet • import java.applet.Applet • public void init ( ) • public void paint (Graphics) • Widgets • TextField • Button • Other • public void actionPerformed(Event) 6

  7. Adding Machine • Applet • import java.applet.Applet • public void init ( ) • public void paint (Graphics) • Widgets • TextField • Button • Other • public void actionPerformed(Event) 7

  8. Adding Machine • public void init( ) • Called whenever starting an applet • HTML code: <applet code=“AddingMachine.class” width=300 height=200></applet> • Set up objects • If have additional parameters to retrieve from HTML code to Applet Inside <applet ...> and </applet>, for each parameter put <param name="your_param_name" value="your_value"> 8

  9. Adding Machine • Applet • import java.applet.Applet • public void init ( ) • public void paint (Graphics) • Widgets • TextField • Button • Other • public void actionPerformed(Event) 9

  10. Adding Machine • public void paint(Graphics) • Usually called by the applet whenever repaint( ) is called • Graphics object to be painted on must be passed in as a parameter • Some Graphics method calls, given a Graphics object g • g.drawString(“Hello World”); • g.drawRect(0, 0, 50, 50); 10

  11. Adding Machine • Applet • import java.applet.Applet • public void init ( ) • public void paint (Graphics) • Widgets • TextField • Button • Other • public void actionPerformed(Event) 11

  12. Adding Machine • Widgets • import java.awt.*; • import java.awt.event.*; • Initialize it • Button reset = new Button(“reset”); • Add it to the applet • add(reset); • Bind it with the actionListener • reset.addActionListener(this); • this refers to the current Applet 12

  13. Adding Machine • Applet • import java.applet.Applet • public void init ( ) • public void paint (Graphics) • Widgets • TextField • Button • Other • public void actionPerformed(Event) 13

  14. Adding Machine • public void actionPerformed(Event) • Determine the source of the Event e • e.getSource( ) • Returns an object which corresponded to the call from the previous slide • reset.addActionListener(this) • Do some action • Usually call repaint( ) as well 14

  15. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 15

  16. Cat and Mouse 16

  17. Cat and Mouse • Four Classes • CatMouseChase.java • Cat.java • Mouse.java • Position.java • CatMouseChase was the Applet that controlled display and mouse interaction, etc 17

  18. Cat and Mouse • Cat and Mouse represented the cat and mouse, respectively • Each has a private field, Position, which represents the polar coordinates of the cat or mouse with respect to the base • Create or instantiate a new object by calling the constructor • Cat cat = new Cat( ); 18

  19. Cat and Mouse • Use public accessor functions to retrieve the private fields • Position catPos = cat.getPosition( ); • Call public methods like so • cat.move(0, 1); • catPos.toString() 19

  20. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 20

  21. Butterfly • The classes • ButterflyApplet • ButterflyCollection • Contains a data structure which holds butterflies • Buttefly • ButterflyDisplayer (for the thread, more on this later) • Three versions • Arrays • Vector or LinkedList (using Java API) • API = Application Programming Interface • Explicitly linked list (you made your own) 21

  22. Butterfly • What was the point of this exercise? • We hoped that you could see how you can implement your own data structure, such as a ButterflyCollection, that maintained a layer of abstraction between the applet/displayer and the actual butterflies being drawn • Can use generic methods like add(Butterfly), remove(Butterfly), etc. 22

  23. Butterfly • Implementing new data structures happens in real life all the time • Usually performs specific additional features, or to optimize time/space • Keep student records sorted by SID • It's meaning is as simple as the name • Something that holds data • Can you think of data structures in real life (or Java) that is based on simpler data structures? 23

  24. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 24

  25. Inventory • The classes • InventoryTest • Instantiated a new Inventory object, and attempted to fill it • Inventory • Underlying data structure: array of entries • Entry • The name of the item • How many there are • LineFormatException • The different errors that occur in a badly formatted input file 25

  26. Inventory • Let's examine some class and method signatures • public Entry (String s) throws LineFormatException • Yup, that's a constructor • They can also support the throws keyword, just like any other method • This simply means: “Calling this method can potentially cause this error (specifically, a LineFormatException).” • class LineFormatException extends Exception • LineFormatException is a subclass of Exception • This is a concept of inheritance, we'll get to that in a few slides 26

  27. Inventory • When something bad happens, we'd like to throw an exception if (something_bad_happens) { thrownew LineFormatException(“this is bad”); } • Note that you don't just throw Exception, you have to throw new Exception • Exceptions are just like any other object 27

  28. Inventory • But don't fret! We have a safety net, which is part of the idea of exception handling try { somethingBadMightHappen( ); } catch (LineFormatException e) { // Display a message } catch (Exception e) { // this is in case an exception we didn't expect is thrown // consider it a safety net for the safety net // usually don't need to be this cautious } 28

  29. Inventory • So what's the deal with throws? • We use this in a method signature to indicate that some caller to this method must implement try/catch, whether a direct caller or not • Otherwise, the program will crash when the exception is encountered • Example public void somethingBadMightHappen( ) throws LineFormatException { throw new LineFormatException(“just because”); } 29

  30. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 30

  31. Bear 31

  32. Bear • Depending how you implemented it, you might have 4 classes (similar structure to Butterfly program) • An applet • Something that extends Frame • A displayer • The Bear • We'll focus on the thread and application aspects of this program 32

  33. Bear • A simple definition of an applet • extends Applet • has an init( ) method • And an application • has a main method • Since we also want to have graphics displayed in a window, our application must also extend Frame • This does not mean all applications have to extend Frame • Inventory was an application that did not 33

  34. Bear • Simple way to do it: • Put a main( ) method inside the applet that instantiated the class that extends Frame (we'll call it BearFrame) • BearFrame has to also implements WindowListener, MouseListener • We'll talk about interfaces and implements later 34

  35. Bear • The thread aspect of the program • So what? • Computers and computer programs that we use commonly use don't really do multi-tasking (thought it seems that way) • True multi-tasking would require a separate CPU for each process • But, computers can achieve something to this effect by running multiple processes for a short interval at a time, and switching between them extremely rapidly • These are called threads 35

  36. Bear • How do I make a thread, say BearDisplayer? class BearDisplayer extends Thread { public voidrun( ) { // This method is called by the program when // BearDisplayer.start( ) is called } } • When do I use synchronized? • Usually if the method is called inside the run( ), or is part of a resulting call by a method invoked in run( ) • Don't worry too much about the specifics, you'll learn more about this if you continue with CS 36

  37. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 37

  38. Basics • Naming convention • Variables and Methods • startWithLowerCase • Initial letters of different words upper case in multi-word variable or method name • If variable is final, all capitals, with underscore between words • THIS_IS_A_CONSTANT • Classes • ThisIsAClass • Like variable, first letter is upper-cased 38

  39. Basics • Declare a variable • int n; • Or many at a time: int a, b, c, d; • Initialize a variable • n = 5; • Declare and initialize at same time • int n = 5; • int vars are initialized to 0 by default • boolean to false • But you shouldn't depend on this! • If something is declare as final, you can't reassign it (constants like Math.PI) 39

  40. Basics • Integer division int i = 5; i = 5 / 2; System.out.println(i); • What gets printed out? 40

  41. Basics • Only one public class per file • I'll write a sample class module to demonstrate this • Sorry for the spacing/indenting! File: FinalExamReview.java public class FinalExamReview { // note the name public FinalExamReview( ) { }// default constructor public void setName(String n) { myName = n; } public String getName( ) { return myName; } public String toString( ) { return myName; } public boolean equals(Object o) { return false; } private String myName; } 41

  42. Basics • Make a new FinalExamReview object • FinalExamReview rev = new FinalExamReview( ); • Do some stuff • rev.setName(“cs9g”); • System.out.println(rev); • In the last example, rev is not a String, but it's toString method will be called by println to get it's String representation 42

  43. Basics • Decisions: if and switch • if (just like C) if (something) { } else { } • Can be nested if (something) { } else { if (something_else) { } else { } } 43

  44. Basics • This block of statements if (something) { } else { if (something_else) { } else { } } • Is equivalent to if (something) { } else if (something_else) { } else { } 44

  45. Basics • Switch // generate a random integer between 0 and 10 int n = Math.random( ) * 11; String s = “”; switch (n) { case 0: s = “zero”; break; // exit the switch statement case 1: s = “one”; break; default: // if it did not match any previous case s = “greater than one”; // don't need break statement } 45

  46. Basics • Self test: convert this code to not use switch // generate a random integer between 0 and 10 int n = Math.random( ) * 11; String s = “”; switch (n) { case 0: s = “zero”; break; // exit the switch statement case 1: s = “one”; break; default: // if it did not match any previous case s = “greater than one”; // don't need break statement } 46

  47. Basics • For loop for (int i=0; i < array.length; ++i) { // Do something } • While loop while (somethingIsTrue) { // Do something } 47

  48. CS 9G – Java – Final Review • Programming Assignments • Applet Introduction: Adding Machine • Objects and Classes: Cat and Mouse • List Structures: Butterfly • Exception Handling: Inventory • Threads: Bear • Additional Topics • Basic Java • Object-oriented programming • Data Structures • Exceptions and Applications 48

  49. Object Oriented Programming • Why is OOP so cool? • Reusable code • How real world works (at least a good model of it) • Public • Private • Static • Inheritance • Interfaces 49

  50. Object Oriented Programming • == • Pronounced “equals-equals” (I do, at least) • For objects • Returns true if they are in the same memory location • False otherwise • For primitives like int, boolean • Returns true if they have the same value • False otherwise 50