user5770672
user5770672

Reputation:

How to call a method from another method in the same class (java)

I've been trying to understand how to call a method from inside another method of the same class by reading "Java how to program early objects".

I'm totally lost at the moment, the analogy used in the book makes it easy to visualise whats going on. However, translating that into code is challenging.

I have tried many times to figure this out and have gotten this far: P.S. for the sake of simplicity I have excluded code that I think is not important for my question...

import java.util.Scanner;

public class BuyAHouseInc
{
    private int housePrice;
    private int amountOfHouses;
    private int houseCounter;

    // method to enter the amount of houses on sale
    public void setAmountOfHouses()
    {
        // do stuff etc.
    }

    // method that sets the house price
    public void setHousePrice()
    {
        // do stuff etc.

        calculateFees(this.housePrice); // this is where I'm not sure...
    }

    //method to calculate fees and taxes
    public void calculateFees(int housePrice) // does this receive this.housePrice?
    {
      // do some stuff
    }

Tester code:

public class BuyAHouseIncTester
{
    public static void main(String[] args)
    {
        BuyAHouseInc client1 = new BuyAHouseInc("John","Doyle","15 Newton Drive\nDublin 5\n", 550000) // Create object 
        // set amount of houses on sale to client 1
        client1.setAmountOfHouses(); 

        // set house price for each of the houses added to database
        client1.setHousePrice();
    }
}

What is the code to call a method inside another method? Do the values of each individual house price get called?

Upvotes: 2

Views: 30264

Answers (4)

Daniel Yoon
Daniel Yoon

Reputation: 41

Please correct me if I'm wrong but is this a possible solution:

public void setHousePrice(int price)
{


    calculateFees(price); 
}


public void calculateFees(int housePrice) 
{
  // stuff with housePrice
}

then do

variable.setHousePrice(variable.getHousePrice);

Upvotes: 1

Christopher Schneider
Christopher Schneider

Reputation: 3915

Yes, your assumptions are correct.

However, there is something important to note. This code:

public void setHousePrice()
{
    // do stuff etc.

    calculateFees(this.housePrice); // <---This probably doesn't do what you think it does
}

// No. This doesn't receive this.housePrice. It receives the VALUE of this.housePrice. 
// It's a completely new variable
public void calculateFees(int housePrice) .
{
  // do some stuff
}

Java is pass by value. You're passing the value of housePrice, but you aren't modifying the original housePrice.

So if you "do some stuff" and modify the housePrice variable passed in, those changes aren't saved unless you are setting housePrice inside calculateFees.

In a case like this, it's best to manipulate any member variables through getters/setters. Convention, by the way, is that a setter should always take a value. This isn't arbitrary, as many libraries rely on set/get methods acting a certain way.

I've edited the above answer to be more clear.

Upvotes: 1

Charu Khurana
Charu Khurana

Reputation: 4551

you could simple call calculateFees(housePrice); as the only housePrice variable visible at point of calling is instance variable private int housePrice;

Assuming you've a constructor call to set housePrice based on how you're creating BuyAHouseInc

//method to calculate fees and taxes public void calculateFees(int housePrice) // does this receive this.housePrice? { // do some stuff }

Yes, this would receive housePrice passed via calculateFees(housePrice);

calculateFees(int housePrice) Local variable defined above is only visible inside calculateFees(int housePrice){...} method

Passing Information to a Method or a Constructor

UPDATE: Based on comments, you'd need to update your setter to pass house price

public void setHousePrice(int housePrice)
    {
        this.housePrice = housePrice;

        calculateFees(this.housePrice); 
    }

Upvotes: 2

Monroy
Monroy

Reputation: 430

public class BuyAHouseInc {
    private double housePrice;
    private int amountOfHouses;
    private int houseCounter;

    public BuyAHouseInc(double housePrice, int amountOfHouses, int houseCounter) {
        this.housePrice = housePrice;
        this.amountOfHouses = amountOfHouses;
        this.houseCounter = houseCounter;
    }

    // method that sets the house price
    public void generateHousePrice() {
        // do stuff etc.
        // you could also put the logic in the set method... Had to change the method name to 
        // generate as the getters and setter where created and the setter method is named
        // exactly like your previous method
        calculateFees(housePrice); // this is were I'm not sure...
    }

    // method to calculate fees and taxes
    public void calculateFees(double housePrice) // does this receive
                                                    // this.housePrice?
    {
        // do some stuff
    }

    public double getHousePrice() {
        return housePrice;
    }

    public void setHousePrice(double housePrice) {
        this.housePrice = housePrice;
    }

    public int getAmountOfHouses() {
        return amountOfHouses;
    }

    public void setAmountOfHouses(int amountOfHouses) {
        this.amountOfHouses = amountOfHouses;
    }

    public int getHouseCounter() {
        return houseCounter;
    }

    public void setHouseCounter(int houseCounter) {
        this.houseCounter = houseCounter;
    }

    @Override
    public String toString() {
    return "BuyAHouseInc [housePrice=" + housePrice + ", amountOfHouses=" + amountOfHouses + ", houseCounter="
            + houseCounter + "]";
}


}

Separate the two classes in different files. Another thing to point out is to use double for the price instead of int. You must also create the getters and setters to be able to get and set your properties. You must also add a constructor in the class. Then you can create an instance of the class in the main and call its methods. The main method would be something like below.

public class Main {

    public static void main(String... args) {
        BuyAHouseInc test = new BuyAHouseInc(200000, 4, 2);
        test.setAmountOfHouses(6); // changed from 4 to 6
        test.generateHousePrice();
        test.calculateFees(test.getHousePrice());
        test.toString();

    }
}

Hope you find this helpful and good luck ! I have edited the code and added the toString method to output information as a string.

Upvotes: 0

Related Questions