πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.4 Calling a Void Method With Parameters

7 min readβ€’june 18, 2024

Avanish Gupta

Avanish Gupta

user_sophia9212

user_sophia9212


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Now, we will discuss adding parameters to the void methods mentioned in the previous section. With parameters, the method works like a function in mathβ€”different input parameters can give different outputs, but the same set of parameters give the same behavior. If there are no parameters, then the method always has the same behavior. All of these are assuming that these methods are calling on the same object because different objects have different characteristics, so they would return different behaviors otherwise.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2Fcsa%202-71Riaete0lCr.png?alt=media&token=5a0b41c2-dde5-4b93-ab92-9802555aee07
This machine serves as a model for how methods work. The inputs are our parameters, the rule is the code inside our method, and the output is the result of the method, which may be a printed line of text or a changed variable.

How To Write and Call Methods With Parameters

The method signature is the same as a constructor signature, with the name of the method followed by the parameters and their types in parentheses. For example, here is a method that prints the perimeter of a rectangle:
public static void printRectanglePerimeter(double length, double width) { System.out.println(2 * (length + width)); }
To call the method, use the method signature, but plug in the values in place of the variables. Here is how:
printRectanglePerimeter(1.5, 2.5);
This will print out 8.0, which is the result of the calculation done in the method.

Overloading Methods

Like constructors, methods can be overloaded as well. Like constructors, we need to have a different order of parameter types. Thus, the following two overloaded are illegal since the orders of parameter types for both are both (double, double):
printRectanglePerimeter(double length, double width) printRectanglePerimeter(double width, double length)
Here is how to write methods that are overloaded with the printRectanglePerimeter() method with the following assumptions:
  1. 1 Parameter = Rectangle is a square
  1. 0 Parameters = No shape exists (This will be the default method call)
public static void printRectanglePerimeter(double length, double width) { System.out.println(2 * (length + width)); } public static void printRectanglePerimeter(double side) { System.out.println(4 * side); } public static void printRectanglePerimeter() { System.out.println(0);
According to the overloaded method that we have just written, a call to printRectanglePerimeter(2.5) would print out 10.0, while a call to just printRectanglePerimeter() would print out 0.

Methods Practice Problems

Consider the following methods:
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


Consider the following methods:
public void milesToKilometers(double miles)
{
double km = miles * 1.60934;
printInKilometers(miles, km);
}
public void printInKilometers(double miles, double km)
{
System.out.print(miles + "-->" + km);
}
Assume that the method called milesToKilometers(1) appears in a method in the same class. What is printed as a result of the method call?
A. 1 –> 1.60934
B. 1 –> 0.621371
C. 1.60934 –> 1
D. 0.621371 –> 1
E. 1.60934 –> 0.621371

Answer: A. 1 –> 1.60934


Consider the following methods:
public void fahrenheitToCelsius(double fahrenheit)
{
double celsius = (fahrenheit - 32) * (5.0/9.0);
printInCelsius(fahrenheit, celsius);
}
public void printInCelsius(double fahrenheit, double celsius)
{
System.out.print(fahrenheit + "-->" + celsius);
}
Assume that the method called fahrenheitToCelsius(32) appears in a method in the same class. What is printed as a result of the method call?
A. 32 –> 0
B. 32 –> 5.0/9.0
C. 0 –> 32
D. 5.0/9.0 –> 32
E. 0 –> 5.0/9.0

Answer: A. 32 –> 0


Consider the following methods:
public void poundsToKilograms(double pounds)
{
double kilograms = pounds * 0.453592;
printInKilograms(pounds, kilograms);
}
public void printInKilograms(double pounds, double kilograms)
{
System.out.print(pounds + "-->" + kilograms);
}
Assume that the method called poundsToKilograms(2) appears in a method in the same class. What is printed as a result of the method call?
A. 2 –> 0.453592
B. 2 –> 0.907184
C. 0.453592 –> 2
D. 0.907184 –> 2
E. 0.453592 –> 0.907184

Answer: B. 2 –> 0.907184


Consider the following methods:
public void litersToGallons(double liters)
{
double gallons = liters * 0.264172;
printInGallons(liters, gallons);
}
public void printInGallons(double liters, double gallons)
{
System.out.print(liters + "-->" + gallons);
}
Assume that the method called litersToGallons(3.785) appears in a method in the same class. What is printed as a result of the method call?
A. 3.785 –> 1.2
B. 3.785 –> 0.264172
C. 1 –> 3.785
D. 0.264172 –> 3.785
E. 3.785 –> 0.99989102

Answer: E. 3.785 –> 0.99989102


Consider the following methods, which appear in the same class:
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);


Consider the following methods, which appear in the same class:
public void calculateSum(int num1, int num2)
{
int numSum = num1 + num2;
/* INSERT CODE HERE */
}
public void printSum(int sum)
{
System.out.println("The sum is: " + sum);
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method calculateSum in order to call the printSum method to print the sum correctly?
A. printSum(num2);
B. printSum(num1);
C. printSum(numSum);
D. calculateSum(sum);
E. calculateSum(num1);

Answer: C. printSum(numSum);


Consider the following methods, which appear in the same class:
public void calculateAverage(int num1, int num2, int num3)
{
int numAverage = (num1 + num2 + num3) / 3;
/* INSERT CODE HERE */
}
public void printAverage(int average)
{
System.out.println("The average is: " + average);
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method calculateAverage in order to call the printAverage method to print the average correctly?
A. calculateAverage(average);
B. printAverage(num1);
C. printAverage(num2);
D. printAverage(num3);
E. printAverage(numAverage);

Answer: E. printAverage(numAverage);


Consider the following methods, which appear in the same class:
public void calculateDiscount(int price, int discount)
{
int calculatedPrice = price - (price * discount / 100);
/* INSERT CODE HERE */
}
public void printDiscountedPrice(int discountedPrice)
{
System.out.println("The discounted price is: " + discountedPrice);
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method calculateDiscount in order to call the printDiscountedPrice method to print the discounted price correctly?
A. printDiscountedPrice(calculatedPrice);
B. printDiscountedPrice(price);
C. printDiscountedPrice(discount);
D. calculateDiscount(calculatedPrice);
E. calculateDiscount(price);

Answer: A. printDiscountedPrice(calculatedPrice);


Consider the following methods, which appear in the same class:
public void calculateVolume(int length, int width, int height)
{
int boxVolume = length * width * height;
/* INSERT CODE HERE */
}
public void printVolume(int volume)
{
System.out.println("The volume is: " + volume);
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method calculateVolume in order to call the printVolume method to print the volume correctly?
A. printVolume(width);
B. printVolume(volume);
C. printVolume(boxVolume);
D. printVolume(height);
E. calculateVolume(boxVolume);

Answer: C. printVolume(boxVolume);


What does the following code output:
public class MethodTrace
{
public void add(int x, int y)
{
System.out.println(x+y);
}
public void subtract(int x, int y)
{
System.out.println(x-y);
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.add(5, 3);
System.out.print(" and ");
traceObj.subtract(5, 3);
}
}
A. 8 and 2
B. 2 and 8
C. 8 and -2
D. 2 and -8
E. Nothing, it does not compile.

Answer: A. 8 and 2


What is the output of the following code:
public class MethodTrace
{
public void concatenate(String x, String y)
{
System.out.println(x+y);
}
public void repeat(String x, int y)
{
for(int i=0; i<y; i++) {
System.out.print(x);
}
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.concatenate("Hello", "World");
System.out.print(" and ");
traceObj.repeat("Hello", 3);
}
}
A. HelloWorld and WorldHello
B. WorldHello and HelloHelloHello
C. HelloWorld and HelloHelloHello
D. WorldHello and WorldWorldWorld
E. Nothing, it does not compile.

Answer: C. HelloWorld and HelloHelloHello


What is the output of the following code:
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);
}
}
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


What is the output of the following code:
public class MethodTrace
{
public void concatenate(String x, String y)
{
System.out.print(x+y);
}
public void toUpperCase(String x)
{
System.out.println(x.toUpperCase());
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.concatenate("good", "bye");
System.out.print(" and ");
traceObj.toUpperCase("see you later");
}
}
A. goodbye and SEE YOU LATER
B. byegood and SEE YOU LATER
C. goodbye and LATER YOU SEE
D. byegood and LATER

Answer: A. goodbye and SEE YOU LATER
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.