Welcome to Lesson 18!

Learning Objectives
By the end of today's class, you should know...
  • What is a void method?
  • What is the difference between a void and non-void method?
  • How do you call a void method (compared to a non-void method)?
  • When do you use a void method?
  • How can one method call another method?
  • How do you write a Javadoc comment
  • What is a tag?
  • List some of the common tags


  • Return midterms in last 5 minutes of class - excellent results!
    • As: 20 (3 100% scores)
    • Bs: 9
    • Cs: 2
    • Ds: 2
    • Fs: 5
    • No Shows: 6
  • If you are not doing as well as you would like in this class:
    • Are you attending class?
    • Are you paying attention (move to the front)?
    • Are you asking for help (office hours, email, tutor, T.A.)?
    • Are you getting started on your homework the night it is assigned?
    • Are you copying off your classmates instead of working through the assignment for yourself?
    • There is still time to turn it around! Please sign up for free tutoring in the ATC 203 lab to help yourself catch up.
  • Don't forget Lab 9 (due Friday!) and Quiz 8 (due Friday!)
  • Women in Computer Science Club Meeting tomorrow
    • ATC 202
    • 12:30-1:30pm
    • Apple speaker!

Review Activity: Tracing a Method Call

1. With a partner, write the following method:

     * Given a string, return a new string where "not " has been added to the front.
     * However, if the string already begins with "not", return the string unchanged.
     * notString("candy") → "not candy"
     * notString("x") → "not x"
     * notString("not bad") → "not bad"

2. What is output by the following program? (Do not run the code -- work it out by hand)

public static int mystery(int param) {
System.out.println("param=" + param);
param = param * 2;
return param;

public static void main(String[] args) {
    int num = 2;
System.out.println("At first, num=" + num);
int result = mystery(num);
System.out.println("After calling, num=" + num);
System.out.println("And result=" + result);

  1. At first, num=2
    After calling, num=4
    And result=4
  2. At first, num=2
    After calling, num=4
    And result=4
  3. At first, num=2
    After calling, num=2
    And result=4
  4. None of these

Void Methods

  • Previously we looked at methods that returned one value
  • Methods returning a value use a return statement
    return result;
  • A method that returns no value is called a void method
  • In Java, void methods are defined like methods that return a value
  • However, the keyword void replaces the return type
  • For example, what do you notice that is different about the following?
    public static void displayDegrees(double degreeFarenheit) {
        double degreeCelsius = 5.0 / 9 * (degreeFarenheit - 32);
             + " degrees Fahrenheit is equivalent to "
             + degreeCelsius + " degrees Celsius.");
  • There are only two differences between definitions for void methods and other methods:
    • void return type
    • return statement is optional and does not specify a value if used
  • If no return type is specified, the method returns after executing the last statement
  • Here is an example program using the void method shown above

Example Program With a void Method

public static void displayDegrees(double degreeFarenheit) {

    double degreeCelsius = 5.0 / 9 * (degreeFarenheit - 32);
    + " degrees Fahrenheit is equivalent to "
+ "%.1f degrees Celsius.\n\n", degreeCelsius);

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
double fTemperature;

 System.out.print("Enter a temperature in Fahrenheit: ");
fTemperature = input.nextDouble();

//Notice method call without assigning result to variable

When to Write void Methods

  • When we use a non-void method, we are asking a question
  • The method returns a value in response to our question
double square_root = Math.sqrt(9.0);

  • When we use a void method, we are giving the computer a command

  • We do not expect or receive an answer

Common Errors With void Methods

  • Note that we cannot call a void method from a System.out.print statement
  • For example, the following causes a compile error:
System.out.println(displayDegrees(fTemperature)); // NO!

  • The reason is that a void methods does not return a value and System.out.println has nothing to print
  • Similarly, we cannot call a void method in an assignment statement:
double temp = displayDegrees(fTemperature); // NO!

  • There is nothing to assign to the variable temp

Activity 18.1: Into the Void (10 pts)

  • Void methods are useful for printing out information in a particular format.
  • Let's consider dates and times.
  • In America, we use the 12 hour clock, but in Europe, the 24 hour clock is used.
  • For example, in America, 8:30 at night is represented as 8:30pm, while in Europe, it is represented as 20:30.
  • In America, we write dates in this format MM-DD-YYYY. In Europe, dates are often written as DD.MM.YYYY
  • Let's write a program that uses void methods to format dates and times.
  • We will print each date and time in both the American and European formats for our user.
  • Open up Eclipse and create a new Java project named DateTime
  • Copy and paste the starter code below into your file:
 * @author
import java.util.Scanner;
public class DateTime {

    public static void main(String[] args) {
        int year;
        int day;
        int month;
        int hour;
        int minutes;
        String dayEve;
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome! This program will print dates and times "
            + "in both the American and European styles!\n");

        System.out.println("First, let's print a formatted date.\n");
        System.out.print("Please enter the current year: ");
        year = input.nextInt();
        System.out.print("Please enter the current month: ");
        month = input.nextInt();
        System.out.print("Please enter the current day: ");
        day = input.nextInt();
        //call to the formatDateAmerican method here
        //call to the formatDateEuropean method here
        System.out.println("\nNow, let's print a formatted time.\n");
        System.out.print("Please enter the current hour: ");
        hour = input.nextInt();
        System.out.print("Please enter the current minutes: ");
        minutes = input.nextInt();
        System.out.print("Please enter whether it is \"morning\" or \"evening\": ");
        dayEve = input.next();

        //call to the formatTimeAmerican method here
        //call to the formatTimeEuropean method here

        System.out.println("\nHave a good day!");

  • Now, you need to write four methods as follows:
takes as input three integer parameter, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format m/d/yyyy
returns nothing
takes as input three integer parameters, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format d.m.yyyy
returns nothing
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the format H:MMam or H:MMpm
returns nothing
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the 24 hour clock. Note that there is no am or pm in this format.
returns nothing
  • These methods should be written outside of main (above or below is fine!).
  • When you are finished writing your methods, call them in main in the place indicated by the comments.
  • Upload your dateTime.java file to Canvas.
Your output should look identical the output below when you are finished:

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);

Javadoc Comments

  • Java also has a type of comment known as a Javadoc comment
  • Javadoc comments are used to generate documentation automatically
  • Syntax:
     * Description part of a Javadoc comment
     * @tag Comment for the tag
  • Note the extra star (*) in the opening of the comment
  • Javadoc comments have two parts:
    1. Description of the code
    2. Followed by zero or more tags
  • For example, you should put a Javadoc comment like the following at the beginning of every class:
     * Hello.java
     * Purpose: Prints a message to the screen.
     * @author Jennifer Parrish
     * @version 1.4 6/2/16
  • In this example, we have two tags:
    • @author followed by the name of the author
    • @version followed by the version number or date

Commenting Methods

  • In addition, you put Javadoc comments like the following before every method:
     * The main method for the Hello program.
     * @param args Not used
  • Then you use a tool, known as Javadoc, to automatically create program documentation
  • Also, a tool known as CheckStyle will check your comments (among other things) for correct usage
  • For your homework, every method must have a Javadoc comment.
  • Comments are for human readers, not compilers
  • Your comments should be structured as follows
    1. Method comments start with /** and ends with */
    2. The first line explains the idea of the method, not the implementation
    3. An @param entry explains each parameter - one for each parameter in the method
    4. An @return entry describes the return value (not required for void methods)
  • The following example has two fully commented methods:

     * multiplies a number by itself
     * @param number the number to square
     * @return the squared number

    public static double square(double number) {
        double result = number * number;
        return result;
     * Formats a date into the month/day/year format
     * @param month the current month
     * @param day the current day
     * @param year the current year
     * @return the formatted date

    public static String printDate(int month, int day, int year) {
        return "" + month + "/" + day + "/" + year;

    public static void main(String[] args) {
        //method calls here

  • Note that if you type /** above a method, Eclipse will help you fill in the rest.

More Information

Activity 18.2: JavaDoc Comments (10 pts)

  • Open up a Java file in Eclipse called Comments.java in a project named Comments
  • Copy and paste the below starter code into your program.
  • Add a proper javadoc comment for each method in the program.
  • When you are finished, submit your program to Canvas.
 * @author

public class Comments {
    public static boolean isLeapYear(int date) {
        if (date % 4 == 0) {
            return true;
        } else {
            return false;
    public static String formatName(String first, char initial, String last) {
        return first + " " + initial + ". " + last;
    public static double areaCircle(double radius) {
        return Math.PI * radius * radius;
    public static void main(String[] args) {
        //method calls go here

Wrap Up

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

Upcoming Assignments

~See You Thursday!~