Скачать презентацию Creating a class Fields instance variables Скачать презентацию Creating a class Fields instance variables

14a1e96b5117e5e3ab91065da252ea2c.ppt

  • Количество слайдов: 21

Creating a class Creating a class

Fields (instance variables) • Fields are the data defined in the class and belonging 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.

getters and setters • public type get. Field() • public void set. Field(type typ. getters and setters • public type get. Field() • public void set. Field(type typ. Name) • They are public! The form the 'public interface' of the class. • Use Eclipse to generate them for you.

Constructors • Constructors are optional. If your class is just a static driver; i. 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.

Methods • public non-static methods may be called from the implicit parameter of an 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.

style conventions • don't use variable/reference names like x, y, i, and j. That's 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: n. C. • 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).

style conventions Example: y verus d. Price? Example: name or str. Full. Name Example: style conventions Example: y verus d. Price? Example: name or str. Full. Name Example: today or gre. Today Example countries or str. Countries (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) – post. Fix 's' conveys data-structure or array

style conventions • Make it up! just be consistent throughout that same class. • style conventions • Make it up! just be consistent throughout that same class. • Example: String. Tokenizer sto. Dates • 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.

Model a system • Object-oriented programming is about modeling a system. • Write the Model a system • Object-oriented programming is about modeling 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 realworld objects (nouns). • Your methods map directly to real-world actions (verbs).

Write a very simple application for a fictitious real estate company that displays the 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.

Class Objects Class Objects

Exceptions • How do you know if a method throws an exceptions? -- look Exceptions • How do you know if a method throws an exceptions? -- look at the java. Docs 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? --If it's a checked exception, then yes. • What's catch and throw? --see example. . .

A couple of homework probs • Your. Date and java library Date • Reorder A couple of homework probs • Your. Date and java library Date • Reorder

Reorder 4 n. Ords -1 [0] n. Vals 18 600 [1] [2] [3] 2 Reorder 4 n. Ords -1 [0] n. Vals 18 600 [1] [2] [3] 2 0 3 1 [0] [1] [2] [3]

Reorder 4 n. Results [1] [2] [3] 2 3 1 0 [0] [1] [2] Reorder 4 n. Results [1] [2] [3] 2 3 1 0 [0] [1] [2] [3] 0 n. Ords -1 [0] n. Vals 18 600 0 [0] [1] [2] int[] n. Results = new int[n. Vals. length]; for (int n. C = 0; n. C < n. Results. length; n. C++) n. Results[n. Ords[n. C]] = n. Vals[n. C]; [3]

Reorder 4 [1] [2] [3] 2 3 1 0 [0] n. Ords -1 [0] Reorder 4 [1] [2] [3] 2 3 1 0 [0] n. Ords -1 [0] n. Vals 18 600 [1] [2] [3] n. Ords[3] == 0, n. Vals[3] == 600. We need to assing 600 to the n. Vals[0] n. Swap = n. Vals[0], so now n. Swap is 4. n. Vals[n. C] = n. Vals[n. B], so 4, 18, -1, 4. Then n. Vals[n. B] = n. Swap. so 600, 18, -1, 4 do the same for orders. break. O(n^2)

Reorder (from hw 1) int n. Swap. Val; for (int n. B = 0; Reorder (from hw 1) int n. Swap. Val; for (int n. B = 0; n. B < n. Orders. length; n. B++) { for (int n. C = 0; n. C < n. Orders. length; n. C++) { if (n. Orders[n. C] == n. B) { //swap vals n. Swap. Val = n. Vals[n. C]; n. Vals[n. C] = n. Vals[n. B]; n. Vals[n. B] = n. Swap. Val; //swap ords n. Swap. Val = n. Orders[n. C]; n. Orders[n. C] = n. Orders[n. B]; n. Orders[n. B] = n. Swap. Val; } } } break;

Your. Date (from hw 1) Your. Date (from hw 1)

Deep Copy Deep Copy

Auto-Boxing Auto-Boxing

Elipses Elipses