Notice: Assignments are currently being rewritten for CSAlpha. This assignment was created for an old version of the curriculum and may no longer align with the current curriculum.
If you have any questions regarding the assignment or see any errors in the assignment writeup, please let me know.
Create a text file called assign3.txt
. You will be using this to respond to questions in the assignment writeup and paste your code.
To begin this assignment, you must have a basic understanding of functions and their uses. The function below, called "square", takes an integer as a parameter and returns the value of the integer squared:
xxxxxxxxxx
public static int square(int number) {
return number * number;
}
Let's go over each individual part of the function for review:
public
This keyword denotes the accessibility of the function. It determines where the function can be called from. The most commonly used access specifiers in Java are public and private. In the diagram below, "Default" refers to a function that doesn't define an access specifier (i. e. doesn't have public, private, or protected in its declaration). For now, we will be using public regardless of what we think the scope of the function should be; so just include this keyword for now.
static
We will cover this keyword once we learn about classes and objects. However, the reason we require this keyword here is because the main
method must be static, so all functions called from within the main
method must also be static.
int
This is known as the return type of the function. Think of it as the type of data the function must "output". All keywords must be placed before the return type, while the return type must always be directly before the function name.
square
This is the name of the function. The programmer chooses the name to his or her liking, but descriptive names that describe the function's use will prevent you from having to reread your code all the time.
(int number)
The content of the parentheses after the name of the function are called the parameters or the arguments of a function. Think of these variables as the input of the function, variables that the functions uses to process the output. You can also have multiple variables in the parentheses, as long as you declare each variable with a datatype and name and separate them with commas. When you call a function, you must pass in the variables/data in the order that the function describes.
In the main method, I've called the square function above as an example:
xxxxxxxxxx
public static void main(String[] args) {
int result = square(4);
System.out.println(result);
System.out.println(square(2));
}
xxxxxxxxxx
PROGRAM OUTPUT TO CONSOLE:
16
4
To call a function, write the function's name and follow with the parameters you want to pass in inside parentheses. When you call a function with a return value, imagine that the return value or "output" of the function is basically replacing the place where you called the function. This is why we are able to set result
to the function call; the function returns a value so the function call becomes the return value after it is finished running.
There are cases where functions don't need to return any value or even take in any parameters. Look at the two examples below:
xxxxxxxxxx
public static void printString(String str) {
System.out.println(str);
}
public static void printHello() {
System.out.println("hello");
}
The first function, printString
, takes in a String argument and prints it. Since the function does not need to return a value, it has the keyword void
for its return datatype.
The second function, printHello
, prints the string "hello"
. Since the function has no need to return a datatype or take in any parameters, it has the keyword void
for its return datatype and empty parentheses after the function name.
With all that knowledge under your belt, you are ready to start the assignment!
For each of the following functions, make sure to test the examples given by calling the functions in the main method. For functions that do not print to the console, check your results manually by printing the function result in the main method after calling them.
Once you are finished with each function declaration, please paste the function into your text file named assign3.txt
.
Declare a function named selectNumber
that takes in an integer, and prints the following message: "You have selected the integer (integer)"
4
as the input, your function should print out: "You have selected the integer 4"Declare a function named printCat
that takes two Strings as input and prints the concatenation of the two Strings (addition of the two Strings).
The first String parameter should precede the second String parameter in the print to console.
For example, with the two Strings "One fish" and " Two Fish", your function should print out: "One fish Two fish"
Declare a function named sumFour
that sums four integer parameters, and returns this sum.
Declare a function named toggle
that returns the opposite value of a boolean parameter, and returns this opposite value. Declare this function without using the NOT operator (!).
true
was passed into this function, the function would return false
, and vice versa.If you can complete the assignment extension, you are well on your way to mastering functions.
Go back to assignment 2; think about how you can use functions to clean up your code. What functions could you declare that would reduce the repetition in your code and also make it easier to add new events (events are the console output after the user makes a choice)?
(Hint: Is there a way to take user input and process it in a separate function? Doing this would require you to restrict user input to a few values you can work with. How can you make the user's input consistent without having to be restricted to only asking yes or no questions?)
You do not have to submit the extension via text file. We will go over it in lesson if you have completed it.
Declare functions of anything that you think would be useful! Please paste these additional functions into your text file.