Baby Steps

In this post I will explain to you about variables and constants and how to use them.

Firstly, we will have to open the project from last week again. To do this you must click on the eclipse application and select the exact same workbench. Everything will open automatically once this is correctly selected.

Step 1. Declaration of constants and variables

The difference between constants and variables is that constants stay the same throughout your whole program and the reason we use them is so if they need to be changed at some point they can be changed in one place. Example, an interest rate on investments. This rate may change at some point but for the purpose of calculating the return rate on an investment this rate stays constant whereas the amount each person may invest changes.

In this same example, we can use a variable to store the amount invested because it varies from person to person.

The way you declare a variable in java depends on the type it is:

int                   (an integer)

double           (a decimal)

String             (a word or a sentence, more than one character)

char                (one character)

So to declare a variable all we have to type into eclipse is the type of variable and its name like this:

int number1;

Let’s now declare a constant integer. The syntax for this is:

final int NUMBER_2;

Each declaration must be closed with a semi-colon. To declare a constant, all we need to add is the word final before it. Did you notice how the names look different for the variable and the constant? This is for the purpose of distinguishing variables from constants later in your code. Variables are Lower-case unless they are made up of two words, for example ‘number’ or ‘twoNumbers’ are both correctly named for a variable. Constants are declared with capital letters and a different word can be shown by separating the words using a ‘_’, for example ‘NUMBER’ or ‘TWO_NUMBERS’.

Now we will declare a constant string and a constant character. Like this:

final String SENTENCE = “Hello World”;

final char LETTER = ‘a’;

Step 2. Using our variables

Create a new cl

Lets create three integer variables, name them num1, num2 and total.

Give num1 a value of 10 and num2 a value of 5.

Next we will add num1 and num2 to create give a value to total.

Then print out “10 plus 5 equals 15” using your variables.

Try to code this yourself and then compare to the code below to see if you did it right.

Capture4

The result:

Capture5

Doubles work in the same way, like this:

Capture6

Sometimes, we want to enter two decimal numbers and contain the answer in an integer, for example if the decimal parts aren’t important.

To do this, you need to parse (in other words, tell Eclipse to treat the doubles as integers), like this:

Capture7

Step 3: Printf() – formatting our Print lines

Most times when printing out the total of two doubles the compiler prints out too many zeroes at the end or just too many useless decimal places. This can be solved easily. Using printf() we can format the output anyway we want.

a printf statement usually looks something like this:

printf(“%c is a character”, ch);

There are two parts to a printf statement – the string, and the variables. The variables being used in the printf are mentioned in the quotation marks first, then in the variables part you are telling the printf statement which variable to use.

Lets take the program above as an example.

Capture8

In the printf statement, the doubles are represented with %f.

Similarly characters are represented as %c, Strings as %s and integers as %d.

As you can see, the variables used are printed out with 6 decimal places. This is useless in this type of calculation as we only need one decimal place.

So, we can format our doubles like this:

Capture8

As you can see, just by adding “.1” before the f, the decimal numbers are formatted to only show one decimal place.

You can also avail of functions such as “\n” (new line) and “\t” (tab) in your print f statements.

Next week we will look at if statements and for loops.

The first step

I know this is Java for beginners but I’m going to assume you have eclipse and Java 8 JDK installed.

In this post I will teach you how to set up your workspace and begin programming. The first application for most people is the “Hello World” application.

Part 1 – Setting up your workspace

Step 1. Open Eclipse. On opening the program it will prompt you to select a workspace. It’s up to you where you want that to be so just select the folder you want to keep your work in.

Step 2. On loading, the program has a welcome page, you can close that down. Go to the top of the page and select ‘file’ > ‘new’ > ‘Java project’. It will then prompt you to enter a name. You can call it anything, I will name it “BlogCoding”. Make sure this is what the window looks like.

Capture

Step 3. Click ‘finish’.

Step 4. In package explorer on the left, click on the file labeled ‘BlogCoding’ or whatever you named it, then right click on ‘src’. Select ‘new’ > ‘package’

Step 5. Name this package “beginnerCoding” (make sure the name starts with a lowercase letter!) and click finish.

Step 6. Next, you have to make a class so you can finally begin coding. Right click on the package named “beginnerCoding”, select ‘new’ > ‘class’ then set it up like the screenshot below and select ‘finish’.

Capture2

The great thing about Eclipse as your IDE is that you can make it insert your main method for you. So, before you even begin programming, you already have a main method.

Part 2 – Your first program

In this post I will teach you how to print a sentence to screen.

To do so all you need to type into Eclipse is:

system.out.println();

and put your sentence in between the brackets like so:

system.out.println(“Hello World”);

Now we run our program by pressing ‘shift’, ‘alt’, ‘X’ at the same time then ‘J’. Your screen should now look like this:

Capture3

The last tip I will include in this blog post is to ensure your brackets always look like above. Always put them on a new line and use indentation to when the bracket pair is inside another bracket pair. This is important for readability and makes everything seem cleaner and tidier.

Until next week!