GROUNDWORK Chapter 6 - Methods Name: ________________________________ Date: ______________ Fill in every blank. Where a term is asked for, write the word the course uses. Where code is asked for, write exact Java. Where output is asked for, write what the program prints, one line per line. VOCABULARY ---------- 1. The variables declared in the parentheses of a method's declaration are called __________________, while the values you pass in when calling the method are called __________________. 2. A variable created inside a method exists only inside it; this is called __________________ scope. 3. A comment that starts with three slashes, ///, written just above a method to say what it does, is called a __________________ comment. 4. Breaking a problem into small methods that each do one job, and giving each a name, is called __________________. READING CODE ------------ 5. What does this print? void doublePrint(int n) { IO.println(n * 2); } int doubleReturn(int n) { return n * 2; } void main() { doublePrint(5); int b = doubleReturn(5); IO.println("b is " + b); } ____________________________________ 6. What does this print? void greet(String name, String greeting) { IO.println(greeting + ", " + name + "!"); } void greet(String name) { greet(name, "Hello"); } void main() { greet("Ada"); greet("Ada", "Good morning"); } ____________________________________ 7. What does this print? int positionOf(String word, char letter) { for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == letter) { return i; } } return -1; } void main() { IO.println(positionOf("banana", 'n')); IO.println(positionOf("banana", 'z')); } ____________________________________ 8. What does this print? int minutesIn(int totalSeconds) { return totalSeconds / 60; } int secondsLeft(int totalSeconds) { return totalSeconds % 60; } void main() { int total = 500; IO.println(minutesIn(total) + " minutes and " + secondsLeft(total) + " seconds"); } ____________________________________ 9. What does this print? void addTen(int n) { n = n + 10; } void main() { int x = 5; addTen(x); IO.println(x); } ____________________________________ WRITING CODE ------------ 10. Complete the first line of a method that prints a greeting and gives nothing back: __________________ greet(String name) { IO.println("Hello, " + name + "!"); } 11. Complete the method so that it hands its answer back to the caller: int area(int width, int height) { __________________ width * height; } int room = area(12, 10); // room should hold 120 12. Write the line above the method that says what it does, in the form the course uses for a method's description: __________________ double celsiusToFahrenheit(double celsius) { return celsius * 9 / 5 + 32; } 13. Java calls main when the program starts. Complete main so that the program also runs showTitle: void main() { __________________ } void showTitle() { IO.println("Running the program."); } WHY IT WORKS ------------ 14. doublePrint is declared void, so it gives back no value. That is why int a = doublePrint(5); never runs: javac refuses it with a __________________ error before the program starts. 15. result is created inside calculate and stops existing once the method returns, because variables created inside a method have __________________ scope; the only way to get a value out of a method is to __________________ it. int calculate() { int result = 42; return result; } void main() { IO.println(calculate()); IO.println(result); } 16. Inside addTen, the line n = n + 10; gives the parameter n a new value inside the method; it does not reach back and change x, because assigning to a parameter only affects that method's own __________________ copy of the value.