Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging...

21
Creating a class

Transcript of Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging...

Creating a class

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 getField() • public void setField(type typName)

• 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.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 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 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).

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

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.

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 real-world 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 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

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? --If it's a checked exception, then yes.

• What's catch and throw? --see example...

A couple of homework probs

• YourDate and java library Date• Reorder

Reorder

4 18 -1 600

nVals2 0 3 1

nOrds [0] [1] [2] [3]

[0] [1] [2] [3]

Reorder

4 18 -1 600

nVals2 3 1 0

nOrds [0] [1] [2] [3]

[0] [1] [2] [3]

int[] nResults = new int[nVals.length];for (int nC = 0; nC < nResults.length; nC++)

nResults[nOrds[nC]] = nVals[nC];

0 0 0 0

nResults [0] [1] [2] [3]

Reorder

4 18 -1 600

nVals2 3 1 0

nOrds [0] [1] [2] [3]

[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)

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 valsnSwapVal = nVals[nC]; nVals[nC] = nVals[nB];nVals[nB] = nSwapVal;

//swap ordsnSwapVal = nOrders[nC]; nOrders[nC] = nOrders[nB];nOrders[nB] = nSwapVal;

break; }}

}

YourDate (from hw1)

Deep Copy

Auto-Boxing

Elipses