1 / 91

Java Lecture 4

Java Lecture 4. CS 1311X Have a Coke!. 13 X 11. Take the Survey!. http://www.coursesurvey.gatech.edu. Today's Lecture brought to you with the kind assistance of. Juicing Up the Coke Machine. Adding cash More methods Adding setup flexibility Constructors Adding a serial number

gaston
Télécharger la présentation

Java Lecture 4

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. Java Lecture 4 CS 1311X Have a Coke! 13X11

  2. Take the Survey! http://www.coursesurvey.gatech.edu

  3. Today's Lecture brought to you with the kind assistance of...

  4. Juicing Up the Coke Machine • Adding cash • More methods • Adding setup flexibility • Constructors • Adding a serial number • Class vs instance variable • Class vs instance methods • Inheritance • Reuse/Ease of maintenance • Adding more flavors • Arrays

  5. The story so far... class CokeMachine { private int numCans = 3; public void vend() { if(numCans > 0) { System.out.println("Have a coke!"); numCans--; } else { System.out.println("Sorry, no Cokes."); } }

  6. The story so far... // class CokeMachine (continued) public void load(int n) { numCans += n; System.out.println("Loaded with " + numCans); } }

  7. Issues • How many cans can the machine hold? • Should the CokeMachine print things out? • Want to be able to vary initial number of cans • Want to give machine a name • Want to have a serial number • Want more variety (Mr. Pibb?)

  8. How many cans can the machine hold? class CokeMachine { private int numCans = 3; private int maxCans = 60; public void vend() { if(numCans > 0) { System.out.println("Have a coke!"); numCans--; } else { System.out.println("Sorry, no Cokes."); } } // vend

  9. How many cans can the machine hold? // class CokeMachine (continued) public void load(int n) { int temp = numCans + n; if(temp > maxCans) { System.out.println("Attempt to overload"); numCans = maxCans; } else { numCans = temp; System.out.println("Loaded with " + numCans); } // load } // CokeMachine

  10. Should the Machine print things out? class CokeMachine { private int numCans = 3; private int maxCans = 60; public boolean vend() { if(numCans > 0) { numCans--; return true; } else { return false; } }

  11. Should the Machine print things out? public int load(int n) { // Will return number of cans loaded int retval; int temp = numCans + n; if(temp > maxCans) { retval = maxCans - numCans; numCans = maxCans; } else { retval = n; numCans = temp; } return retval; } // load

  12. Testing Using a test Main // Static method for testing coke machines public static void vendNCokes(int n, CokeMachine cm) { for(int i=0; i<n; i++) { if(cm.vend()) { System.out.print("Coke! "); } else { System.out.println("Empty"); } } } // vendNCokes

  13. Testing Using a test Main public static void main(String args[]) { CokeMachine cokeM; cokeM = new CokeMachine(); vendNCokes(5, cokeM); System.out.println("Tried to load 30 actually " + cokeM.load(30)); vendNCokes(5, cokeM); System.out.println("Tried to load 60 actually " + cokeM.load(60)); } // main } // CokeMachine

  14. Result >javac CokeMachine.java >java CokeMachine Coke! Coke! Coke! Empty Empty Tried to load 30 actually 30 Coke! Coke! Coke! Coke! Coke! Tried to load 60 actually 35 >

  15. Vary initial number of cans • Often it's necessary of perhaps desireable to initialize variables in the object. • Java allows the programmer to write special initialization modules called constructors • A constructor can only be run once, automatically at the time the object is created using the new operator

  16. Adding a Constructor class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int num, int max) { numCans = num; maxCans = max; } Using a constructor: CokeMachine cm; cm = new CokeMachine(3, 60);

  17. What happened? >javac CokeMachine.java CokeMachine.java:51: No constructor matching CokeMachine() found in class CokeMachine. cokeM = new CokeMachine(); ^ 1 error Was there a constructor that looked like: public CokeMachine()

  18. The Mysterious Default Constructor • Java automatically supplies a constructor to any class that doesn't have one. It's cleverly known as the default constructor. • It looks like this:

  19. The Mysterious Default Constructor • Well actually it's invisible but if you could see it then it would look like this: public CokeMachine() { }

  20. So where did it go? • Java giveth... • Java taketh away... • As soon as you define any constructor the default constructor no longer exists. • Why?

  21. But I want one! class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int num, int max) { numCans = num; maxCans = max; } public CokeMachine() { numCans = 3; maxCans = 60; }

  22. You could even do this class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int num, int max) { numCans = num; maxCans = max; } public CokeMachine() { /* Note that numCans and maxCans are not initialized */ }

  23. So you would need these... // class CokeMachine (continued) public setNumCans(int n) { numCans = n; } public setMaxCans(int n) { maxCans = n; } Methods which modify the value of variables inside of the object or class are known as modifiers.

  24. You could also write these... // class CokeMachine (continued) public int getNumCans() { return numCans; } public int getMaxCans() { return maxCans; } Methods which return the value of variables inside of the object or class are known as accessors.

  25. Notice the parameter names class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int num, int max) { numCans = num; maxCans = max; }

  26. We want... • To name the parameters numCans maxCans • Why? • Two reasons • Documentation • Nature of CS Guys

  27. Documentation

  28. Typical CS Geeks Guys

  29. Typical CS Geeks Guys Heh, heh... My high score in Space Invaders was 257,368

  30. Typical CS Geeks Guys I wonder if Java Beans will give me gas?

  31. Typical CS Geeks Guys I wonder how much of a curve there will be in CS 1311X?

  32. Writing like the big kids... class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int numCans, int maxCans) { this.numCans = numCans; this.maxCans = maxCans; } public CokeMachine() { numCans = 3; maxCans = 60; }

  33. Writing like the big kids... class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int numCans, int maxCans) { this.numCans = numCans; this.maxCans = maxCans; } public CokeMachine() { this(3, 60); } Constructor Chaining

  34. How do it know? • Java differentiates constructors using the parameter list • CokeMachine() • CokeMachine(int, int) • Note: Same idea works for methods

  35. Lots of flexibility class CokeMachine { private int numCans; private int maxCans; private String name; public CokeMachine(String name, int numCans, int maxCans) { this.numCans = numCans; this.maxCans = maxCans; this.name = name; }

  36. Lots of flexibility // class CokeMachine (continued) public CokeMachine(String name) { this(name, 3, 60); } public CokeMachine(String name, int numCans) { this(name, numCans, 60); } public CokeMachine(int maxCans, String name) { this(name, 3, maxCans); } public CokeMachine(int numCans, int maxCans) { this("unnamed", numCans, maxCans); }

  37. Let's not get carried away! // class CokeMachine (continued) public CokeMachine(int numCans) { this("unnamed", numCans, 60); } public CokeMachine(int maxCans) { this("unnamed", 3, maxCans); }

  38. Lots of flexibility // class CokeMachine (continued) public CokeMachine(String name) { this(name, 3, 60); } public CokeMachine(String name, int numCans) { this(name, numCans, 60); } public CokeMachine(int maxCans, String name) { this(name, 3, maxCans); } public CokeMachine(int numCans, int maxCans) { this("unnamed", numCans, maxCans); }

  39. Why so many constructors? • User convenience • CS 1312

  40. Recall Kurt's Dream CokeMachine Object numCans = ___ maxCans = ___ name = ___ CokeMachine Object numCans = ___ maxCans = ___ name = ___ CokeMachine Object numCans = ___ maxCans = ___ name = ___ class CokeMachine { private int numCans; private int maxCans; private String name; ... } CokeMachine Object numCans = ___ maxCans = ___ name = ___

  41. Want to have a serial number? • Change in requirements • Each machine should have a serial number • Will need to keep track of how many machines have been created • Where to keep a variable that keeps track of the number of machines? • Encapsulation???

  42. Adding a Serial Number class CokeMachine { private int numCans; private int maxCans; private String name; private int serial; private static int count = 0; public CokeMachine(String name, int numCans, int maxCans) { this.numCans = numCans; this.maxCans = maxCans; this.name = name; count++; serial = count; } Note: Since we used constructor chaining all the other constructors still work!

  43. Recall Kurt's Dream CokeMachine Object numCans = 3 maxCans = 60 name = "CoC" serial = 2 static count CokeMachine Object numCans = 3 maxCans = 60 name = "CoC" serial = 3 static count CokeMachine Object numCans = 3 maxCans = 60 name = "CoC" serial = 1 static count class CokeMachine { private int numCans; private int maxCans; private String name; private int serial; private static int count = 3; ... }

  44. Static vs. Instance • Variables that "live" in the class: Static or Class Variables • Variables that "live" in the object: Instance Variables • We can (and typically must) have corresponding methods

  45. Examples class SimpCokeMach { int numCans int serial; static int count = 0; public SimpCokeMach() { numCans = 10; count++; serial = count; }

  46. Examples // class SimpCokeMach (continued) public int getNumCans() { return numCans; } public int getSerial() { return serial; } public int getCount() { return count; }

  47. Examples // class SimpCokeMach (continued) public static void main(String args[]) { SimpCokeMach scm1 = new SimpCokeMach(); SimpCokeMach scm2 = new SimpCokeMach(); System.out.println(scm1.getNumCans()); System.out.println(scm2.getNumCans()); System.out.println(scm1.getMaxCans()); System.out.println(scm2.getMaxCans()); System.out.println(getCount()); }

  48. Examples // class SimpCokeMach (continued) public int getNumCans() { return numCans; } public int getSerial() { return serial; } public static int getCount() { return count; }

  49. Examples class STester { public static void main(String args[]) { SimpCokeMach scm1 = new SimpCokeMach(); SimpCokeMach scm2 = new SimpCokeMach(); System.out.println(scm1.getCount()); System.out.println(scm2.getCount()); System.out.println(SimpCokeMach.getCount()); } }

  50. Universal Solution? // class SimpCokeMach (continued) public static int getNumCans() { return numCans; } public static int getSerial() { return serial; } public static int getCount() { return count; }

More Related