Methods Flashcards

1
Q

1) Study the following code before moving on to the next flashcard.

public class FindCost4
{
public static void main(String[] args )
{
Scanner keyboard = new Scanner(System.in);
double price, tax;

System.out.println(“** Product Price Check **”);

System.out.print(“Enter initial price: “);
price = keyboard.nextDouble();

System.out.print(“Enter tax rate: “);
tax = keyboard.nextDouble();

price = addTax(price, tax); // call the addTax method

System.out.println(“Cost after tax = “ + price);

}
static double addTax(double priceIn, double taxIn)
{
return priceIn * (1 + taxIn/100);
}
}

a) Why are the variables in the line that calls the addTax method different to the variables in the actual addTax method?

e.g. price = addTax(price, tax);

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2) Why are the variables in the line that calls the addTax method different to the variables in the actual addTax method?

price = addTax(price, tax); // call the addTax method

System.out.println(“Cost after tax = “ + price);

}
static double addTax(double priceIn, double taxIn)
{
return priceIn * (1 + taxIn/100);
}
}

A

1) Because they (price, tax) are the ACTUAL PARAMETERS that we are sending into the method.

They are copied into the FORMAL PARAMETERS of the method. we call this ‘passing parameters’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3) Were the ACTUAL Parameters (price, tax) declared earlier on in the program?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4) As the names of the ACTUAL PARAMETERS are different to the names of the FORMAL PARAMETERS, how does the program know which variables we are talking about?

A

It is the order. The order counts!

As we can see from the code, the ACTUAL PARAMETERS come before the Formal Parameters.

price = addTax(price, tax); // call the addTax method
System.out.println(“Cost after tax = “ + price);
}
static double addTax(double priceIn, double taxIn)
{
return priceIn * (1 + taxIn/100);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

5) Could we do something like this? (changed variable names to numbers)

price = addTax(5.55, 17.5); // call the addTax method
System.out.println(“Cost after tax = “ + price);
}
static double addTax(double priceIn, double taxIn)
{
return priceIn * (1 + taxIn/100);
}
}

A

Yes, we can.
As long as we still have the two, double variables that the addTax method is expecting, it will still work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

6) Can we do this?

price = addTax(133.0); // call the addTax method
System.out.println(“Cost after tax = “ + price);
}
static double addTax(double priceIn, double taxIn)
{
return priceIn * (1 + taxIn/100);
}
}

A

No!
Know we only have one double, but the addTax method is expecting two!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7) A method to determine if a number is even or odd

There are many instances in our programming lives where we might need to test whether a number is even or odd.

How can we test whether a number is even or not?

A

An even number will give a remainder of zero when divided by 2. An odd number will not. So we can use the
modulus operator here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8) Why do we have an int in the formal parameters?

static boolean isEven(int numberIn)
{
return (numberIn % 2 == 0);
}

A

Because our input will be an integer.
The boolean part is for the output, which will be either true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9) The following code is known as an: e_______________

numberIn % 2 == 0

A

expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

10) A method that returns a value of either true or false can be called a _________________

A

boolean method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

11) Boolean methods

We can use boolean m___________ as a test, just as we do with boolean expressions.

e.g. if (10 > price) \the true or false answer would determine the next action

      if(isEven(number))     \\the true or false result of the method call will do the same.
A

methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

12) What operator would we use to test for a false value?

e.g. if(isEven(number))

A

!
if(!isEven(number))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13) Look at the following code. Are the variables x and y local to method1?

public class ScopeTest
{
public static void main(String[] args)
{
int x = 1;
int y = 2;
method1(x, y); // call method1
}
static void method1(int xIn, int yIn)
{
int z; // z is local to method1
z = xIn + yIn;
System.out.println(z);
}
}

A

No, they are local to the main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

14) If x or y were referred to in the method1 method, would they be out of scope.

public class ScopeTest
{
public static void main(String[] args)
{
int x = 1; // x is local to main
int y = 2; // y is local to main

method1(x, y); // call method1
}
static void method1(int xIn, int yIn)
{
int z; // z is local to method1
z = xIn + yIn;
System.out.println(z);
}
}

A

Yes, they would!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

15) Can we overload method names?

A

Yes, we can!
We can use the same names for methods within our class, provided that the parameters are different.

e.g. Both methods are called ‘max’.

static int max(int firstIn, int secondIn)

static int max(int firstIn, int secondIn, int thirdIn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly