Chapter 6 Methods - MPL Flashcards

1
Q

Write the definition of a method twice, which receives an integer parameter and returns an integer that is twice the value of the parameter.

A

public static int twice (int p1){
return p1 * 2;
}

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

Write the definition of a method add, which receives two integer parameters and returns their sum.

A

public static int add(int i1, int i2){
return i1 + i2;
}

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

Write the definition of a method powerTo, which receives two parameters. The first is a double and the second is an int. The method returns a double.

If the second parameter is negative, the method returns zero. Otherwise it returns the value of the first parameter raised to the power of the second parameter.

A

public double powerTo(double double1, int int2){
if (int2 < 0){
return 0;
}else{
return Math.pow(double1, (double)int2);
}
}

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

smallest of x, y, and z to another integer variable min.

Assume that all the variables have already been declared and that x, y, and z have been assigned values.

A

if(x < y){
if(x < z){
min = x;
}else{
min = z;
}
}else{
if(y < z){
min = y;
}else{
min = z;
}
}

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

Given that a method receives three parameters a, b, c, of type double, write some code, to be included as part of the method, that checks to see if the value of a is 0; if it is, the code prints the message “no solution for a=0” and returns from the method.

A

if (a==0){
System.out.println(“no solution for a=0”);
}

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

Given that a method receives three parameters a, b, c, of type double, write some code, to be included as part of the method, that determines whether the value of “b squared” – 4ac is negative. If negative, the code prints out the message “no real solutions” and returns from the method

A

if (Math.pow(b,2) - (4ac) < 0){
System.out.println(“no real solutions”);
return;
}

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

add is a method that accepts two int arguments and returns their sum.

Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared.

Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales.

Assume that add is defined in the same class that calls it.

A

eurasiaSales = add(euroSales, asiaSales);

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