Epnix.Com

A place to test and try things out

Browsing Posts in Java 5

Ok bad pun on Class development but that’s where I’m at in the current book.  I think I already have a follow-on book lined up for when I get done with this one.  It’s still a Intro style book, I tend to do that cause everybody has a different approach as to how to teach, what’s good foundation stuff and what’s not.  One example of this, the Head first book suggests using just a simple texted editor like notepad, instead of a IDE (integrated development environment) like NetBeans. The logic is your learn the IDE more then the language.  However the book I’m looking at reading next is the exact opposite.  It has whole sections dealing with how to use NetBeans to type out Java applications, Apps and so on. So yeah, in my opinion it’s always good to read though a couple of books.  The big down side though is sometimes you don’t want to pay to much attention when a topic is discussed that you think you already covered in the previous book.  This is tedious for sure, but it still needs to be read so you don’t miss the chance at some little gem of knowledge that you might otherwise not have known. 

Anyhow, back to the chapter at hand, They are discussing a methodology or approach to writing classes. Their task list goes:

  • Figure out what the class is suppose to do
  • List the variables and methods (Don’t forget to create methods to encapsulate your variables)
  • Write prepcode
  • Write test code
  • Implement the class
  • test
  • Debug and re-implement, repeat as much as needed.

This seems like a pretty straight forward and logic approach, which brings to mind a few questions (I know, I seem to question everything sometimes).The first and foremost is that I need a better method of deciding what classes I need, Ok not really a question as much as a observation.  Next is, that they, rightfully, say a programming workplace might have a different approach to writing java code.  I realize that everybody does things a little bit differently but really, how much could you change without totally throwing out any sort of quality control.  If you dropped test code and debug.  Ok almost went off on a rant without really knowing how it works in a real programming shop.  So if anybody would like to fill in the blanks as to how the real work flow for a java programming shop it would be cool to know.

More About Objects

No comments

Here is my Code:

class Horse {

int HairColor;
String Name;
}

public class RefObjTest {

public static void main (String [] args) {
Horse a = new Horse();
Horse b = new Horse();
Horse c = b;

System.out.println(“We have 3 Horses”);
a.Name = “A”;
b.Name = “B”;
System.out.println(“The first horse is ” + a.Name + “, the second is ” + b.Name + “, and the last ones’ name is ” + c.Name + “.”);
System.out.println(“Changeing Horse c’s Name”);
c.Name = “C”;
System.out.print(“The first horse is ” + a.Name + “, the second is ” + b.Name + “, and the last ones’ name is ” + c.Name + “.”);

}
}

And Here is my Out put.

We have 3 Horses
The first horse is A, the second is B, and the last ones’ name is B.
Changeing Horse c’s Name
The first horse is A, the second is C, and the last ones’ name is C.

The good news is, I can see what’s going to happen.  What I don’t understand is, Why would I want this?  If I’m looking at houses on the street, or horses in the barn, Seems like it would be really inefficient to create a full object for each one. Hmmm Well I suppose it could be just where I’m at in the book, maybe there’s a better way of doing things down the way as far as utilizing the classes that are made.

Ok, I’m going the spare you the Why this question comes to mind, but I’m still going to ask the question. Can Elephants Jump?  I just put that question into Facebook / twitter as this Chapter starts out talking about putting a Giraffe into a Rabbit variable.  and calling Rabbit hopper = new Giraffe(); .  So I thought yeah, might be interesting seeing a Giraffe hopping, but then thought oooh what would be even cooler was seeing a Elephant jump… If they could how much would the ground shake?  Ok Back to java.

Variables in themselves are pretty straight forward until they talk about Object reference variables.  I think I know what they mean, but the examples they are giving are not helping at all. So I’m stuck trying to sort though what is correct.  It’s these times that I wish I had a Instructor or at least a mentor that can say ok that example is causing more confusion, here look at it THIS way, until it finally make sense.  Or they could listen to what I’m thinking and say yep, you actually got it, ignore that example.

What I’m looking at is this.

Horse a = new Horse();
Horse b = new Horse();

To me, I’m looking at 2 instances of the object Horse.  One instance is called “a” one instance is called “b”. And lets say I sent some color Instance Variable on “a” to be red and on “b” it’s brown.  Let me Show the whole Code:

Horse a = new Horse();
Horse b = new Horse();

a.haircolor = red;
b.haircolor = brown;

So how I have 2 objects, that are instances of Horse.  According to the book I have 2 Horse Objects, and if I did:

Horse c = b;

To me I would now have 3 instances of the horse object with b and c being exact duplicates of each other.  According to this book though I only have 2 objects as b and c reference the same object.  The difference is, if both b and c are referencing the same object then if I make a change to a variable in b wouldn’t it also be changed in c?

Looks like time to let Google do some walking.

I’ve decided to name the learn Java posts based on the chapter titles.  I’m sure I could use the exact title in the title of the post, but there’s usually no need for that, just something close.

The book says when designing a object think about what it “Knows” and what it “does”.  For me it’s just easier to remember Variables and Methods.  I suppose I could mix their example with what works for me to get Variables and what it does.  It’s just a paradigm to help one remember what your looking for when creating a class.  The be correct, your thinking of what the Objects instance Variables and Methods are going to be.

In some ways I see java as being messy with all the files.  One file per class, a large program would have a lot of bits and pieces scattered all over the place.  Well yes, it would be all in one folder, and yes, it can be contained in a .jar file but still.  Of Course I’m looking at it from a solo developer point of view. Where you would have to work on all sorts of different files at any given time.  However in a team perspective, it makes more sense, as everybody could easily work on their with out locking out someone else from what that other person has to work on. Good news is keep all the files in one folder then Compile via wild card:

javac *.java

Then hope you didn’t have any errors.  I wonder if there is a command to redirect error output to a file.  I remember doing that way back when, in High School no less, when we had to learn Pascal.  Except instead of file, we dumped to a printer.  It was funny, you’d send the job and then listen for the printer.. If it only buzzed for a few lines then you had some small syntax error, maybe forgot to declare a variable or something If it printed pages of errors, you missed a semi-colon and didn’t even bother to look at the printout.

Well looks like I can

javac class.java –Xstdout filename

though in a option to put all the resultant class files in a different directory

javac class.java –Xstdout complemsg.txt –d finished files

That should make everything all neat and tidy.

Lessons in Java

No comments

I have a plan.. Ok maybe not so much of a plan as a lose set of possibilities. One of which is to make apps for the Blackberry.  The reasons are 2 fold.  One is to make any oddball app that I want, though I have to admit the Blackberry community is pretty good at covering that.  The other is to make some a little bit of money, if I can sell 100 copies of a app that people want at a $1 a piece, that’s still $100 bucks more then I started with.  Not to mention the fact that in most cases I probably want the app also.  It give me more programming experience, so I’ll be getting paid to learn. Finally anything that is a web technology is good for my current career choice.  Not a bad deal all the way around.

Of course there is a slight problem, part one of this many part problem is knowledge of the Java Programming language.  I’m a little lacking there.  So This is the beginning of Project file on learning Java.

What will be seen under this category and tags is my notes on learning the language and some of the pitfalls along the way.  I know they will be useful for me, but I also hope that they will be useful to anybody else that may be trying to learn the language at a future date.  I guess one more thing I should say is I’m not trying to teach the language here.  There are plenty of books out there and some websites that will do that.  Again, this is more my thoughts, notes, and “discoveries”

My posts and projects are a bit disjointed at the moment, I have this listed as Project 2 even though it will probably be posted before project one.  I will be bouncing around to give the brain a break on a particular topic, and even thought I started the server project a bit ago, Java is what I’m working on now.

What I’m using:

I personally learn best from various “teach yourself” books and the one I’m reading right now is called “Head First Java” Second Edition, by Kathy Sierra, and Bert Bates (ISBN 0596009208) (hmmm side note, I guess if I’m going to do that I should learn how to do a proper citation).  It’s a 2005 book, but I don’t see a 3rd edition at this time, and this one does seem to be well regarded, with good and recent reviews.  So I guess it’s a good place to start.

For Java, using JDK 6 update16, which btw this was the first sticking point in learning Java.  You can’t just go to Sun’s website and click on the java button.  That’s going to take you the version that you need to run apps.  To write programs you need the development tools. Personally there is to many choices at that link.  If all  your trying to do for now is learn java look for the block that’s labeled Java SE Development Kit (JDK).  Download that and install.  The Book recommends also downloading the documentation.  Which I did, but haven’t installed it yet.  After getting it installed I had to add to the windows Path statement where the JDK Bin files where.  When copying the path from windows explorer make sure to copy the path to the JDK, not the JRE that installs.  Yeah, Duh! I know, makes sense huh?  Well sometimes logic fails at 2am.. oops!

Today’s Notes:

Source file Contains one Class, Class Contains one or more Methods, One method contains a set of statements.

Basic “Hello World” Program:

class HelloWorldApp {
   public static void main (String[] args) {
      System.out.println(“Hello World!”);
   }
}

NOTE the “S” in String[] is capitalized. Along with System, but I had gotten that one right. (Book uses a different example)

Must have a “main” method in every app, but not in every class.  Apps can contain multiple Class files.

Syntax:

  • Statements end in Semicolon’s
  • Most white spaces don’t matter
  • Must declare type and name of variables (ie. int weight;)
  • Classes and methods must be defined with in curly braces { }
  • // Single Line Comments
  • /* Multiline comments
        are like this with the Asterisk slashes */

while (condition) {
   statement;
}

for (initialization; termination; increment) {
    Statement;
}

One thing I can’t seem to remember the order of creation.

  1. write / Save the .java file
  2. Comple .java file : javac appfile.java This makes the .class file
  3. Run the .class file: java appfile.