Saturday, January 26, 2013

1/26/13 Inheritance

Sometimes you are coding something using multiple classes, and you find yourself writing the same code for each of those classes. Did you know that you can make one class inherit methods and variables from another? This will allow you to create a class that contains everything that another class dose plus more! To make one class Inherit another, you would type the following after the class name and before the open brace:
extends supperclass
where supperclass is the class that you want to inherit. This will give your class access to all the methods in supperclass as well as giving a copy of all of the private variables that you can find in supperclass. If you want to run a constructor for supperclass inside of your class, you can just type:
supper(arguments);
Is this still confusing? If so, I will give you and example:
public class one{
 private int number;
 private boolean isTrue;

 public one(){
  number=0;
  isTrue=false;
 }

 public one(int num, boolean truth){
  number=num;
  isTrue=truth;
 }

 public int test(int i){
  if(isTrue){
   return number*i;
  }
  return number/i;
 }
}

public class two extends one{
 private string word;

 public two(){
  supper();
  word = "";
 }
 public two(int num, boolean truth, String str){
  supper(num, truth);
  word = str;
 }
 public int run(){
  return test(word.length);
 }
}

This code here is a very basic example for how to create a class that inherits another. As you can tell, this can be used to do a lot of things. If you do not fully understand what the relationship is between a supper and sub class, you can think of it like cars. A Honda accord would be a sub class of Honda, which in turn is a sub class of car. This would make Honda a supper class of the Honda accord and car a supper class of Honda. As this may infer, you can have two or more sub classes for one supper class. As always, I hope you learned a thing or two about Inheritance and java.

Saturday, January 19, 2013

1/19/13 Eclipse

To this point in time, I have tough you how to use java, but you may be wondering, what software should I use to program in java? There are a lot of options out there to chose from, but I ultimately think that Eclipse is the best.

Eclipse is an open source software that runs java code. Unlike programs like Jgrasp, Eclipse is a lot more user friendly. While you are writing code, Eclipse is compiling it for you, and underlining your compile errors in red (much like Microsoft word does). Not only will it tell you exactly where your error is, but if you hover over the error, you will get a list of options that would fix it. Now, you don't even need to code the correction, just by clicking on the correction that you want to make, Eclipse will fix it for you! Speaking of fixing things, if you hit ctrl + shift + f, eclipse will auto format your code so that it looks nice! In the middle of calling a method? Eclipse will create a drop down list of all the programmed methods that start with what you have entered so far. Tired of typing all those set and get methods? Just go to source, and hit generate Getters and Setters, and Eclipse can do that too! Don't want to type your import statements? Just hit ctrl + shift + o, and Eclipse will do that for you. There are so many more ways to save time by using eclipse, but I will not list them here. Another useful tool is the ability to refactor your programs name. In all, I believe that Eclipse is the best platform to use when programming in java.

The one problem with Eclipse is that it is slow. Because it is constantly compiling, the computer may start lagging a little while you are writing code. Some of Eclipse's short cuts can also start getting in your way when you are trying to code things for yourself. If you want an efficient and simple platform for java, I recommend using Jgrasp. I hope that I have given you enough information to determine what platform you would like to use while programming, and if you have another platform that you use, just let me know by posting it in the comments.

Saturday, January 12, 2013

1/12/13 Array Lists

A few posts ago, you learned about Arrays. The one major disadvantage to using an array is that it has a fixed size, so you need to know how many elements you will need before you create it. An arraylist is basically an array that's size can fluctuate as you use it. In this post, I will teach you how to do basic functions with arraylists.

To start of, you will need to create an arraylist. To do this, you would type:

Arraylist <Type> name = new Arraylist<Type>();

Where name is its name, and type is the data type that you want to use. When entering your data type, you will need to capitalize it . This is because you are filling the arraylist with an object that acts as the primitive data type that it represents (for an int, the object is called Integer). Fortunately, these objects can be used just like you use the primitive data type that they represent. This constructor will create an arraylist that has 10 indexes, all filled with their default value. To delete an index, you would type:

name.remove(index);

This would remove the index index from the arraylist name. After it is removed, all the other indices shift down to fill in the gap. If you are trying to go through every index in an array and delete some of them, you would need to re-check the deleted index, because the next element has shifted to that index. To add a value to the arraylist, you would type:

name.add(index, object);

This will add object at index index. If you just put object inside of the parentheses, it will be put in index 0. This will cause all the latter indices to shift up one to make space for the new one. Now that you know how to add and remove indices, you will need to return elements from your arraylist. To do this, you would type:

name.get(index);

This will return the index index from arraylist name. As you may think, to set the value of an index, you would type:

name.set(index);

There is one more arraylist method that I will teach you, the size method. Unlike other things, arraylists use a size method instead of a length one. To find the size of an arraylist, you would type:

name.size();

I hope that you learned a thing or two about java, and feel free to ask me if you have any questions about anything that I posted in any of my articles!