910 likes | 1.25k Views
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
E N D
Java Lecture 4 CS 1311X Have a Coke! 13X11
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 • Class vs instance variable • Class vs instance methods • Inheritance • Reuse/Ease of maintenance • Adding more flavors • Arrays
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."); } }
The story so far... // class CokeMachine (continued) public void load(int n) { numCans += n; System.out.println("Loaded with " + numCans); } }
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?)
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
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
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; } }
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
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
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
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 >
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
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);
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()
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:
The Mysterious Default Constructor • Well actually it's invisible but if you could see it then it would look like this: public CokeMachine() { }
So where did it go? • Java giveth... • Java taketh away... • As soon as you define any constructor the default constructor no longer exists. • Why?
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; }
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 */ }
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.
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.
Notice the parameter names class CokeMachine { private int numCans; private int maxCans; // Constructor public CokeMachine(int num, int max) { numCans = num; maxCans = max; }
We want... • To name the parameters numCans maxCans • Why? • Two reasons • Documentation • Nature of CS Guys
Typical CS Geeks Guys Heh, heh... My high score in Space Invaders was 257,368
Typical CS Geeks Guys I wonder if Java Beans will give me gas?
Typical CS Geeks Guys I wonder how much of a curve there will be in CS 1311X?
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; }
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
How do it know? • Java differentiates constructors using the parameter list • CokeMachine() • CokeMachine(int, int) • Note: Same idea works for methods
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; }
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); }
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); }
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); }
Why so many constructors? • User convenience • CS 1312
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 = ___
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???
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!
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; ... }
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
Examples class SimpCokeMach { int numCans int serial; static int count = 0; public SimpCokeMach() { numCans = 10; count++; serial = count; }
Examples // class SimpCokeMach (continued) public int getNumCans() { return numCans; } public int getSerial() { return serial; } public int getCount() { return count; }
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()); }
Examples // class SimpCokeMach (continued) public int getNumCans() { return numCans; } public int getSerial() { return serial; } public static int getCount() { return count; }
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()); } }
Universal Solution? // class SimpCokeMach (continued) public static int getNumCans() { return numCans; } public static int getSerial() { return serial; } public static int getCount() { return count; }