Java Coden, Day 2
Object Oriented Programming
sample code
The theory bits:
using object oriented programming typically doesn't gain you any optimizations, but
(in theory) it does make it easier to model and build programs that better match the
problems we're asked to solve with programming
- In java:
- CLASS = OBJECT
- METHOD = FUNCTION
- encapsulation
- hiding data and functions
- making the pieces of your code into a black box
- allows you to go back and re-work that code without breaking things because you've
(hopefully) hidden the details of how you actually did the work.
- Anything that's public in your classes IS NOT encapsulated, so don't expect
to be able to change it willy-nilly after you've started using it. (Or if you do,
it will probably be a LOT more work). If you really want to encapsulate something,
define it as private.
- inheritance
- sort of layers of an onion theory. You can tack on additional functionality or data
to a class without having to change the original class.
- polymorphism
- the ability to redefine methods in derived classes to match those new classes
- usually a pretty tricky undertaking unless you've thought about making this easy
to do when you were building your original classes
- a reasonable example is a printArticle method in our class Article. This allows us
to print the artcile to System.out (standard out, where-ever that points). If we
then created a new class, called BetterArticle, which contained some additional data
(like maybe an author listing or such), then we might want to redefine printArticle
to also print out the author listing with the article. Over-riding that method to
add in the printing of the author listing is using polymorphism.
The practical bits:
When you define a variable (other than a primitive data type), it's a reference.
You can't access non-static methods or data from it until you've created the object.
Creating a reference | Creating an object |
String wordlist; | String wordlist = new String("one motorcycle harry book"); |
| String wordlist = "one motorcycle harry book"; (implied NEW keyword) |
Arrays are objects. Defining int[][] int_matrix = new int[100][100];
doesn't create
those integers, it just creates a reference. If you want to use those integers, you have to explicity
create them in the array:
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
int_matrix[x][y] = 0;
}
}
If you don't do this, you'll find yourself looking at a "Null Pointer Exception" - that's when you try to
use an object that isn't there. You'll probably see a lot of these, it's a common and easy to make mistake.
Doin it in STYLE
- Run words together, except for constants
- Class names -- Capitalize first letter of each word: String, ArrayIndexOutOfBoundsException
- Method names -- Captalize first letter of each word, except first: replace(), equalsIgnoreCase()
- Constants -- Capitalize all letters; Separate individual words by underscores: MAX_VALUE, DECIMAL_DIGIT_NUMBER
Better yet, read the Ambysoft style guide.
CVS
Concurrent Versioning System: CVS
Network based RCCS system for version control on code. Does require some "set up" though. In our environment, we're using SSH as the transport mechanism. To utilize CVS you need to set a couple of environment variables.
In BASH,KSH,SH:
export CVS_RSH=/usr/local/bin/ssh
export CVSROOT=:ext:heckj@doc.missouri.edu:/usr/local/CVS
In TCSH,CSH:
setenv CVS_RSH /usr/local/bin/ssh
setenv CVSROOT :ext:heckj@doc.missouri.edu:/usr/local/CVS
A CVS Manual is available
at http://www.loria.fr/~molli/cvs/doc/cvs_toc.html.
CVS Commands:
cvs --help-commands
:
- gives you a quick summary listed of cvs commands available to you.
cvs co JavaCourse
:
- checks out the JavaCourse "tree" from CVS. Builds the tree
and it's appropriate filesets into the directory you're currently working in.
cvs status JavaCourse
:
- gives you the complete status of the CVS tree portion you've specified
- in this case "JavaCourse".
cvs update JavaCourse
:
- updates all the files from the CVS "tree" for the Directory JavaCourse
Looking at the CVS tree from the Web