Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?

Similar presentations


Presentation on theme: "Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?"— Presentation transcript:

1 Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?

2 Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?  Syllables in highlighted phrase: 24

3 Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?  Syllables in highlighted phrase: 24  If it existed, this defines number in 24 syllables  Therefore no such number exists!

4

5 Common Nightmare

6 Prevent the Nightmare  Exactly same holds for objects  Fields also should be kept private: private int fieldName;  Field accessed only within class  Improves modularity of your program  Makes object-oriented programs easier to write  Others cannot change value of field; can they?

7 Quick Cross-College Poll  Analyzing college students credit habits  Looking for patterns in number & brand of cards  Need everyone to fill out a sheet with:  Name  Credit card number  Credit card expiration date  Credit card type (Visa, Mastercard, Amex, etc.)  Data stored in array for use in later analyses

8 Access Limits are Useful  Obviously need to prevent universal data access  Else I’d be sipping drink with an umbrella on Wed.  Already discussed several reasonable limits  Do not give out credit card or social security numbers  Always make your fields private  To access fields, always use getters & setters  Consider company’s array of charge receipts…

9 Array of Charges at a Store Card  12341234 … Amount  $100.45 Card  23452234 … Amount  $10.45 Card  34452234 … Amount  $165.23 Card  53492134 … Amount  $1.52 Card  12341234 … Amount  $65.12 Card  12341234 … Amount  $98.88

10 Five-[Line] Discount public void hack(Charge[] arr) { double diff = 0; for (int i = 0; arr.length; i++) { Charge c = arr[i]; if (c.getCard().getNum() == 12341234) { diff += c.getAmount() – 0.02; c.setAmount(0.02); } else { c.setAmount(c.getAmount() + diff); diff = 0; } } }

11 Array of Charges at a Store Card  12341234 … Amount  $0.02 Card  23452234 … Amount  $110.87 Card  34452234 … Amount  $165.23 Card  53492134 … Amount  $1.52 Card  12341234 … Amount  $0.02 Card  12341234 … Amount  $0.02

12 Problem with Charge Class  Charge class is mutable  Fields’ values can be changed once they are set  Could go in & remove all setter methods  Code could be added later, by accident or by evil  Especially in larger, longer projects this happens  Use final to eliminate this possibility  For those final, must set field in constructor  Compiler error when set anywhere else in code

13 Update Charge Class public final class Charge { private final Card card; private final float amount; public Charge(Card account, float amt) { card = account; amount = amt; } public void setAmount(float amt) { amount = amt; } public float getAmount() { return amount; } public void cancelCharge() { amount = 0; }

14 Is Charge Immutable?  Even now, Charge class still mutable!  Use getter for card field & change fields in it  Get five-finger discount via less clever means  Getter returns reference to Card instance  Can then directly modify Card instance: charge.getCard().setNumber(234342313); Card Amount  $123.45 Number  12341234 ExpMonth  12 ExpYear  2008 charge

15 Make Mine Mutable?  Oops… we still need ability to cancel charges  Type wrong amount when ringing up sale at register  Customers decide they DID want Hawaiian short  Impossible if Charge immutable class in code  Class would need to be both mutable & immutable

16 Both Mutable & Immutable Two ways to solve problem (until later this week) 1. Define and use companion class  Create mutable & immutable classes in code  Duplicate by instantiating immutable object  But if changing mutable object what can we do? 2. Provide clone of the original object

17  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

18  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

19  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

20  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

21  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

22  Cloning creates new instance duplicating original  Objects can be cloned as often as desired  Clones do not effect original, so can be changed What is a Clone?

23 Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing

24 Cloneable Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing

25 Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing Cloneable

26 Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing Cloneable

27 Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing Not Cloneable

28 Send in the Clones!  All classes include protected Object clone();  protected limits calls to method to within package  Default throws CloneNotSupportedException  Not usable by all classes, despite method existing ???

29 Cloning Template public class Inventory { // Lots of unimportant code public Object clone() throws CloneNotSupportedException{ Inventory newOne = null; return (Inventory)super.clone(); } } Inventory v = new Inventory(); Inventory x = v.clone(); // Returns null!

30 March of the Clones  Object.clone checks Cloneable was listed not  Whether clone defined does not matter  If Cloneable not listed, error may cause crash  Could  Could omit super.clone() call at start  But Object.clone() does a lot of needed work

31 Cloning Template Clonable public class Inventory implements Clonable { // Lots of unimportant code public Object clone() throws CloneNotSupportedException{ Inventory newOne = null; return (Inventory)super.clone(); } } Inventory v = new Inventory(); Inventory x = v.clone(); // Now it works!

32 Your Turn  Get into your groups and complete activity

33 For Next Lecture  Read AF 10.1 - 10.3 for Wednesday extendssuperthis  How are extends, super, and this used? is-a  What does is-a relationship mean for classes?  Exactly what is inheritance and why is it useful?  There is weekly assignment problem on Angel  Due by 5PM next Tuesday (via e-mail)


Download ppt "Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?"

Similar presentations


Ads by Google