简体   繁体   中英

Calling methods with objects trouble

So, I need to do this for part of my homework :

Create a method in the Customer class called hasMoreMoneyThan(Customer c) which returns true if the customer calling the method has more money than the customer c , otherwise it should return false .

I am looking to be pointed in the right direction for the line that says "the customer calling the method"

This is very confusing to me and doesn't make sense, this my Customer is a class.

Here is the necessary code :

public class Customer
{ 
    private String name;
    private int age;
    private float money;

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }


    public Customer(String n, int a, float m)
    {
        name = n;
        age = a;
        money = m;

   }

I started writing the method:

public boolean hasMoreMoneyThan(Customer c)
{

}

But am not sure how to call that with my Customer object (which I think the question is asking.

Other relevant code :

public class StoreTestProgram {

    public static void main(String args[]) {
        Customer[]    result;
        Store         walmart;

        walmart = new Store("Walmart off Innes");
        walmart.addCustomer(new Customer("Amie", 14, 100));

    }
}

When a method is called on an object, the objects variables are in the current scope. In this case, the 'customer calling the method' is the object that the method is being called on (object being an instance of the class).

So, if boolean hasMoreMoneyThan(Customer c) is being called on Customer a , then you should think of it as asking Customer a has more money than Customer c? .

You can use the this keyword to refer to the current object (to help the reader differentiate from Customer c ).

So, in your hasMoreMoneyThan method, you can compare this.money with c.money .

And to call this method, you need a reference to the current customer and the customer you want to compare with. You could do something like:

Customer currentCustomer = new Customer(...
Customer customerToCompareWith = new Customer(...

if (currentCustomer.hasMoreMoneyThan(customerToCompareWith)) {
    // do something
}

Edit Let's try a different example. Let's say you want a method to know whether a customer is older than another customer. That code might look like:

public boolean isOlderThan(Customer c) {
    return this.age > c.age;
}

And to call the method:

if (currentCustomer.isOlderThan(customerToCompareWith)) {
    // the current customer is older
} else {
    // the current customer is not older
}

this is how you reference an object from methods that are members of an object. this.money ><=? c.money this.money ><=? c.money if in your constructor you you used public Customer(String name, int age, float money) you would use this.name= name instead of name= n to clear up ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM