Variables, Methods and Parameters


Collapse Content

Variables

A variable in programming is like a container which holds a value:

Variable Video


There are different types of containers for different types of objects. For example, you can have a container which holds an integer (like 6), or another container which holds a String of text (like "hello").

In Java, you need to declare all your variables before using them. That means you simply state its type and name:

int num;

In this case you declared a variable called num, which can hold an int or integer.

Once its been declared, you can assign it to a value:

 num = 5;

You can also combine these steps:

int num = 5;

You can change the value of a variable at any point:

 num = 72;

num is now 72 instead of 5.

But in Java it cannot be set to the wrong type:

num = "word"; //error!

Since num was declared as an int, you cannot assign it the String "word".

Methods


Methods Video

In the previous challenge (and future ones), you coded a solution within a piece of code known as a method:

public static int doStuff(int a, int b){
    //your code here - you could then type:
    return a + b;
}

A method is a block of code that can be called by other code. In this example, the code at the bottom (that we said you could ignore), calls the method doStuff and it does stuff. By putting pieces of your code into method blocks, you can keep things organized, and you can call the same code multiple times from different parts of your program.
Note that you will usually want to name your methods with a more descriptive name than "do stuff".

In many cases, methods can be used like parts of a logical flowchart seen in Algorithm ways.

Parameters

Look at the top of that method again:

 doStuff(int a, int b){

The method takes in two variables, a and b. Variables that are taken in by a method are called parameters. They are declared in the opening parentheses of a method and are assigned a value whenever the method is called. If the method is called again, they are assigned new values.

So other code can pass two numbers to doStuff to do stuff with. In this example, it will add two numbers, so:

doStuff(1, 3) // will return 4
doStuff(-1, 11) // will return 10
doStuff("hello", "world") //will cause an error! This is because, the method declares a and b as integers so they cannot receive Strings!

In the challenges on this site, the code on the bottom of each challenge passes in numbers to the method doStuff (which get assigned to the parameters a and b). You need to fill in doStuff so it "does something" with a and b and returns the correct answer to the code that called it.

Challenge

As in the previous challenge, you will be given the method doStuff which takes in two parameters a and b. (You should replace //your code here with your own code.) For this challenge, you are to return the average of the two numbers, rounded down to the nearest integer.

Guideline: Create a new variable to store the sum of a and b. To get the average of the two numbers, simply divide that sum by 2 with /, and return that result. / will automatically round the answer down.

Challenge

Return the Average of any two integers, rounded down.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

Declare an integer variable age and assign it a value of 22 in one line of code.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

All Node Comments
Contact Us
Sign in or email us at [email protected]