[Java] GUI Console Output

Normally, if you write an application, to get the console output, you need to either load it in command propmt or write a batch file to have the Jar file launched with the command prompt window. This method might not work at some locations (ie. Comapny Shared Folder).

To solve this problem, we can create out own Output window in Java, and redirect  System.out to that window by overriding the methods.

Continue reading

[Java] ArrayList – sorting

Let’s say I am writing a program to keep track of amount of hours we spent on testing each engine, I need to utimately generate a report. It will be nice if I can sort the Engine based on it’s Serial number. So here’s the easy way to sort an ArrayList.

We sill use the method:

Collections.sort(list, comparator);

Continue reading

[Java] ArrayList

As I was writting the Voucher Reporting Tool for GE Aviation, the process of storing data in arrays became more and more tedious. Every time I wanted to add another engine, or remove a DAN #, I have to write all those codes to create a new array, filling it in, arranging it and such. Why can’t arrays be resized? Anyway, I turned to the ArrayList class, which I should have been a long time ago.

I guess it is similar to the Data Structure called Linked List, but all coded out by other devs and ready for use. Here’s a simple example.


import java.util.ArrayList;

public class Testing {

public static void main(String[] args) {

//Defining the Array List
ArrayList list = new ArrayList();

//Adding Elements in Order
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");

//Add Element at a certain Index
list.add(2, "Two and a Half");

//Revoce Element by Index Number
list.remove(list.size()-2);

//Remove Element by supplying the Object
list.remove("Five");

//Retrieve Elements in the Array List
for(int i = 0; i < list.size(); i ++)
System.out.println(list.get(i));

//Search for a certain Element Index
System.out.println("Two is at position: "+list.indexOf("Two"));

}
}

[Java] The String Class

Since we have been dealing with different types of variables, we might as well take a brief look at the String class. So let’s recall from last lesson, almost all of the primitive data types have to deal with numbers. It’s either variables that store whole numbers, or variables that store decimal numbers. The special one is char, you can store letters / symbols in it according to the ACSII code chart.

However, a char variable can only hold one character. What if you want to store a whole line? Like “I Love Cheese”? It is stupid if we have to do something like:


public class Calc {

    public static void main(String[] args) {

        char i = 'i';

        //Yes, you can define a bunch of variables of the same type using this method
        char l = 'l', o = 'o', v = 'v', e = 'e';

        char c = 'c';
        char h = 'h';
        char s = 's';

        char space = ' ';

        //Why do we have to use "" in front of all the char variables?
        //Why if we don't use "" it print out a number?
        //Does this have something to do with casting type?
        System.out.println(""+i+space+l+o+v+e+space+c+h+e+e+s+e);

    }

}

Yeah, that is definitely not the right way to do it. So here’s where the String class comes in. Continue reading

[Java] Variable Types

If you are currently like WTF, then you’ve done it. Didn’t I tell you not to try division? To put everyone on the same page, let’s edit in the following.


public class Calc {

    public static void main(String[] args) {

       int a = 3;
       int b = 2;
       int result = a/b;

        System.out.print("3/2 = ");
        System.out.println(result);

    }

}

And you will be surprised to find the result to be:

3/2 = 1

Continue reading

[Java] Simple Calculations

Now we know how to display messages in Java, it’s cool and all, but let’s try to do something a bit more useful. The first computer was a calculator after all, so let’s try to write a program to calculate something.

Create a new project with the main class call Calc, with file name Calc.java, and type the following in the source codes.

public class Calc {

    public static void main(String[] args) {

        /* Oh by the way, this is how you type
         * multi-line comments.
         */

        System.out.print("(5+6)*8-1 = ");
        System.out.println((5+6)*8-1);

    }

}

So, read the source codes above, as you probably figured out by now, this will “print” something to the console output. The question is what will it print? Continue reading

[Java] Hello World!

You’ve probably heard of the famous Hello World program in the past. Why is Hello World so famous? Well, it is usually the first thing that each programmer write in a programming language. It demonstrate the simplest ability to display text.

So what are we waiting for? Let’s dive in!

If you installed Netbeans, and follow the last lesson, you should have a HelloWorld class created for you with a template. See all those grayed out text? Those are comments, they don’t get compiled, they are just ignored by the compiler. Delete all of them for now, you should have something like below.


public class HelloWorld {

    public static void main(String[] args) {

    }

}

Continue reading

[Java] Intro to Netbeans IDE

I will assume you all are smart enough to figure out how to install the SDK and the Netbeans IDE. If you chose another IDE, good for you! Move on to the next lesson! This lesson is dedicated to the basics of Netbeans. Well, at least I will show you how to set up a project and test run it.

Launch Netbeans if you haven’t done so already. You should get something like this:

Continue reading

[Java] Getting Started…

In order to write software in Java, you will need the Java Software Development Kit (SDK), which can be aquired freely from the internet. The SDK contains libraries, compiler, documentations and a bunch of other stuffs.

Java SDK, that’s pretty much the only requirement to start writing programs. However, to make our lives easier when writing, and to compile, we will want to use an Integrated Development Environment (IDE). An IDE is a software that have features that help you program, compile, build and test run the program that you just wrote. Many IDE have error detection that will help you make your program work. Trust me, just go with it. You don’t want to write your whole program in Notepad, compile it in Command Line, take all those effort and just have it crash at the end not knowing what went wrong. Continue reading