Category Archives: Java

The For Loop

Have you ever wanted to make a program that will count to 10!! Probably not, but if you did you could use a for loop. A for loop is used to repeat repetitive task, but one may ask why not use a while loop? The difference is that a for loop can count. By this I mean that if you tell the for loop to stop after 7 times it will. So lets look at a for loop and break it down.

Screen Shot 2013-02-20 at 12.21.18 AM

Now that we undertand the components of a for loop, lets take a look at an example

Screen Shot 2013-02-17 at 3.38.29 PM

This for loop starts off by creating an int that we set equal to one, we set it to one rather than zero because we dont want the program to out put [0,1,2,3,..] we want [1,2,3,…]. Next we set our condition, the condition is checked every time the loop runs and if in this case x is greater than 7 the for loop will quit running. Lastly each time the for loop runs we add 1 to x. With all of this set we output x on each loop. This creates a counting effect and you can see that the output counts from 1 to howMany. howMany is our variable that is checked against, so on the 7th loop  will be equal to 8 therefore the loop quits because howMany> x.

The for loop is very helpful when repetitive task are necessary. While not seen in this example the for loop can be used to complete very complex task, such a alphabetising names and even solving complex equations.

Thanks for reading email me at ccapp.dev@gmail.com if you have any questions

Java Scanner Tutorial (Area of A Square)

Have you ever logged on to a computer, posted a status update or typed in a number on your calculator? Most of you reading this have, in our modern world of technology user input plays a huge role. User input is what allows us as programs to interact with our user. Today we will learn how to write a program that will prompt a user for information. Although quite simple this program will introduce you to the idea of user input. Lets Get Started!!!

Step 1) Go ahead and open your favorite Java IDE(Integrated Development Environment)

Step 2) Create a new Java Project (Im using NetBeans by the way!)
Screen Shot 2013-02-17 at 12.04.14 AMScreen Shot 2013-02-17 at 12.04.05 AM

Step 3) Give your project a name

Screen Shot 2013-02-17 at 12.04.29 AM

Step 4) Open the Java file (Area.java)

Screen Shot 2013-02-17 at 12.04.50 AMScreen Shot 2013-02-17 at 12.04.57 AM

Step 5) Lets start coding

Screen Shot 2013-02-17 at 12.07.45 AM

The java.util.scanner is what allows the user to send information from his/her keyboard to the program. In a sense it links the physical and virtual world. To use this feature we must import it. We do this by typing “import” and then specify the class that we want to use. Java has hundreds of these classes that do a range of things from graphics to sound, all together its called the library.

Step 6) Start in the Main

Screen Shot 2013-02-17 at 12.09.40 AM

This program allows a user to input the side lengths of a square. Then the program will diagram the square and compute its area. Once the user inputs the side length we need a place to store that number, so we create a variable. A variable is simply a place to store information. So if you were a computer, then you may have a variable called first name. And inside the first name variable the word “Carter”. This is in a sense how variables work, this also allows us to call upon the variables value in the future. So here we set up a variable for the side length and area.

Step 6) Prompt the user

Screen Shot 2013-02-17 at 12.32.40 AM

This bit of code above creates the backbone of the scanner set up. The first line is how we instantiate the scanner. In plain terms we are telling the computer that we are making a new scanner called “keyboard”. Next we print out on the screen a prompt, by using System.out.print instead of System.out.println we can make a prompt. The last line of code sets the value of the next int that is typed on the keyboard to the variable side.

Step 7) Draw the Square

Screen Shot 2013-02-17 at 12.15.05 AM

The for loop above is what draws the square. The logic within for loops can be tricky sometimes so please look at the for loop tutorial if you need some assistance.

Step 8) Give them the answer!!

Screen Shot 2013-02-17 at 12.16.29 AM

After the for loop we compute the area of the square (s^2 = area). We then take this newlwy stored information and print it on the screen with our “System.out.println” method.

Step 9) Run The code!!!!!

If you had any trouble you can find the full source code here (http://pastebin.com/8YKysJev)