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

  • Context: Java 
  • Thread starter Thread starter whitehorsey
  • Start date Start date
  • Tags Tags
    Java Parameters
Click For Summary
SUMMARY

The 'amount' parameter in the recordPurchase method of the Register class serves as a placeholder for the actual value passed during the method call. When the method is invoked, such as with reg.recordPurchase(25.0), the amount parameter takes the value of 25.0. This value is then added to the current purchase total, updating it accordingly. The initial value of purchase is 0.0, resulting in a new total of 25.0 after the first method call.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with object-oriented programming concepts
  • Basic knowledge of method parameters and return values
  • Experience with class and object instantiation in Java
NEXT STEPS
  • Explore Java method overloading and its implications
  • Learn about encapsulation and data hiding in Java classes
  • Investigate the use of constructors in Java for object initialization
  • Study the Java Collections Framework for managing multiple purchases
USEFUL FOR

Java developers, computer science students, and anyone seeking to understand method parameters and object-oriented programming principles in Java.

whitehorsey
Messages
188
Reaction score
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
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K