Friday, February 8, 2013

Excercise between W1 and W3

Week-3: 3.6: While


Another type of loop you can use in Java is called the while loop. While loops are a lot
easier to understand than for loops. Here's what they look like:

while ( condition ) {


So you start with the word "while" in lowercase. The condition you want to test for goes
between round brackets. A pair of curly brackets comes next, and the code you want to
execute goes between the curly brackets. As an example, here's a while loop that prints
out some text (Try the code out for yourself):

int loopVal = 0;

while ( loopVal < 5) {
System.out.println("Printing Some Text");

Visit For Details

Week-3: 3.5: For Loop


As we mentioned earlier, the programming you are doing now is sequential
programming. This means that flow is downward, from top to bottom, with every line of
code being executed, unless you tell Java otherwise.

You saw in the last section that one way to "tell" Java not to execute every line is by
using IF Statement to section off areas of code.

Another way to interrupt the flow from top to bottom is by using loops. A programming
loop is one that forces the program to go back up again. If it is forced back up again
you can execute lines of code repeatedly.

As an example, suppose you wanted to add up the numbers 1 to 10. You could do it quite
easily in Java like this:

int addition = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;

But you wouldn't really want to use that method if you needed to add up the numbers 1
to a 1000. Instead, you can use a loop to go over a line of code repeatedly until you've
reached 1000. Then you can exit the loop and continue on your way.

Visit for Details

Week-3: 3.4: Switch Statement


Another way to control the flow of your programs is with something called a switch
statement. A switch statement gives you the option to test for a range of values for
your variables. They can be used instead of long, complex if … else if statements. The
structure of the switch statement is this:

switch ( variable_to_test ) {
case value:
case value:


Visit for Details

Week-3 : 3.3: Boolean Statement


A Boolean value is one with two choices: true or false, yes or no, 1 or 0. In Java, there is
a variable type for Boolean values:

boolean user = true;

So instead of typing int or double or string, you just type boolean (with a lower case "b").
After the name of you variable, you can assign a value of either true or false. Notice
that the assignment operator is a single equals sign ( = ). If you want to check if a
variable "has a value of" something, you need two equal signs ( = =).

Try this simple code:

boolean user = true;

if ( user == true) {
System.out.println("it's true");
else {
System.out.println("it's false");

Visit for Details

Week-3: 3.2 : IF else

 If else:

Instead of using two IF Statements, you can use an IF … ELSE Statement instead. Here's
the structure of an IF … ELSE statement:

if ( condition_to_test ) {

else {


The first line starts with if, followed by the condition you want to test for. This goes
between two round brackets. Again, curly brackets are used to section off the different
choices. The second choice goes after the word else and between its own curly brackets.
Here's our code again that checks a user's age:

Visit for Details

Week-3: 3.1: IF statement

The programming you're doing now is sequential programming, meaning the code is
executed from top to bottom. It's very linear, in that each and every line of code will be
read, starting with the first line of code you write and ending at the last line.

But you don't always want your programmes to work like that. Often, you want code to
be executed only if certain conditions are met. For example, you might want one message
to display if a user is below the age of 18 and a different message if he or she is 18 or
older. You want to control the flow of the programme for yourself. You can do this with
conditional logic.

Conditional logic is mainly about the IF word: IF user is less than 18 then display this
message; IF user is 18 or older then display that message. Fortunately, it's very easy to
use conditional logic in Java. Let's start with IF Statements.

Visit for Details

Week-2: 2.7: Java Option Panes

Java Option Panes:

another useful class for accepting user input, and displaying results, is the JOptionPane class.
This is located in the javax.swing library. The JOptionPane class allows you to have input boxes
like this one:

Visit for Details:

Week-2: 2.6: Accepting Input from User

Accepting Input from User:

One of the strengths of Java is the huge libraries of code available to you. This is code that has
been written to do specific jobs. All you need to do is to reference which library you want to use,
and then call a method into action. One really useful class that handles input from a user is called
the Scanner class. The Scanner class can be found in the java.util library. To use the Scanner
class, you need to reference it in your code. This is done with the keyword import.

import java.util.Scanner;

The import statement needs to go just above the Class statement:
import java.util.Scanner;
public class StringVariables {

This tells java that you want to use a particular class in a particular library - the Scanner class,
which is located in the java.util library.
The next thing you need to do is to create an object from the Scanner class. (A class is just a
bunch of code. It doesn't do anything until you create a new object from it.)
To create a new Scanner object the code is this:

Scanner user_input = new Scanner( );

Visit for Details

Week-2: 2.5: String Variable

String Variable:
As well as storing number values, variables can hold text. You can store just one character, or
lots of characters. To store just one character, the char variable is used. Usually, though, you'll
want to store more than one character. To do so, you need the string variable type.

Start a new project for this by clicking File > New Project from the menu bar at the top of
NetBeans. When the New Project dialogue box appears, make sure Java and Java Application
are selected:

Visit for Details

Week-2: 2.4 : Operator Precedence

Operator Precedence:
You can, of course, calculate using more than two numbers in Java. But you need to take care of
what exactly is being calculated. Take the following as an example:
first_number = 100;
second_number = 75;
third_number = 25;
answer = first_number - second_number + third_number;

If you did the calculation left to right it would be 100 - 75, which is 25. Then add the third
number, which is 25. The total would be 50. However, what if you didn't mean that? What if
you wanted to add the second and third numbers together, and then deduct the total from the first
number? So 75 + 25, which is 100. Then deduct that from the first number, which is 100. The
total would now be 0.

To ensure that Java is doing what you want, you can use round brackets. So the first calculation
would be:

answer = (first_number - second_number) + third_number;

 Details Operator Precedence

Week-2: 2.3: Variable : Float and Short

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;

for Details

Week-2: 2.2: Variable : Double

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;

Visit for Details

Week-2: 2.1: Variable: Integer

Programs 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).

Click for Details

Tuesday, February 5, 2013

Week-1: 1.7 : Sharing your Java Programs

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:

Click for Details

Week-1: 1.6: Printing to the Output Window

You can run the code you have so far, and turn it into a program. 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:

Click for Details 

Week-1 : 1.5: Running Java Program

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: