Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change.

Similar presentations


Presentation on theme: "Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change."— Presentation transcript:

1 Creating a class

2 Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change value i.e. they're variable. If a field is not variable, then you should exclude it, or make it a constant (public final static). Any static member belongs to the CLASS -- only one. A field is private. This is what is meant by encapsulation. You are hiding all but the 'public interface' (not to be confused with Interface). There is NO limit as to how many instances (objects) of a class you can have; and often you will have more than one.

3 getters and setters public type getField() public void setField(type typName) They are public! The form the 'public interface' of the class. Use Eclipse to generate them for you.

4 Constructors Constructors are optional. If your class is just a static driver; i.e. it just has a static main method, then no need to instantiate it; and no need for constructor. If your class is a "java bean" -- a class that models a real world object (noun) in your system, then it should have a constructor. House, Car, Person, etc. If you don't provide a constructor, a no-args constructor is provided automatically. You can, and often want, more than one constructor with different signatures. You call a constrcutor with the 'new' keyword.

5 Methods public non-static methods may be called from the implicit parameter of an object (once instantiated in memory space). public static methods may be called directly from the class -- WITHOUT the need to instantiate in memory. Example: public static void main(). Java doesn't instantiate your methods over and over, just the fields. They are compiled in the.class file, only once.

6 style conventions don't use variable/reference names like x,y, i, and j. That's very difficult to read. The exception is a counter, and even then, it should look like this: nC. cntrl-shift-f will format your code with indents. Use it. some programmers use underscore to denote fields; and you may as well. All contemporary IDEs highlight fields (in Eclipse they're blue).

7 style conventions Example: y verus dPrice? Example: name or strFullName Example: today or greToday Example countries or strCountries (append an s for data structures, the type should be first) Communicate meta-data! – prefix conveys type – prefix conveys variable or object reference (one and three letters in prefix respectively) – postFix 's' conveys data-structure or array

8 style conventions Make it up! just be consistent throughout that same class. Example: StringTokenizer stoDates If you just need a quick-and-dirty object reference defined locally inside a method, use str, gre, scn, etc. So long as it's not a keyword like int.

9 Model a system Object-oriented programming is about modelling a system. Write the problem out as requirements -- this will essentially be a math world problem (your favorite kind to solve!) Your computer objects map directly to real- world objects (nouns). Your methods map directly to real-world actions (verbs).

10 Write a very simple application for a fictitious real estate company that displays the houses it currently offers. Some of the data they'd like to display include; address, market_value, an image of the house, and if the house has been foreclosed. 1/ display all the houses to the console (use char[][] for image). 2/ Apply a 7% discount on all of our houses. Then display all of our houses to the console (use char[][] for image) 3/ Determine the most expensive house and display it to the console. All houses are by default for sale, and one they're sold, they are removed from the display list. There is plenty of other data like, weeks on market, the owners of the house, cube footage, etc. that we don't care about for our simple app.

11 Class Objects

12 Surprise

13 Exceptions How do you know if a method throws an exceptions? -- look at the javaDocs in the API Do I need to catch all exceptions? -- in a professional environment, yes. In this class, no. Will the compiler warn me about possible thrown exceptions? --sometimes. What's catch and throw? --see example...

14 A couple of homework probs YourDate and java library Date Reorder

15 418600 nVals 2031 nOrds [0] [1] [2] [3]

16 Reorder 418600 nVals 2310 nOrds [0] [1] [2] [3] int[] nResults = new int[nVals.length]; for (int nC = 0; nC < nResults.length; nC++) nResults[nOrds[nC]] = nVals[nC]; 0000 nResults [0] [1] [2] [3]

17 Reorder 418600 nVals 2310 nOrds [0] [1] [2] [3] nOrds[3] == 0, nVals[3] == 600. We need to assing 600 to the nVals[0] nSwap = nVals[0], so now nSwap is 4. nVals[nC] = nVals[nB], so 4, 18, -1, 4. Then nVals[nB] = nSwap. so 600, 18, -1, 4 do the same for orders. break. O(n^2)

18 Reorder (from hw1) int nSwapVal; for (int nB = 0; nB < nOrders.length; nB++) { for (int nC = 0; nC < nOrders.length; nC++) { if (nOrders[nC] == nB) { //swap vals nSwapVal = nVals[nC]; nVals[nC] = nVals[nB]; nVals[nB] = nSwapVal; //swap ords nSwapVal = nOrders[nC]; nOrders[nC] = nOrders[nB]; nOrders[nB] = nSwapVal; break; }

19 YourDate (from hw1)

20 Deep Copy

21 Auto-Boxing

22 Elipses

23 Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class. An interface is a contract; it defines the methods

24 Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class.

25


Download ppt "Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change."

Similar presentations


Ads by Google