Welcome to Lesson 17!


Learning Objectives

By the end of today's class, you should know...
  • How do you trace a method call?
  • What are some style requirements for methods?
  • More method practice

Announcements

  • Midterm 2 after a short lesson and the break!
  • Deadline to drop the class with a 'W' has been extended:
  • Assignment 16 due next class!
  • How did it go with the method lesson video?
  • No Quiz or Lab this week - Happy Thanksgiving!


Review Activity


With a partner, answer the following questions:

  • Label the components of the following method:

public static double calc_area_circle(int radius) {

    double area = 3.14 * radius * radius;

    return area;

}

Method Name:

Return type:

Parameter(s):


  • Write a correct function call for the above method, passing it the value of 9, and storing the result in the cArea variable:

         double cArea = ???????????????????;

    System.out.println("The area of the circle with radius 9 is: " + cArea);




Flow of Control for a Method Call

  • To use methods well, we must understand their flow of control
  • In our example, the program starts executing in the main() method
  • When our program gets to the following statement, it stops executing in main() and jumps to our method:
    int total = add(num1, num2);
    
  • The program executes the statements in the method and then returns to the statement from which it jumped
  • When the method returns, the returned value replaces the method call
  • After returning, the program completes processing the calling statement and then moves on to the next statement
  • In our example, the statement saves the returned value in the variable: total
  • Every time the flow of control reaches a method call, the program:
    1. Temporarily stops executing in the current method
    2. Jumps to the called method and executes the statements of that method
    3. Returns to the point in the code from which it jumped

Method Call Flow


  1. Every program starts executing at the start of main().
  2. When reaching a method call, arguments are copied to the parameters.
  3. Method code executes until reaching a return statement.
  4. Return statement returns a value to the method call.
  5. Calling method continues after the method returns.


Activity 17.1: Tracing a Method Call (10 pts)

  1. 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 (}).

  2. 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:
    1. Place the opening brace on the same line as the method heading:
      public static int myMethod() {
          // statements of the method
      }
      
    2. 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:
    • weeklySalary()
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
    • monthlySalary()
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
    • yearlySalary()
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):


~Midterm 2~



Upcoming Assignments

~Happy Thanksgiving!~