Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Methods with Parameters

  1. Mar 29, 2012 #1
    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?
     
  2. jcsd
  3. Mar 29, 2012 #2

    Mark44

    Staff: Mentor

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

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java Methods with Parameters
Loading...