Quick Learn JAVA

Hour II. Data types and Loops
Java has several standard(primitive) data types, this hour will cover the two most common. Since String is actually an object, it will be covered in the fourth hour.
Example III.
import java.io.*;

class Candy {
  public static void main (String[] args) throws IOException {

    BufferedReader stdin = new BufferedReader
      (new InputStreamReader(System.in));
    int num1, num2, sum; // declares three integers
    double dollars; // declares a number that can have decimals

    System.out.print ("How many candy bars do you want  : ");
    System.out.flush();

    // read a line, and then converts it to an integer
    num1 = Integer.parseInt( stdin.readLine());

    System.out.print ("How many suckers you do want  : ");
    System.out.flush();
    num2 = Integer.parseInt( stdin.readLine());

    sum = num1 + num2; // Adds the two numbers;
    dollars = (double) sum * .75;
    System.out.println("You owe : $" + dollars);
  } // method main
}
Using (double) to convert the integer sum to a double, we can multiply by .75 to calculate the amount of money. This method of conversion using parentheses is called casting.

Java has several types of loops, the most useful being the for & while loops, which are both demonstrated in this next example. The standard if-then-else, will also be a very handy programming tool.

Example IV.
import java.io.*;

class Loopit {
  public static void main (String[] args) throws IOException {
    BufferedReader stdin = new BufferedReader
      (new InputStreamReader (System.in));
    int count, max, num;
    num = 0; // Assign initial value of count
    while (num != -1) {
      System.out.print ("Enter a number to factorialize (-1 to quit): ");
      System.out.flush();
      num = Integer.parseInt (stdin.readLine());
      max = 1; // Assign to 1, so factorial isn't zero every time
      if (num == -1) {
        System.out.println("Okay, quiting...");
      }
      else { // Since they're not quitting we better factorialize
        for  (count = 1; count<=num; count++) {
          max = count * max;
        }   
        System.out.println (num+"! (factorial) is : "+ max);
      }
    }
  } // method main
}
The first loop above is called a while loop, the syntax being :
      while (condition) { dosomething; }
The program runs what is in the brackets until the condition becomes false. For instance, the above program runs until the user enters negative one. Hence, "!=" mean not equal to.

Next is if, then and else. The syntax being :
      if (condition) { dosomething; }
      else { dosomethingdifferent; }
The above program compares (==) the number entered to negative one. If they are equal it tells the user the program is quitting, otherwise it factorializes the number using the for loop.

The syntax for a for loop is :
      for (initialization; condition; increment) { dosomething; }
In example four, the initialization, count = 1, assigns count to one. The increment, count++, adds one to the variable count until the condition, count<=num, becomes false. In other words, count is assigned one, two, three, ... num, with the inside of the loop being processed after each increment. Notice again that count++ increments by one; however, count+=2 would increment count by a factor of two. This is also true for count-- and count-=2. The former decreases by a factor of one, the latter by a factor of two.

No comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More