Flow of Control for a Method Call
Method Call Flow
- Every program starts executing at the start of
main() . - When reaching a method call, arguments are copied to the parameters.
- Method code executes until reaching a return statement.
- Return statement returns a value to the method call.
- Calling method continues after the method returns.
Activity 17.1: Tracing a Method Call (10 pts)
- List the line numbers of each statement of your program from
Activity 16.1 in the order the lines are executed. For example, if
main() starts on line 9, statements are executed as follows:
9, 10, 11, 12, ...
Note: Do not bother to list blank lines or lines containing only a curly brace (} ).
- Submit your list of line numbers in the text box on Canvas for this activity.
Some Style Requirements for Methods
- Consider again our example method
|
public static int add(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two numbers to add: "); int num1 = input.nextInt(); int num2 = input.nextInt(); int total = add(num1, num2); System.out.println("Sum=" + total); }
|
- Note the placement of the curly braces
- There are two common styles of curly brace placement for methods:
- Place the opening brace on the same line as the method heading:
public static int myMethod() {
// statements of the method
}
- Place the opening brace under and lined up with the first letter of the return type:
public static int myMethod()
{
// statements of the method
}
- We can use either style as long as we are consistent
- Also notice the indentation of the statements inside the method
- As before, we always indent 3-4 more spaces after an opening curly brace
- After the closing curly brace, we no longer indent the extra 3-4 spaces
- Indenting makes it easier to see the block of code
- In addition, method names always start with a lower case letter like variables
- We can tell the difference between method and variable name because methods have parenthesis
Activity 17.2: Calculating Your Salary (10 pts)
- Open up Eclipse and create a new Java project called Salary.
- Copy and paste the starter code below into the file.
- NOTE: Do not change the starter code! All you need to do is add to it.
import java.util.Scanner;
public class Salary {
//Write your methods here
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int hours;
double hourly_wage;
double weekly_salary = 0;
double monthly_salary = 0;
double yearly_salary = 0;
System.out.println("This program will calculate your weekly, monthly "
+ "and yearly salary!");
System.out.print("Please enter your hourly wage: ");
hourly_wage = input.nextDouble();
System.out.print("Please enter the number of hours you work each week: ");
hours = input.nextInt();
//call methods below
weekly_salary = //method call goes here
monthly_salary = //method call goes here
yearly_salary = //method call goes here
System.out.printf("You make $%.2f per week\n", weekly_salary);
System.out.printf("You make $%.2f per month\n", monthly_salary);
System.out.printf("You make $%.2f per year\n", yearly_salary);
input.close();
}
}
- Now write three methods as follows and place them above main:
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the weekly salary
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the monthly salary
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the yearly salary
Assume that you work 50 weeks a year (2 weeks vacation)
- Add a method call and store the result as the appropriate variable.
weekly_salary = //method call goes here
monthly_salary = //method call goes here
yearly_salary = //method call goes here
- Now, run your code and verify that it prints the correct values for each salary type.
- When you are finished, upload your file to Canvas.
- Your output should look identical to the following (except user input will vary):
|
|