Monday, April 16, 2012

Java Tutorial & Lesson 5 - Understanding Methods and Attributes

Understanding Java Methods


In order to create more complex programs that won't simply do some math (like the previous examples) we need to be able to organize out functions. Until now, we could simply write the whole program inside the main method, but that wouldn't be neither organized nor functional.

Then, in order to write more advanced programs and code that we can use over and over again without re-writing it each time, we will need to use Methods. Here is a small definition of the methods:

Java Methods:

In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance. (Source)

In practical terms, this means that we can use methods to group a set of operations. Let's try that in our project with a few easy examples:


static int add(int a, int b) 
{  
    return a + b;
}
public static void main(String[] args) 
{
    int a = 3;
    int b = 2;
  
    int result = add( a, b );
  
    System.out.println( "The answer is: " + result );  
}

Now we have created a simple method that will simply add two any numbers and return the result. This method's name is add(). The structure of a method is as shown in the example:

  • static: (since it isn't normally used, this will be explained later)
  • int: is the returning type of the method.
  • add: this is the name of the method. You can name it however you want, but preferably something related to what it actually does.
  • (int a, int b): this two elements inside the brackets are called the parameters of the method. It means that in order to make this method work, we have to provide it with two elements (in this case of type int).
Now we can use this method as many time as we want. This is called reusability, which is the ability to perform some operation without writing the whole code again. For example we can write this inside the main method:


int a = 5;
int b = 3;
int c = 7;
int d = 2;
  
int result = add( a, b );
System.out.println( "The answer is: " + result );
result = add( c, b ); //Notice that we already declared the variable "result", so we don't have to declare it again (write int before the name)
System.out.println( "The answer is: " + result );
result = add( d, a );
System.out.println( "The answer is: " + result );

Java Attributes


The Java Attributes are just like variables, but they can be used in different places of the class, not just one method. This means that it is storing a value that can be used in different methods, thus making it actually a property of the class. The declaration of Java attributes is the same as the Java variables, but this time they are outside of the method, and are usually declared at the beginning of the code (after the import statements). Also, they should be declared with a availability property, which can be either private, public or protected (we will study these later). For now, let's just always set them as private.
Let's see how we can use attributes using the last example:
private static int a=3;
private static int b=2;
static int add(int a, int b) 
{  
    return a + b;
}
public static void main(String[] args) 
{  
    int result = add( a, b );
  
    System.out.println( "The answer is: " + result );  
}

As you see, there is no big change, but this little difference will help you be able to use a single stored value throughout different functions or methods.

Now let's see a more complex example.

Suppose that you have three groups of students, each one with three students, and you have to calculate the average of their final exam and the average of all the students. Normally you would have to add their results and divide them by the number of students. And you would have to do this 3 times. Let's simplify that with methods.

First we need to know the results of the students. We can store these values in variables as the following:

public static void main(String[] args) 
{
    //First group
    double est11 = 3.5;
    double est12 = 2;
    double est13 = 4.5;
    //Second group
    double est11 = 3.7;
    double est12 = 5;
    double est13 = 4;
    //Third group
    double est11 = 3.3;
    double est12 = 2.1;
    double est13 = 4;
}
Note: Just in case, remember this is the main method and it should be inside a class (such as the one we created in previous lessons)

Now that they are stored in variables of type double, let's create the method that calculate the average of the results:

static double average(double first, double second, double third) 
{
    double sum = first + second + third;
    return sum/3;
}
Now this method creates a variable called sum that adds the three values that it receives as parameters (a,b and c). Then it simply returns that result divided by 3 (the number of students).

But we also want to calculate the total average of all the students. Since our method only takes 3 numbers at the time, we will have to create a new method to calculate this final result:


//Lets declare an attribute here called: totalStudents
private static int totalStudents = 9;
static double average(double student1, double student2, double student3, double student4, double student5, double student6, double student7, double student8, double student9)
{
    double sum = student1+student2+student3+student4+student5+student6+student7+student8+student9;
    return sum/totalStudents;
}

The final program should look like this:

private static int totalStudents = 9;

static double average(double student1, double student2, double student3, double student4, double student5, double student6, double student7, double student8, double student9)
{
    double sum = student1+student2+student3+student4+student5+student6+student7+student8+student9;
    return sum/totalStudents;
}

static double average(double first, double second, double third) 
{
    double sum = first + second + third;
    return sum/3;
}
public static void main(String[] args) 
{
    //First group
    double est11 = 3.5;
    double est12 = 2;
    double est13 = 4.5;
    double resultGroup1 = average( est11, est12, est13 );
    System.out.println( "The average of the first group is: " + resultGroup1 );
    //Second group
    double est21 = 3.7;
    double est22 = 5;
    double est23 = 4;
    double resultGroup2 = average( est21, est22, est23 );
    System.out.println( "The average of the second group is: " + resultGroup2 );
    //Third group
    double est31 = 3.3;
    double est32 = 2.1;
    double est33 = 4;
    double resultGroup3 = average( est31, est32, est33 );
    System.out.println( "The average of the first group is: " + resultGroup3 );
}


Please feel free to comment and ask if you have any questions, your feedback will be greatly appreciated.

2 comments: