I was trying to play around with Netbeans 7.0.1 yesterday with the imports and packages. I wanted to import a class that I had created earlier into a new project. It took me a lot of time to actually do this simple task because I was ignorant of the fact that I did not have much Netbeans experience. At first I tried to just create a jar file like normal and have the class I wanted in a default package. However, this turned out futile and useless as I was unable to import anything.
For example,
my java class is called classA and it wasnt' in any default package. When I created the jar I just went to the directory that classA was in and created the jar form there, say MyJar.jar. I then proceeded to import MyJar.jar into the netbeans library for the particular package. When I imported classA using import classA, it would not allow me to use it as it was not found.
After reading on several text, I decided to create a jar using directories and subdirectories instead of creating the jar on the class firsthand. I put classA into directories
org/test/
and changed the java file to allow and inserted the code " package org.test" on top
I then went into the directory above org, and proceeded to create a jar by using the following command:
jar -cvf MyJar.jar org
this would package everything neatly and when I imported into Netbenas the jar MyJar.jar, I was able to use classA by using the code
"import org.test.classA"
This works at the moment because I was able to change the source code of classA by putting it into a proper package. However, what would happen if a class file is all I am given and I do not have the source code? There should be some way in which I am able to add a class into the library in Netbeans so I can use it. I will edit this post more once I have found this method
Tuesday, September 20, 2011
Friday, September 16, 2011
Java Tidbits
There are some interesting tidbits about Java that a programmer might find useful to learn.
1) Instance variables will be initialized to default values if it is not set during the creation of an object. This might seem like a useless reminder but it's amazing the number of programmers who forget about this.
2) The for loop will allow java to access variables instantly (as of Java SE 6).
For example, instead of writing
int x = 0;
for ( x =0; x
1) Instance variables will be initialized to default values if it is not set during the creation of an object. This might seem like a useless reminder but it's amazing the number of programmers who forget about this.
2) The for loop will allow java to access variables instantly (as of Java SE 6).
For example, instead of writing
int x = 0;
for ( x =0; x
MyObject ob = new MyObject();
-- ob in this case will automatically call on to the toString method for MyObject. If that is not found, it will find the super() method, which in this case will be the Object's toString() method as every class directly inherits the Object Class.
Definitions Galore
In object oriented programming, just like in any other type of programming, there are several important definitions that a person must learn. This post will be updated frequently and definitions that I feel are necessary for a programmer willl be added here. I will try to make my own definitions based on my understanding and in some cases I will try to rely on definitions found on the web.
1) Instance variables :
Instance variables are variables that an object knows about itself. It is a variable defined in a class for which every object has a copy of. In java, instance variables will be set into their default values if they are not initialized when the object is created. For example, if int x is an instance variable, it will be set to 0 if x is not initialize during the objects creation.
1) Instance variables :
Instance variables are variables that an object knows about itself. It is a variable defined in a class for which every object has a copy of. In java, instance variables will be set into their default values if they are not initialized when the object is created. For example, if int x is an instance variable, it will be set to 0 if x is not initialize during the objects creation.
Object Oriented Programming
1) What is it?
2) How does it apply to me?
3) What is the benefits and cons of object oriented programming?
4) What are the other programming paradigms that can be used?
4) What are the other programming paradigms that can be used?
Saturday, September 10, 2011
JDK 1.7 and Netbeans 7.0.1
I was trying to use the switch case method with a string when an error came up:
error: strings in switch are not supported in -source 1.6
switch(name){
(use -source 7 or higher to enable strings in switch)
It was really weird as I have set everything to 1.7 in Netbeans, OR SO I THOUGHT.
I tested the class using the command line and got confirmation that it can be compiled perfectly. Hence, by powers of deduction I realized that netbeans was the problem in all of this.
I finally found the solution from the project configuration. Even though the tools -> Java version is set to 1.7, under project configuration -> properties -> sources, the source/binary format TAB must be set to 1.7. Otherwise all hell ( this might be an exaggeration) breaks loose and the errors will pop up saying you cannot compile it.
Stacks
Today I will be re-learning how to write a stack. A stack is a simple data structure that allows the first input to come out last. It is a First in Last out type of data structure. A more proper definition for a stack seems to be a Last In First Out type of data structure. In essence, a stack has two methods, pop and push. Push is used when data needs to be pushed into the stack and pop is used when data needs to be taken out of the stack. Basically, a stack can be viewed to something as a pancake stack. The last pancake that is put on top of the stack will be the first pancake that will be taken out and eaten.
I've been reading that creating algorithms before programming will tremendously help the coding. I think I will try that out today by writing algorithms before writing the stack code. In addition to that, it seems that writing the test code for the methods before it is finished is another method that will ensure good quality code. I think for the moment, I will just stick with writing the algorithm first. By algorithm, at this time I mean pseudo code. I haven't really written pseudo code in a long time so I will be googling most of the constants name.
Algorithms:
METHOD void push(Object A)
GET the object required to put into the stack.
CREATE Node aNode in which (Object A, Reference to Next Node)
IF stack is empty
PUT aNode the firstNode (Where firstNode will be the first node to be taken)
ELSE
PUT tempNode = firstNode
firstNode = aNode
aNode.reference_to_nextNode = tempNode
END METHOD
METHOD Object pop()
CREATE returnNode
PUT returnNode = firstNode
firstNode = firstNode.reference (This is to ensure that the first node is popped)
RETURN returnNode
The algorithms above for the stacks use a Linked List. I believe that using an array or soemthign similar will be easier but for learning purposes, I think I should use a linked list. At the moment, I cannot think of any negative side effects to using a Linked List over the Array. The only negative side effect that I could think of is that it will take longer to use as Linked Lists are of an object type whereas arrays could be simplified to contain simple primitives.
I have not made any test cases for these methods and I wasn't planning to do that in the beginning. However, after writing all the pseudocode out, I figured why not? If I'm going to do something, I might as well do it properly.
TEST Case
POP
1) ensure that popping a null object will throw an error?
PUSH
1) ensure that the object being pushed are all the same type. (Hence use generics)
THINGS I HAVE LEARNED:
Perhaps the Node Private clas should be created as a public class in which everyone can extend?
However, that would defy the OO basics in which a Stack is not a Node. It is a collection of nodes but it will not make sense to have the Stack class extend Nodes. So I guess, at the moment, I will just leave it as is and have a private Node class in each of the data structure I create.
Thursday, September 8, 2011
Generics
Generics are an important tool in Java. I have learned that with generics, the compiler can help error checking in case the programmer forgets to put the correct element in.
For example,
ArrayList al = new ArrayList();
al.add(Object o);
The above statements do not use generic and is quite valid. However,a programmer might be able to cause an error if the objects that he adds are not the same type. If at first an integer is added and then a string is added later on, the compiler would not be able to detect this and a runtime error will occur.
Using a generic,
ArrayList al = new ArrayList();
will ensure that every time the method al.add() is called, the parameters added has to be a String.
Thursday, September 1, 2011
Java Revision
It has literally been ages since I wrote a Java program. Today I am copying a code from a book I have been reading and relearned a few noobish albeit useful ideas.
1) variables in java has to be set or presumed to be set or the javac compiler wont run.
Monday, August 8, 2011
Java SE 7 and Netbeans 7.01
I have just downloaded and installed Java 7. Let's see how it goes. The classpath has all been set and hopefully I will be able to get back on Java and start developing as soon as possible.
Tuesday, August 2, 2011
Post Numero Uno
This is the first post for the Java Pew Pew blog. I intend to make this blog into a useful site where I post my experiences and steps in learning Java. I am not sure whether it will be beneficial to anyone but I hope to use this blog as my journal and diary while I learn and master Java programming.
As for the title Java Pew Pew, it is just a title that I made up on the spot as I had just finished playing a game and I figure why not. Let's hope by doing this constantly and updating what I have been learning in Java, I can become an extremely competent Java programmer.
Let's pew pew java then!!
Subscribe to:
Posts (Atom)