Posts

Showing posts from October, 2011

JAVA-10 Short and Float Variables

Image
Two more variable types you can use are short and float . The short variable type is used to store smaller number, and its range is between minus 32,768 and plus 32,767. Instead of using int in our code on the previous pages, we could have used short instead. You should only use short if you're sure that the values that you want to hold don't go above 32, 767 or below -32,768. The double value we used can store really big numbers of the floating point variety. Instead of using double, float can be used. When storing a value in a float variable, you need the letter "f" at the end. Like this: float first_number, second_number, answer; first_number = 10.5f; second_number = 20.8f; So the letter "f" goes after the number but before the semicolon at the end of the line. To see the difference between float and double,

JAVA-09 The Double Variable

Image
The double variable can hold very large (or small) numbers. The maximum and minimum values are 17 followed by 307 zeros. The double variable is also used to hold floating point values. A floating point value is one like 8.7, 12.5, 10.1. In other words, it has a "point something" at the end. If you try to store a floating point value in an int variable, NetBeans will underline the faulty code. If you try to run the programme, the compiler will throw up an error message. Let's get some practise using doubles. Change the int from your previous code to double. So change this: int first_number, second_number, answer; to this: double first_number, second_number, answer; Now change the values being stored: first_number = 10.5; second_number = 20.8; Leave the rest of the programme as it is. Your coding window should look l

JAVA-08 Java Variables

Image
Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides. The data is given a name, so that it can be re-called whenever it is need. The name, and its value, is known as a Variable. We'll start with number values. To store a number in java, you have lots of options. Whole numbers such as 8, 10, 12, etc, are stored using the int variable. (The int stands for integer.) Floating point numbers like 8.4, 10.5, 12.8, etc, are stored using the double variable. You do the storing with an equals sign ( = ). Let's look at some examples (You can use your FirstProject code for these examples). To set up a whole number (integer), add the following to the main method of your project from the previous section : public static void main ( String[ ] args ) { int firs

JAVA-07 Sharing your Java Programmes

Image
You can send your programmes to other people so that they can run them. To do that, you need to create a JAR file (Java Archive). NetBeans can do all this for you. From the Run menu at the top, select Clean and Build Main Project . When you do, NetBeans saves your work and then creates all the necessary files. It will create a folder called dist and place all the files in there. Have a look in the place where your NetBeans projects are and you'll see the dist folder: Double click the dist folder to see what's inside of it: You should see a JAR file and README text file. The text file contains instructions on how to run the programme from a terminal/console window. Now that you know how to run your java source files, let's do some programming.

JAVA-06 Printing to the Output Window

Image
You can run the code you have so far , and turn it into a programme. It doesn't do anything, but it will still compile. So let's add one line of code just so that we can see how it works. We'll output some text to a console window. Add the following line to your main method: public static void main( String[ ] args ) {        System.out.println( " My First Project " ); } When you type the full stop after "System", NetBeans will try to help you by displaying a list of available options: Double click out to add it to your code, then type another full stop. Again, the list of options appears:   Select println ( ). What this does is to print a line of text to the output screen. But you need to place your text between the round brackets of println . Your text needs to go between a pair of double quotes:  Once you have your double quotes in pla

JAVA-05 Running your Java Programmes

Image
When you run a programme in NetBeans, it will run in the Output window at the bottom of your screen, just underneath your code. This is so that you don't have to start a terminal or console window - the Output window IS the console. There are various ways to run your programme in NetBeans. The easiest way is to press F6 on your Keyboard. You can also run programmes using the menus as the top of NetBeans. Locate the Run menu, then select Run Main Programme : You can also click the green arrow on the NetBeans toolbar:  Another way to run your programmes is from the Projects window. This will ensure that the right source code is being run. Simply right click your java source file in the projects window and you'll see a menu appear. Select Run File . Using one of the above methods, run your programme. You should see something happening in the Output window: The second line