Return to main page
View source or report issues on GitHub

Anatomy of a Java Method Signature

Starting in 2023, we will begin programming by looking at methods from [CodingBat] (https://www.codingbat.com)

In CodingBat, our goal is to write methods:star:, or small bits of code that solve a particular problem or answer a particular question. A java program will usually be built from many methods, connected together, so writing a single method is the root skill of computer programming.

Let’s consider one CodingBat problem below:


The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if its not a weekday or we’re on vacation.

Write code to represent this method.

1
2
3
public boolean sleepIn(boolean weekday, boolean vacation) {
    // your job is to put code here!
}

The the example code above, there are several key terms we need to understand before we can begin to write code. All of this information comes from the very first line of the method, which is also called the method signature:star:.

In this case, one way this method might be used, or called:star:, is with the code sleepIn(true, false). We are passing two pieces of information - it IS a weekday, but we are NOT on vacation. The method should then answer with the value false because in that situation, we cannot sleep in!

Quiz Yourself

Consider the Coding Bat method signature below:

1
2
3
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {

}
  1. What is the return type of the method above?

    Click to expand answer

    boolean

  2. How many parameters does this method have?

    Click to expand answer

    2

  3. What are the name(s) of the parameter(s)?

    Click to expand answer

    aSmile and bSmile

  4. If you wanted to call this method, letting the method know that no smiling is happening, how might you do that?

    Click to expand answer

    monkeyTrouble(false, false)

Consider the Coding Bat method signature below:

1
2
3
public int dateFashion(int you, int date) {

}
  1. What is the return type of the method above?

    Click to expand answer

    int (integer)

  2. How many parameters does this method have?

    Click to expand answer

    2

  3. What are the name(s) of the parameter(s)?

    Click to expand answer

    you and date

  4. If you wanted to call this method, letting the method know that you had a fashion score of 7 and your date had a fashion score of 5, how would you do that?

    Click to expand answer

    dateFashion(7, 5)

Study Materials

Bitmoji Books Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for java on the back.