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, 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.
The first word in the method, public
, tells us that this method can be used by any part of the java program. Most methods are public.
The second word, boolean
, is a return type. It tells us what kind of question our method will answer. Any time you see the word boolean, it means you have data that is either true
or false
- so this method will either answer with true
(we will sleep in!) or false
(we won’t).
The third word, sleepIn
is the name of the method.
The two phrases in the parentheses, boolean weekday
and boolean vacation
tell us that we need to give this method two pieces of information, and that both of them will either be true or false (boolean again!). The names weekday
and vacation
will be treated as variables - the method doesn’t know what their value is, because they can be different each time the method is used. These pieces of information provided to the method are also called parameters.
In this case, one way this method might be used, or called, 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!
Consider the Coding Bat method signature below:
1
2
3
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
}
What is the return type of the method above?
boolean
How many parameters does this method have?
2
What are the name(s) of the parameter(s)?
aSmile
and bSmile
If you wanted to call this method, letting the method know that no smiling is happening, how might you do that?
monkeyTrouble(false, false)
Consider the Coding Bat method signature below:
1
2
3
public int dateFashion(int you, int date) {
}
What is the return type of the method above?
int
(integer)
How many parameters does this method have?
2
What are the name(s) of the parameter(s)?
you
and date
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?
dateFashion(7, 5)
Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for java on the back.