First Java Program for Anyone

by JoshuaB291 in Circuits > Computers

631 Views, 6 Favorites, 0 Comments

First Java Program for Anyone

java.png

This simple intructable will give you a quick look at what program is like. It is very basic and easy to follow, so don't be afraid to click this, and learn a little. Maybe you will find that this is something you love!

Go to This Website

Address.PNG

There is usually a lot more to programming, but the simplest way to start is using an IDE (Integrated development environment), for this small project, we will use a simple online developer.

This Is What the Page Should Look Like

Start Page.PNG

The important parts on this page are the coding area and the output. The area labeled "your code" is where you type out the program. The black area is known as the console. It is where your program will put output. The blue button that says "execute" will compile then run the program, feel free to push it now and see what the console outputs. Can you see why the output is what it is?

Hello World

Hello World.PNG

This is the first program that every programmer writes: the famous, "Hello World." This program simply outputs "Hello World" to the console. Just copy my picture into the code area and watch as it runs. A few things I will point out: the the System.out.println(string) prints out a string to the console. A string is a variable type that means words; there is also "int" for integer, "bool" for Boolean (ie true or false), and many other variable types.

Adding a Little More

name.PNG

In this step we will mess around with adding another string and concatenating it to the output. The "+" symbol is used to concatenate, in the system.out.println we are concatenating a string and two string variables. Notice the "\n" before the string, this is called a return, it tells the program to go to a new line, similar to if the enter key was pressed.

Numbers

Number.PNG

In this step we will mess with a int variable. Int variables hold numbers, printing the variable allows the user to output many different things, with one variable. Notice using another system.out.println will also return the output to a new line.

Counting

100count.PNG

Now lets say we wanted to make the program count from 1 to 100, this program does that, but when you run it, all you see is the "100." can you see why? The reason for this is because the program first counts, then outputs what the variable is, so the program loops until the variable x is equal to 100, then moves on to print the output.

Counting Fixed

2-10.PNG

Alright lets move the print into the loop, and only count to 10 so that the output doesn't get to filled up. Now when we run the program you will notice that it outputs all the numbers 2 - 10 missing 1. The reason for this is because x is already incremented once before being output. lets fix this in the next step, feel free to see if you can before moving on.

Counting 1 Thru 10

1-10.PNG

This is an example of just one way to fix the program. If you got it working on your own, congrats! printing before we increment allows the variable to be 1 and print, then increment. If you where to run it just making that change you would see it only print 1 - 9, so putting in the "=" in the while loop allows the program to run 1 last time once the variable is at 10.

If Statements

1-9.PNG

This change makes the program only print when x is an odd number. The math behind this is fairly simple. Taking the variable and applying mod (%) 2 will return a 0 if the number is even, and a 1 if the number is odd. This is because mod works by dividing the number and returning the remainder, any even number you divide by 2 has no remainder, and any odd number would have a 1 remainder. The exclamation point "!" stands for not, so != is read as "not equal." Thus, when the variable x mod 2 does not return a 0, or is odd, print the variable.

Go Crazy

That is all for this small little example, hopefully you found it somewhat entertaining, and maybe even had some fun! As you can tell there is a large difference from this simple programming to the large programs we use on a daily basis. Feel free to have some fun on this website, see what you can create, and go crazy with it!