Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Methods

1) Let’s watch a video.


We are going to watch a video. Don't worry if you don't understand
everything,
just relax and enjoy!
https://www.youtube.com/watch?v=7MBgaF8wXls

2)According to the video, answer the following question:

What’s an argument in a method?


An argument is any extra information that the program needs to run.
Which class is imported above the class “apples”?
Scanner.
What’s wrong in the name of the classes?
The first letter must be written in capital letter.
Do you remember the name written by keyboard?
Bucky Roberts.
Do you know which the tutorial number is?
15 th.
Which was the name of the executable class?
Apples.
Which the method used for printing on the screen? Is it a static
method? Why?
Println(“Hello”+name);
No because out is an object automatically created by Java.

Do you remember which the prompt to user was?


Enter your name here:
Is the method simpleMessage(String name) static? Why?
No because is invoked trough the object Tuna.

3) Compare
Compare your answers with your partner. You can use
expressions like:

As far as I’m concerned.


In my opinion.
I agree/disagree with you.
Personally, I think.
I’d say that.
I’d suggest that.
I'd like to point out that.
What I mean is.

4) Identifying method features


Read the following text. Underline the words you don't understand.
Compare your words with your partner and ask the meaning of the
unknown words.

Scope of Declarations
You have seen declarations of various Java entities, such as classes, methods, variables and parameters.
Declarations introduce names that can be used to refer to such Java entities. The scope of a declaration is
the portion of the program that can refer to the declared entity by its name. Such an entity is said to be “in
scope” for that portion of the program.

The basic scope rules are as follows:


1. The scope of a parameter declaration is the body of the method in which the declaration appears.
2. The scope of a local-variable declaration is from the point at which the declaration appears to the end
of that block.
3. The scope of a local-variable declaration that appears in the initialization section of a for statement’s
header is the body of the for statement and the other expressions in the header.
4. The scope of a method or field of a class is the entire body of the class. This enables non-static
methods of a class to use the class’s fields and other methods.

Method Overloading
Methods of the same name can be declared in the same class, as long as they have different sets of
parameters (determined by the number, types and order of the parameters) — this is called method
overloading. When an overloaded method is called, the Java compiler selects the appropriate method by
examining the number, types and order of the arguments in the call. Method overloading is commonly used
to create several methods with the same name that perform the same or similar tasks, but on different types
or different numbers of arguments.

Useful expressions:
What does ______ means?
I don't understand the meaning of ____. Can you explain it to me?

5) Fill in the blanks in each of the following statements:

a) A method is invoked with an object.

b) A variable known only within the method in which it is declared


is called a local variable.

c) The return statement in a called method can be used to pass the


value of an expression back to the calling method.

d) The keyword void indicates that a method does not return a


value.

e) Data can be added or removed only from the top of a stack.


6) Give the method header for each of the following methods:

a) Method hypotenuse, which takes two double-precision arguments


side1 and side2 and returns a double-precision.
public double hypotenuse (double, double)

b) Method smallest, which takes three integers x, y and z and


returns an integer.
public int smallest (int, int, int)

c) Method instructions, which does not take any arguments and does
not return a value.
[Note: Such methods are commonly used to display instructions to a
user.]
Public void instructions ()

d) Method intToFloat, which takes an integer argument number and


returns a floating-point result.
public float intToFloat (int)

Compare your answers with your partner.

You might also like