Calling a Void Method With Parameters

Emily Wilson
8 min read
Listen to this study note
Study Guide Overview
This study guide covers methods with parameters in Java. It explains method signatures, parameter passing, method overloading, and calling methods. It includes practice problems (MCQs and FRQs) and key exam tips for the AP Computer Science A exam.
AP Computer Science A: Methods with Parameters - The Night Before 🚀
Hey there, future AP Computer Science A master! Let's get you feeling super confident about methods with parameters. Think of this as your last-minute power-up before the big exam. We're going to make sure everything clicks into place, and you'll be ready to rock!
Understanding Methods and Parameters
Methods as Machines
Remember that methods are like little machines that perform specific tasks. They take inputs (parameters), follow a set of instructions (the code inside the method), and produce an output (which could be a printed line, a changed variable, or nothing at all for void methods).
Image: A visual representation of a method as a machine, taking inputs, processing them, and producing an output.
-
Inputs (Parameters): These are the values you pass into the method. They're like the ingredients you give to a recipe.
-
Rule (Method Code): This is the recipe itself—the step-by-step instructions the method follows.
-
Output: This is what the method produces, whether it's a calculation, a printed message, or a change in data.
The Power of Parameters
-
Flexibility: Parameters make methods flexible. With different inputs, you get different results. It's like using different numbers in a math function.
-
Consistency: The same set of parameters will always give you the same output, assuming you're calling the method on the same object.
-
No Parameters: If a method has no parameters, it will always behave the same way, like a machine that does the same thing every time.
Writing and Calling Methods with Parameters
Method Signature
Think of the method signature as the method's ID card. It tells you the method's name, the types of parameters it takes, and whether it returns a value (or void
).
public static void printRectanglePerimeter(double length, double width) {
System.out.println(2 * (length + width));
}
-
public static void
: These are modifiers (we'll cover those later). For now, just know they're part of the method's ID. -
printRectanglePerimeter
: This is the name of the method. -
(double length, double width)
: These are the parameters. Each parameter has a type (double
) and a name (length
,width
).
Calling the Method
To use a method, you call it by its name and provide the actual values for the parameters.
printRectanglePerimeter(1.5, 2.5);
This line calls the printRectanglePerimeter
method, passing 1.5 as the length
and 2.5 as the width
. The output will be 8.0. ## Overloading Methods
Just like constructors, methods can be overloaded. This means you can have multiple methods with the same name but different parameter lists.
Rules for Overloading
-
Different Parameter Types or Order: The key to overloading is that each method must have a unique combination of parameter types or a different order of types.
// Illegal Overloading (same parameter types and order)
// printRectanglePerimeter(double length, double width)
// printRectanglePerimeter(double width, double length)
Overloading Examples
Let's add some overloaded versions of printRectanglePerimeter()
:
public static void printRectanglePerimeter(double length, double width) {
System.out.println(2 * (length + width));
}
public static void printRectanglePerimeter(double side) { // Square
System.out.println(4 * side);
}
public static void printRectanglePerimeter() { // Default case
System.out.println(0);
}
-
printRectanglePerimeter(2.5)
would print10.0
(square). -
printRectanglePerimeter()
would print0
(no shape).
Method Overloading Analogy: Think of a vending machine. It can dispense different items (methods) based on the buttons you press (parameters). You press the 'soda' button, you get soda. You press the 'chips' button, you get chips. Same machine, different actions based on input.
Quick Review
-
Method Signature: Name + parameters (types and names).
-
Calling a Method: Use the method's name and provide values for the parameters.
-
Overloading: Same name, different parameter lists (types or order).
Practice Question
Methods Practice Problems
Multiple Choice Questions
Question 1:
public void millimetersToInches(double mm) {
double inches = mm * 0.0393701;
printInInches(mm, inches);
}
public void printInInches(double millimeters, double inches) {
System.out.print(millimeters + "-->" + inches);
}
Assume that the method called millimetersToInches(25.4)
appears in a method in the same class. What is printed as a result of the method call?
A. 25.4 –> 0.0393701 B. 25.4 –> 0.9990558 C. 25.4 –> 1.00000054 D. 0.0393701 –> 25.4 E. 0.9990558 –> 25.4
Answer: C. 25.4 –> 1.00000054
Question 2:
public void calculateArea(int length, int width) {
int rectangleArea = length * width;
/* INSERT CODE HERE */
}
public void printArea(int area) {
System.out.println("The area is: " + area);
}
Which of the following lines would go into /* INSERT CODE HERE */
in the method calculateArea
in order to call the printArea
method to print the area correctly?
A. printArea(retangleArea); B. printArea(length); C. printArea(width); D. calculateArea(area); E. calculateArea(length);
Answer: A. printArea(retangleArea);
Question 3:
public class MethodTrace {
public void add(int x, int y) {
System.out.print(x+y);
}
public void divide(int x, int y) {
System.out.println(x/y);
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.add(4, 2);
System.out.print(" and ");
traceObj.divide(5, 2);
}
}
What is the output of the following code?
A. 6 and 2.5 B. 6 and 2 C. 6 and 3 D. 6 and 3.5 E. Nothing, it does not compile.
Answer: B. 6 and 2
Free Response Question
Question:
Write a class called Calculator
with the following methods:
-
A method
add
that takes two integers as parameters and returns their sum. -
A method
subtract
that takes two integers as parameters and returns their difference. -
A method
multiply
that takes two doubles as parameters and returns their product. -
A method
divide
that takes two doubles as parameters and returns their quotient. Handle the case where the divisor is zero by returning 0.0. 5. A methodprintResult
that takes a double as a parameter and prints it to the console with a descriptive message.
public class Calculator {
public int add(int num1, int num2) {
return num1 + num2;
}
public int subtract(int num1, int num2) {
return num1 - num2;
}
public double multiply(double num1, double num2) {
return num1 * num2;
}
public double divide(double num1, double num2) {
if (num2 == 0) {
return 0.0;
} else {
return num1 / num2;
}
}
public void printResult(double result) {
System.out.println("The result is: " + result);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
int sum = calc.add(5, 3);
int difference = calc.subtract(10, 4);
double product = calc.multiply(2.5, 4.0);
double quotient = calc.divide(10.0, 2.0);
double zeroDivision = calc.divide(5.0, 0.0);
calc.printResult(sum);
calc.printResult(difference);
calc.printResult(product);
calc.printResult(quotient);
calc.printResult(zeroDivision);
}
}
Scoring Rubric:
-
add method (1 point): Correct method signature, returns the sum of two integers.
-
subtract method (1 point): Correct method signature, returns the difference of two integers.
-
multiply method (1 point): Correct method signature, returns the product of two doubles.
-
divide method (2 points): Correct method signature, returns the quotient of two doubles, handles division by zero.
-
printResult method (1 point): Correct method signature, prints the result with a descriptive message.
-
main method (2 points): Creates a Calculator object, calls the methods, and prints the results.
Final Exam Focus
-
High-Priority Topics: Method signatures, parameter passing, method overloading.
-
Question Types: MCQs testing method calls, FRQs requiring method implementation, tracing code with method calls.
-
Time Management: Quickly identify method signatures and parameter types. Practice tracing code to predict outputs.
-
Common Pitfalls: Confusing parameter types, incorrect method calls, not handling edge cases (like division by zero).
Last-Minute Tips:
- Review method signature syntax: Ensure you understand how to define methods with parameters and return types.
- Practice tracing method calls: Work through examples to predict output, especially with overloaded methods.
- Pay attention to data types: Be mindful of integer vs. double division, and type compatibility when passing parameters.
- Don't panic: You've got this! Take a deep breath, read carefully, and trust your preparation. You're ready to ace this exam!
You've got this! Go get that 5! 🌟

How are we doing?
Give us your feedback and let us know how we can improve