Hour IV. Arrays, Objects, Classes and Methods
The manipulation of objects and arrays, complemented with the use of several classes and methods will define you as a true java programmer, so take good notes!
Example VI.
class Bank {
public static void main (String[] args) {
// Creates an object with the class Bank_Account defined below
Bank_Account account = new Bank_Account(150.00);
System.out.println ("Balance: "+ account.current_Account());
account.debit_Account(50.30);
System.out.println ("New Balance : "+ account.current_Account());
}
}
class Bank_Account {
private double value_account;
public Bank_Account ( double initial_value) {
value_account = initial_value;
} // end constructor for object creation
public void debit_Account ( double number) {
value_account = value_account + number;
}
public double current_Account () {
return value_account;
}
}
The above program has several methods and two classes. Look at the creation and usage of the object account. Its value is changed by the method debit_Account. Using objects can simplify programs by allowing user defined types.
The syntax for class creation is self explanatory; however, the syntax for method declaration is a bit more complex. Using the example :
public void debit_Account ( double number)
• public is the access level, which defines what can access the method. public allows other classes to access the method; however, private would only allow the method to be accessed inside class Bank_Account
• void is the return type. void is used when a particular method does not need to return a value; however, in example six, the current_Account method returns a double
• debit_Account is the name of the method
• ( double number) is known as the arguments for the method. This allows the value of the "bank account" to be passed into the method for processing
Now that we understand classes and methods (if not, read it again), we'll move on to something a lot simpler Arrays.
Example VII.
class Arrays {
public static void main (String[] args) {
int[] list = new int[5]; // creates Array of 5 integers
for (int index=0; index < 5; index++) {
list[index] = index * 2; // Assign values to Array
}
for (int index=4; index > -1; index--) {
System.out.println(list[index]);
}
} // method main
}
Java uses [ and ] to signify an array. In the above program an array of 5 integers is created. The number five is the number of elements in the array; however Java starts array indexing at ZERO, therefore the last index of the above array is four (5-1).
Arrays can also be used for objects, as well as the primitive data types. For example, the String[] args you see along with the main method is simply an array of String objects. This array of objects stores the command line parameters you type when you run your program. For example, if you typed :
java Someclassname new
the value stored in args[0] would be "new". Make sure args[0] exists before you access it, or else you will get an array out of bounds exception.
________________________________________
Now that you have completed the tutorial, you are ready to enter the world of programming, right? You have the basics, and where you go from here depends on how much ambition and hard work you are willing to spend on developing your skills.
The manipulation of objects and arrays, complemented with the use of several classes and methods will define you as a true java programmer, so take good notes!
Example VI.
class Bank {
public static void main (String[] args) {
// Creates an object with the class Bank_Account defined below
Bank_Account account = new Bank_Account(150.00);
System.out.println ("Balance: "+ account.current_Account());
account.debit_Account(50.30);
System.out.println ("New Balance : "+ account.current_Account());
}
}
class Bank_Account {
private double value_account;
public Bank_Account ( double initial_value) {
value_account = initial_value;
} // end constructor for object creation
public void debit_Account ( double number) {
value_account = value_account + number;
}
public double current_Account () {
return value_account;
}
}
The above program has several methods and two classes. Look at the creation and usage of the object account. Its value is changed by the method debit_Account. Using objects can simplify programs by allowing user defined types.
The syntax for class creation is self explanatory; however, the syntax for method declaration is a bit more complex. Using the example :
public void debit_Account ( double number)
• public is the access level, which defines what can access the method. public allows other classes to access the method; however, private would only allow the method to be accessed inside class Bank_Account
• void is the return type. void is used when a particular method does not need to return a value; however, in example six, the current_Account method returns a double
• debit_Account is the name of the method
• ( double number) is known as the arguments for the method. This allows the value of the "bank account" to be passed into the method for processing
Now that we understand classes and methods (if not, read it again), we'll move on to something a lot simpler Arrays.
Example VII.
class Arrays {
public static void main (String[] args) {
int[] list = new int[5]; // creates Array of 5 integers
for (int index=0; index < 5; index++) {
list[index] = index * 2; // Assign values to Array
}
for (int index=4; index > -1; index--) {
System.out.println(list[index]);
}
} // method main
}
Java uses [ and ] to signify an array. In the above program an array of 5 integers is created. The number five is the number of elements in the array; however Java starts array indexing at ZERO, therefore the last index of the above array is four (5-1).
Arrays can also be used for objects, as well as the primitive data types. For example, the String[] args you see along with the main method is simply an array of String objects. This array of objects stores the command line parameters you type when you run your program. For example, if you typed :
java Someclassname new
the value stored in args[0] would be "new". Make sure args[0] exists before you access it, or else you will get an array out of bounds exception.
________________________________________
Now that you have completed the tutorial, you are ready to enter the world of programming, right? You have the basics, and where you go from here depends on how much ambition and hard work you are willing to spend on developing your skills.
No comments:
Post a Comment