Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Computer Science 2015/02/09. Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables.

Similar presentations


Presentation on theme: "AP Computer Science 2015/02/09. Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables."— Presentation transcript:

1 AP Computer Science 2015/02/09

2 Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables "shadow" class fields b.how this can be used to access a shadowed field 3.Explain: a.what are class invariants, and b.how they relate to constructors, accessors, and mutators 4.Code a class using class invariants.

3 Review: Visibility ●public: anything can access it ●private: only the class can access to it

4 Usual Visibility Class PartVisibility Constructors public Fields private Methods public

5 Usual Visibility: Exceptions ●Class constants opublic static final NUM_LINES = 7; ●"Helper" methods o Decomposing a task into smaller sub-tasks o The method/task is public o The "helper" methods/sub-tasks are private

6 Shadowing public class Sample { private int x; public Sample() { x = 2; } public void f(int x) { x = 3; }

7 Shadowing Exercise: 1.Create an accessor for class field x. 2.What is the value of the field x after f(3) is called? Is it 2 or 3?

8 Shadowing ●Answer: 2 ●Why? o Local variables "shadow" class fields. o Any reference to x refers to the local variable, not the class field.

9 Shadowing This is wrong: public class Sample { private int x; public Sample(int x) { x = x; }

10 Shadowing This is right: public class Sample { private int x; public Sample(int x) { this.x = x; }

11 Shadowing ● this.x will always give you the class field o (Except for static fields - more on that later) ●Even if a local variable is also named x

12 Class Invariants ●Definition of invariant: never changing ●Sample invariants o 2 + 2 = 4 o Death and taxes ●Sample non-invariants o Weather o High school relationships

13 Class Invariants ●A DiceRoller class has: o a field int lastRoll o an accessor int getLastRoll() o a mutator void rollDice() ●Invariant: lastRoll is a number from 1 to 6 o Does not matter how many times the dice are rolled ●You can make your own invariants!

14 Class Invariants Rules: 1.Invariant is true in the initial state a. Constructors establish the initial state 2.The invariant is true after a state change a. Mutators can change the state 3.Fields should be private a. Only mutators can change the state 4.(Accessors don't change the state)

15 Lab Create a Fraction class (starter code, tests provided) State: ●Numerator ●Denominator Invariants: 1.Fraction is always reduced 2.Denominator is always positive 3.Zero is not allowed as denominator

16 Exit Quiz, Homework For the Fraction class: 1.List the constructor(s). 2.List the accessor(s). 3.List the mutator(s). Homework: ●Wednesday: Self-checks 8.23-27 ●Monday: Fraction lab


Download ppt "AP Computer Science 2015/02/09. Learning Objectives 1.List the usual visibility for constructors, fields, and methods. 2.Explain: a.how local variables."

Similar presentations


Ads by Google