How does the 'amount' parameter in the recordPurchase method work?

In summary, the amount parameter is a placeholder for the actual value that will be used when calling the recordPurchase method. When called, the method will add the amount parameter to the existing purchase total and update the purchase variable.
  • #1
whitehorsey
192
0
I'm having trouble with understanding how methods that have a parameter work.
For example,
public class Register
{
private double purchase;
private double payment;
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;


public Register()
{
purchase = 0;
payment = 0;
}

public void recordPurchase(double amount)
{
double newTotal = purchase + amount;
purchase = newTotal;
}

What does the value amount in the parameters equal? Is it 0?
 
Technology news on Phys.org
  • #2
The amount parameter is a placeholder for whatever value is used as the actual parameter when the method is called.

What you have is merely the definition of the recordPurchase method. It would be called like this.
Code:
Register reg = new Register(); // Create a Register object.
reg.recordPurchase(25.0);
Inside the method the first time it is called, newTotal would be set to 25.0 + 0.0, and purchase would be set to 25.0.
 

1. What are Java methods with parameters?

Java methods with parameters are blocks of code that can be called and executed with specific inputs, or parameters, that are passed into them. These parameters allow the method to perform different actions or calculations based on the values provided.

2. How do you define parameters for a Java method?

To define parameters for a Java method, you need to include a list of parameter names and data types within the parentheses after the method name. For example: public void myMethod(int num1, int num2) would define a method that takes in two integer parameters.

3. Can Java methods have multiple parameters?

Yes, Java methods can have multiple parameters. The number of parameters and their data types must be specified when defining the method. You can have as many parameters as needed, as long as the method can still perform its intended task.

4. How do you pass values into a Java method with parameters?

To pass values into a Java method with parameters, you need to provide the arguments within the parentheses when calling the method. For example: myMethod(5, 10) would pass the values 5 and 10 into the myMethod method as the first and second parameters, respectively.

5. What is the purpose of using parameters in Java methods?

The purpose of using parameters in Java methods is to make the code more flexible and reusable. By passing different values into the method, it can perform different actions without having to write separate methods for each scenario. This also allows for easier maintenance and updates to the code in the future.

Similar threads

  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Replies
18
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top