Java programming 101

So here I am, in class hearing about "Object Technology in Java" and working on some of the examples. Thought I'd share and maybe explain a bit about java in the process.

[start code]

// First I need to import some predefined libraries that allow me to format text for output, I do this by doing the following.

import java.text.* ;

//Next I create a class called BankAccount with has all the methods I need for my very simplistic bank account.

class BankAccount

{



// now for some variables for our bank account class

double balance; // This will hold the bank account balance

String account; // Our account number

String name; // Name person who owns the account



/*Define format style show doubles when printed look like 1,000.00. If I didn't do this,

it would print out like 1000.0 and not be how the 2 decimal places */

DecimalFormat fmt = new DecimalFormat( "#,##0.00" ) ;



// Here is our deposit method/function. All it requires is the amount of money to be deposited

void deposit(double depositAmount)

{

balance += depositAmount; // Adds the deposited amount with the current balance

System.out.println ("You deposited: $" + fmt.format(depositAmount)); // print the new balance

}



// Withdraw method, specify the amount you want to withdraw from the balance

void withdraw (double withdrawAmount)

{

balance -= withdrawAmount; // subtracts the amount from the balance

System.out.println ("You withdrew: $" + fmt.format(withdrawAmount)); // prints new balance

}



// Sometimes you just want to print the balance... here is a method that does just that.

void printBalance()

{

System.out.println ("Total amount in your account is is: $" + fmt.format(balance));

}

}

// Here is where we define the Bank class.

class Bank

{

// Main always gets run when the program is executed, its a static method.

public static void main(String[] args)

{

/* Create an instance out of the BankAccount class called myAccount, I can then use the methods in

BankAccount via the object myAccount

*/

BankAccount myAccount = new BankAccount();

// use the methods to set some values and print them off.

myAccount.balance = 5000;

myAccount.printBalance();

myAccount.deposit(100000);

myAccount.printBalance();

myAccount.withdraw(25.15);

myAccount.printBalance();

}

}

[end code]

Here is the results when you run the above program, just so you can get a visual:

[start output]

Total amount in your account is is: $5,000.00

You deposited: $100,000.00

Total amount in your account is is: $105,000.00

You withdrew: $25.15

Total amount in your account is is: $104,974.85

[end output]