Friday, January 25, 2013

Java Control Structures Part 2-2



Programming Example: Checking Account Balance

This program calculates a customer’s checking account balance at the end of the month.  The input to this program is a file consisting of the customer’s account number, account balance at the beginning of the month, and each transaction (type and amount) made by the customer.  The transactions can be withdrawals (subtracted from balance), interest (added to balance), and deposits (added to balance). The data is in a specific format.

The output consists of the account number, beginning balance, ending balance, total interest paid, total amount deposited and number of deposits made, total amount withdrawn and number of withdrawals made.  The output must also be in the specified format.

The solution to this example consists of declaring and initializing variables and objects.  A Scanner object and Filereader object are used to read in data from the file based on the format.  Once the transaction codes are read in, a switch statement is used to determine what the code does and whether the amount it is associated with should be added or subtracted from the balance. An EOF-controlled while loop is used to make sure all the data is read in and put through the switch statement. Finally, the output is printed to a file using a PrintWriter object.




Programming Example: Fibonacci Number

A Fibonacci number, part of a Fibonacci sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, …

can be found using the following formula:

an = an-1 + an-2

This program determines the nth Fibonacci number given the first two.

The input to this program is first two Fibonacci numbers and the position in the sequence of the desired Fibonacci number (n).

The output to this program is the nth Fibonacci number.

The solution consists of prompting the user to enter Fibonacci number 1, Fibonacci number 2, and the n, so that Fibonacci number n can be calculated.  The first Fibonacci number is stored in the variable previous1, the second Fibonacci number is stored in the variable previous2, and the n is stored in the variable nth Fibonacci.

If…else statements are used to check if the first or second Fibonacci number is the desired number, if it isn’t the following while loop is entered to determine the number in the nth position:

counter = 3; //you already know the first and second number so you are starting with the third
while(counter <= nthFibonacci)
    current = previous2 + previous1;
    previous1 = previous2;
    previous2 = current;
    counter++;
}

The final answer will be stored in current. 


The for Looping (Repetition) Structure

The Java for looping structure is a specialized form of the while loop. Its primary purpose is to simplify the writing of counter-controlled loops. For this reason, the for loop is typically called a counted or indexed forloop.


The general form of the for statement is:

for (initial statement; loop condition; update statement)
   statement

The initial statement, loop condition, and update statement are called for loop control statements.

The for loop executes as follows:

1.      The initial statement executes
2.      The loop condition is evaluated.  If the loop condition evaluates to true:
a.       Execute the for loop statement.
b.      Execute the update statement.
3.      Repeat Step 2 until the loop condition evaluates to false.

The initial statement usually initializes a variable (called the for loop control, or indexed variable).

Some additional comments on for loops follow:

  • If the loop condition is initially false, the loop body does not execute.

  • The update expression, when executed, changes the value of the loop control variable (initialized by the initial expression), which eventually sets the value of the loop condition to false. The for loop executes indefinitely if the loop condition is always true.

  • If you put a semicolon at the end of a for statement (just before the body of the loop), the action of the for loop is empty.

  • In a for statement, if the loop condition is omitted, it is assumed to be true.

  • In a for statement, you can omit all three statements—initial statement, loop condition, and update statement. The following is a legal for loop:

for (;;)
                            System.out.println("Hello");

This is an infinite for loop, continuously printing the world Hello.


Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678




No comments:

Post a Comment