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

  • Thread starter Thread starter whitehorsey
  • Start date Start date
  • Tags Tags
    Java Parameters
AI Thread Summary
The discussion focuses on understanding method parameters in programming, specifically in the context of a Register class. The method `recordPurchase(double amount)` uses a parameter named `amount`, which serves as a placeholder for the actual value passed when the method is invoked. In the example provided, when a new Register object is created and the method is called with a value of 25.0, the `amount` parameter takes on this value. Consequently, within the method, `newTotal` is calculated by adding the current `purchase` value (initially 0) to `amount`, resulting in `purchase` being updated to 25.0. This illustrates how parameters function as dynamic inputs that influence the method's behavior during execution.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top