Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

2.4 Calling a Void Method With Parameters

7 min readdecember 28, 2022

Athena_Codes

Athena_Codes

user_sophia9212

user_sophia9212

Athena_Codes

Athena_Codes

user_sophia9212

user_sophia9212

Now, we will discuss adding parameters to the mentioned in the previous section. With parameters, the method works like a function in math—different input parameters can give different , 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

Courtesy of Illustrative Mathematics.

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 is the same as a , 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) {
  

To call the method, use the , 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) {
  

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 (int length, int width)

{

int rectangleArea = length * width;

/* INSERT CODE HERE */

}

public void (int area)

{

("The area is: " + area);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the area correctly?

A. (retangleArea);

B. (length);

C. (width);

D. (area);

E. (length);

Answer: A. (retangleArea);

Consider the following methods, which appear in the same class:

public void (int num1, int num2)

{

int numSum = num1 + num2;

/* INSERT CODE HERE */

}

public void (int sum)

{

("The sum is: " + sum);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the sum correctly?

A. (num2);

B. (num1);

C. (numSum);

D. (sum);

E. (num1);

Answer: C. (numSum);

Consider the following methods, which appear in the same class:

public void (int num1, int num2, int num3)

{

int numAverage = (num1 + num2 + num3) / 3;

/* INSERT CODE HERE */

}

public void (int average)

{

("The average is: " + average);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the average correctly?

A. (average);

B. (num1);

C. (num2);

D. (num3);

E. (numAverage);

Answer: E. (numAverage);

Consider the following methods, which appear in the same class:

public void (int price, int discount)

{

int calculatedPrice = price - (price * discount / 100);

/* INSERT CODE HERE */

}

public void printDiscountedPrice(int discountedPrice)

{

("The discounted price is: " + discountedPrice);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the printDiscountedPrice method to print the discounted price correctly?

A. printDiscountedPrice(calculatedPrice);

B. printDiscountedPrice(price);

C. printDiscountedPrice(discount);

D. (calculatedPrice);

E. (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)

{

("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)

{

(x+y);

}

public void subtract(int x, int y)

{

(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)

{

(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)

{

(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)

{

(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

Key Terms to Review (13)

CalculateArea

: CalculateArea refers to the process of determining the size or extent of an enclosed shape or region using mathematical formulas or algorithms. It is commonly used in programming when working with geometric shapes.

calculateAverage

: CalculateAverage refers to the process of finding the mean value of a set of numbers by adding them up and dividing by the total count.

calculateDiscount

: CalculateDiscount means determining how much money can be saved from an original price by applying a percentage reduction.

calculateSum

: CalculateSum is an operation that involves adding up multiple numbers together to obtain their total sum.

Constructor Signature

: A constructor signature refers to the unique combination of a class's name and its parameter types. It helps create instances (objects) of that class with specific initial values.

Method Signature

: A method signature refers to the unique combination of a method's name and its parameter types. It helps distinguish one method from another.

Outputs

: Outputs refer to the results or values that are produced by a program or function when it is executed.

Overloading Methods

: Overloading methods refers to defining multiple methods with the same name but different parameters in a class. This allows for flexibility and reusability by providing different ways to perform similar tasks.

printArea

: The term printArea refers to a function or method that outputs the calculated area of a shape onto a physical medium, such as paper or a computer screen.

printAverage

: PrintAverage refers to displaying or outputting the calculated average value on a screen or paper.

printSum

: PrintSum refers to an action where the computed sum of multiple numbers is displayed on some physical medium for viewing purposes.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

Void Methods

: Void methods are methods in programming that do not return a value. They perform a specific task or action, but they do not produce any output.

2.4 Calling a Void Method With Parameters

7 min readdecember 28, 2022

Athena_Codes

Athena_Codes

user_sophia9212

user_sophia9212

Athena_Codes

Athena_Codes

user_sophia9212

user_sophia9212

Now, we will discuss adding parameters to the mentioned in the previous section. With parameters, the method works like a function in math—different input parameters can give different , 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

Courtesy of Illustrative Mathematics.

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 is the same as a , 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) {
  

To call the method, use the , 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) {
  

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 (int length, int width)

{

int rectangleArea = length * width;

/* INSERT CODE HERE */

}

public void (int area)

{

("The area is: " + area);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the area correctly?

A. (retangleArea);

B. (length);

C. (width);

D. (area);

E. (length);

Answer: A. (retangleArea);

Consider the following methods, which appear in the same class:

public void (int num1, int num2)

{

int numSum = num1 + num2;

/* INSERT CODE HERE */

}

public void (int sum)

{

("The sum is: " + sum);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the sum correctly?

A. (num2);

B. (num1);

C. (numSum);

D. (sum);

E. (num1);

Answer: C. (numSum);

Consider the following methods, which appear in the same class:

public void (int num1, int num2, int num3)

{

int numAverage = (num1 + num2 + num3) / 3;

/* INSERT CODE HERE */

}

public void (int average)

{

("The average is: " + average);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the method to print the average correctly?

A. (average);

B. (num1);

C. (num2);

D. (num3);

E. (numAverage);

Answer: E. (numAverage);

Consider the following methods, which appear in the same class:

public void (int price, int discount)

{

int calculatedPrice = price - (price * discount / 100);

/* INSERT CODE HERE */

}

public void printDiscountedPrice(int discountedPrice)

{

("The discounted price is: " + discountedPrice);

}

Which of the following lines would go into /* INSERT CODE HERE */ in the method in order to call the printDiscountedPrice method to print the discounted price correctly?

A. printDiscountedPrice(calculatedPrice);

B. printDiscountedPrice(price);

C. printDiscountedPrice(discount);

D. (calculatedPrice);

E. (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)

{

("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)

{

(x+y);

}

public void subtract(int x, int y)

{

(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)

{

(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)

{

(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)

{

(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

Key Terms to Review (13)

CalculateArea

: CalculateArea refers to the process of determining the size or extent of an enclosed shape or region using mathematical formulas or algorithms. It is commonly used in programming when working with geometric shapes.

calculateAverage

: CalculateAverage refers to the process of finding the mean value of a set of numbers by adding them up and dividing by the total count.

calculateDiscount

: CalculateDiscount means determining how much money can be saved from an original price by applying a percentage reduction.

calculateSum

: CalculateSum is an operation that involves adding up multiple numbers together to obtain their total sum.

Constructor Signature

: A constructor signature refers to the unique combination of a class's name and its parameter types. It helps create instances (objects) of that class with specific initial values.

Method Signature

: A method signature refers to the unique combination of a method's name and its parameter types. It helps distinguish one method from another.

Outputs

: Outputs refer to the results or values that are produced by a program or function when it is executed.

Overloading Methods

: Overloading methods refers to defining multiple methods with the same name but different parameters in a class. This allows for flexibility and reusability by providing different ways to perform similar tasks.

printArea

: The term printArea refers to a function or method that outputs the calculated area of a shape onto a physical medium, such as paper or a computer screen.

printAverage

: PrintAverage refers to displaying or outputting the calculated average value on a screen or paper.

printSum

: PrintSum refers to an action where the computed sum of multiple numbers is displayed on some physical medium for viewing purposes.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

Void Methods

: Void methods are methods in programming that do not return a value. They perform a specific task or action, but they do not produce any output.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.