Java Methods with Parameters

  • Java
  • Thread starter whitehorsey
  • Start date
  • #1
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?
 

Answers and Replies

  • #2
33,961
5,615
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.
 

Related Threads on Java Methods with Parameters

Replies
3
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
3
Views
6K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
5
Views
13K
Replies
9
Views
4K
  • Last Post
Replies
5
Views
11K
Replies
4
Views
2K
Replies
4
Views
78
Top