7. ANALISIS Y DISCUSION DE RESULTADOS
7.2. ANALISIS PARA CUESTIONARIO PADRES DE FAMILIA
7.2.1 Encuesta a padres de familia y/o acudientes (45)
A method is invoked, causing it to be placed at the top of the call stack until it is finished executing. When a method is done executing, three things can occur:
■■ The method returns a value, in which case a primitive data type or
reference is passed back to the caller of the method.
■■ The method does not return a value, in which case the return value is
declared as void.
■■ The method throws an exception, which is thrown back to the caller
of the method. Exceptions are discussed in Chapter 11, “Exception Handling.”
In all three of these cases, the flow of control jumps back to the caller of the method. To demonstrate the flow of control of methods, let’s look at an example. The following Date class is a simple class that could be used to represent a cal- endar date. How many fields does the Date class have? How many methods?
public class Date {
public int day, month, year; public int getDay()
{
System.out.println(“Inside getDay method”); return day;
}
public void printDate() {
System.out.println(“Inside printDate method”);
System.out.println(month + “/” + day + “/” + year); }
}
The Date class has three fields, all of type int: day, month, and year. The Date class also has two methods: getDay() and printDate(). The getDay() method declares that it returns an int, and notice within getDay() it returns the day field, which is an int. The printDate() method is declared void and does not return a value. Both methods have an empty parameter list, which is denoted by the empty parentheses.
In the following DateProgram, a Date object is instantiated and the two methods in the Date class are invoked. Study the DateProgram carefully and try to determine its output.
public class DateProgram {
public static void main(String [] args) {
System.out.println(“Within main...”); Date today = new Date();
today.day = 25; today.month = 12; today.year = 2003;
System.out.println(“The day is “ + today.getDay()); System.out.println(“Printing the date...”);
today.printDate();
System.out.println(“What is displayed next?”); today.getDay();
} }
The first line of output of the DateProgram is as follows because it is the first statement in main():
Within main...
A Date object is then instantiated, and its fields are initialized to represent December 25, 2003.
Another System.out.println() statement occurs, and the string “The day is “ is concatenated with today.getDay(). The getDay() method is invoked on the today object, causing flow of control to jump to the getDay() method.
Notice that the string “The day is “ is not displayed yet. The call to System.out.println() has not occurred yet because order of operations requires that the method call to getDay() occur before the call to println(). After getDay() returns a value, the string concatenation is evaluated, and then println() will be invoked.
Flow of control is now within getDay(), and the following string is displayed:
Inside getDay method
Then the return statement is reached, which in this example returns the number 25. The return also causes flow of control to jump back to the calling method, which was main(). The 25 is concatenated with “The day is “, then the System.out.println() method is invoked, which outputs the following:
The day is 25
The next statement to execute is the println() in main(), which outputs the following:
Printing the date...
Then, today.printDate() executes, which invokes the printDate() method on the today object. The flow of control jumps to within printDate(), and the first statement in printDate() displays the following:
Inside printDate method
The next statement in printDate() prints out the fields of the object in the fol- lowing format:
12/25/2003
That is the end of printDate(), and it does not return a value, so the flow of control simply returns to main(), and the following is displayed:
What is displayed next?
The statement in question here is today.getDay(). I wanted to demonstrate that you can invoke a method that returns a value and not do anything with the return value. We just invoked getDay() again on the today object, so flow of control goes back to the getDay() method. The following statement is dis- played again:
Inside getDay method
Then the number 25 is returned to main(). The flow of control jumps to main(), but we did not do anything with the return value 25. The main() method simply keeps executing, and because we are at the end of main(), our program ends. Figure 5.1 shows the entire output of the DateProgram.
Figure 5.1 Output of the DateProgram.
Methods in Java can appear only within a class because Java is a strictly object-oriented programming language. In many languages, methods appear at a global level and can be invoked at any time. In Java, methods (not declared as static) can only be invoked on instances of the class. For example, the getDay() method can be invoked only on instances of the Date class.
If you do want to write a global-type method that can be invoked by anyone at any time without requiring an instance of a class, you write a static method. In Chapter 7, “Advanced Java Language Concepts,” static methods are discussed. A static method is essentially equivalent to the concept of a global method.