Welcome to Lesson 18!


Learning Objectives
By the end of today's class, you should know...
  • How can one method call another?

Announcements

  • Midterm after a short lesson and the break
  • No quiz this week due to midterm
  • Don't forget to complete your lab, though!


Lesson 17 Practice Exam Questions

With a partner, answer the following questions:

Q1: Write a proper Javadoc comment for the below method from last class:

  • Name: lowerCaseCounter:
    • It takes a String parameter
    • It returns an int for the total number of lower case letters contained in the String

Q2: Write the below method:

  • Name: displayPrice
    • takes in one double parameter for a price
    • Prints the price to the console with a dollar sign and to two decimal places
    • returns nothing

Q3: Now call the method using the starter code below:

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double number;
System.out.print("Enter the price: ");
number = input.nextDouble();
System.out.print("The formatted price is: ");
//call the method here!
}

Methods Calling Methods

  • Methods may call other methods
  • Within the body of one method, we can call another method call
  • Methods can call other methods as often as needed
  • We are already doing this when main calls a method
  • The following program calls a "helper" method to help calculate the BMI.
  • Because calculating the BMI is a somewhat complicated process, it is helpful to create a second method to do part of the work for us.
  • The heightToSquareInches method handles turning the height from feet and inches (such as 5'8") into inches squared (such as 68"2).

Example of Methods Calling Methods


    /**
     * Calculates a user's Body Mass Index
     * @param feet the user's height in feet
     * @param inches the user's height in inches
     * @param weight the user's weight in pounds
     * @return the Body Mass Index
     */
    public static double calculateBMI(int feet, int inches, double weight) {
        int inches2 = heightToSquareInches(feet, inches);
        double bmi = weight / inches2 * 703;
        return bmi;
    }

    /**
     * Converts height in feet and inches to height in inches squared
     * @param feet the height in feet
     * @param inches the remaining inches
     * @return the height in inches squared
     */
    public static int heightToSquareInches(int feet, int inches) {
        int heightInches = feet * 12 + inches;
        int heightInches2 = heightInches * heightInches;
        return heightInches2;

    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int height_feet, height_inches;
        double weight, bmi;

        System.out.print("Please enter your weight in lbs: ");
        weight = input.nextDouble();

        System.out.print("Please enter your height in feet: ");
        height_feet = input.nextInt();

        System.out.print("Please enter your remaining height in inches: ");
        height_inches = input.nextInt();

        bmi = calculateBMI(height_feet, height_inches, weight);
        System.out.println("Your BMI is " + bmi);
    }

Midterm 2


Wrap Up

  • With your partner, answer the questions from today's learning objectives

Upcoming Assignments


~Have a Great Weekend!~