A procedure that is defined by the user is called
What are parameters?
·
The formal names given
to the data that gets passed into a method.
A procedure that is
defined by the user is called:
·
Method
What is a Javadoc?
·
a comment that clearly
states the functionality and usage of a class or method.
The value that a
method outputs is called
·
a return value
What does a void
method return?
·
no value
What does this method
call output?
public double doubleNumber(double myNumber)
{
return (double) (int) myNumber * 2;
}
doubleNumber(7.8);
·
14.0
What does this method
call return?
public int tripleInt(int x)
{
return x * 3;
}
tripleInt(4);
·
12
What will the value of
myBankAccount be after the method call?
int myBankAccount = 122;
public void depositMoney(int bankAccount, int deposit)
{
bankAccount += deposit;
}
depositMoney(myBankAccount, 572);
·
122
write a method that
will ask for user input until user inputs the String "no". Allow the
user to input any capitalization of the String "no" ("no",
"No", "NO", "nO") Also, have the method return
the number of loops.
·
public int
loopTillNo()
{
int count = 0;
while(!readLine("Again?").toLowerCase().equals("no"))
{
count++;
}
return count;
}
What will this method
call print?
public void patternGrid(int rows, int columns, char symbol)
{
for(int m = 0; m < rows; m++)
{
for(int n = 0; n < columns; n++)
{
System.out.print(symbol);
}
System.out.println();
}
}
patternGrid(3,4,'#');
·
####
####
####
What would this method
call output?
public int divideByTen(int num)
{
while(num / 10 >= 10)
{
num /= 10;
}
return num;
}
divideByTen(340)
·
34
What is the return
value of this method call?
public static String findTheMiddle(int number)
{
String stringNum = "" + number;
int mid = stringNum.length()/2;
if(stringNum.length() % 2 == 1)
{
return stringNum.substring(mid,mid+1);
}
else
{
return stringNum.substring(mid-1,mid+1);
}
}
findTheMiddle(2110890125)
·
89
What is returned by
this method call: translator("pokemon")?
public String translator(String word)
{
return word.substring(1) + word.charAt(0) + "ay";
}
·
"okemonpay"
What will this method
call output?
public int checkMethod(boolean x, boolean y)
{
if(!x)
{
if(y)
{
return -1;
}
else
{
return 1;
}
}
else
{
return 0;
}
}
checkMethod(false, true)
·
-1
What could the method
signature for a Karel command look like?
·
public void move()
{
//some code
}
What will this method
call print to the screen?
public static void formatText(int a, int b, String c)
{
System.out.println(b + " " + c + ", " + a);
}
formatText(2018, 17, "Dec")
·
17 Dec, 2018
Write a method that
loops forever until the user inputs the correct secret password or until the
user fails to enter the correct password 10 times. The secret password the
program should look for is the String "secret"
·
public void
secretPassword()
{
int count = 0;
while(true)
{
if(count == 10)
{
System.out.println("You are locked out!");
return;
}
if(readLine("Password?").equals("secret"))
{
System.out.println("Welcome!");
return;
}
count++;
}
}
What will this method
call output?
public String yesOrNo(boolean myBoolean)
{
if(myBoolean == true)
{
return "Yes";
}
else
{
return "No";
}
}
yesOrNo(true)
·
"Yes"
What will this method
call output?
public boolean someMethod(int number)
{
if(number % 2 == 1)
{
return true;
}
else
{
return false;
}
}
someMethod(9990)
·
false
What will this method
call output?
public int myMethod(String x, char y)
{
int z = 1;
for(int i = 0; i < x.length(); i++)
{
if(x.charAt(i) == y)
{
z++;
}
}
return z;
}
myMethod("Karel The Dog", 'e');
·
3
What is wrong with
this method definition?
public int printPayAmount(int amount)
{
System.out.println(amount);
}
·
Nothing is returned
from this method
What kind of error
does this method cause?
public void myMethod(String x)
{
for(int i = 0; i <= x.length(); i++)
{
System.out.println(x.substring(i, i+1));
}
}
myMethod("Frog");
·
Runtime Error: String
index out of range
What will this method
call print to the screen?
public void funWithNumbers(double myDouble)
{
int myInt = (int) myDouble;
String myString = "";
while(myInt != 0)
{
myString = myInt % 10 + myString;
myInt /= 10;
}
System.out.println(myString);
}
funWithNumbers(314159)
·
314159
Why do we use methods
in Java? (which ones)
I. To make code easier to understand
II. To define global variables
III. To avoid repeated code
IV. To simplify code
V. To avoid needing to define them as public or private
·
xxx6
What does this method
call output?
public int someMethod(int x, int y)
{
int sum = 0;
while (x < 10)
{
sum += x % y;
x++;
y++;
}
return sum;
}
someMethod(3,1)
·
10