1

Ok, I am teaching myself java and I am trying to write a recursive method that can count how many times it is called/used. This is my code so far:

public class FinancialCompany
{
  public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    double total = Sum(money);
    Months();

    StdOut.printf("you have reached an amount of $%8.2f", total);
    StdOut.println();
  }
  public static double Sum(double money)
  {
    double total = (money * 0.01) + money;
    if(total >= 1000000)
    {
      return total;
    }
    else
    {  
      total =(money * 0.01) + money;
      return Sum(total);
    }
  }

  public static int counter = 0;

  public static void Months()
  {
   counter++;
   StdOut.println("It should take about " + counter + " month(s) to reach your goal of $1,000,000");
  }

}

This is the output:

Enter your monthly investment: 1000 (I chose 1000)
It should take about 1 month(s) to reach your goal of $1,000,000
you have reached an amount of $1007754.58

Every time I run this is prints out final amount but I want it to tell me how many months it took to get there but all it prints out is 1 month. Any help will be greatly appreciated!

**

Completed Code(Thanks to everyone's contributions!)

**

public class FinancialCompany
{

  public static int counter = 0;

  public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    double investment = money;
    double total = Sum(money, investment);    
    StdOut.println("It should take about " + counter + " month(s) to reach your goal of $1,000,000");
  }
  public static double Sum(double money, double investment)
  {
    counter++;
    double total = (money * 0.01) + money;
    if(total >= 1000000)
    {
      return total;
    }
    else
    {  
      return Sum(total + investment ,investment);
    }
  }
}
  • Zach

5 Answers 5

2

Couldn't you just make a global variable like counter outside of the methods? Sort of like this.

public class testThingy {
    public static int counter = 0;
    /* main method and other methods here */
}

Just add 1 to counter every time the method is called (within the method you want counted) and it should update the global variable.

6
  • I did this and it counts now but it is not very accurate.
    – zbfarris
    Apr 26, 2013 at 0:27
  • Strange. So you have a global variable and you update that global variable within the method you are trying to count? So within Months() you have the global variable++? Apr 26, 2013 at 0:43
  • It was counting funny because I goofed up on my code in another area. But what you suggested actually got my counter to work so thank you! I put my completed code as an edit on my original post if you want to take a look at it.
    – zbfarris
    Apr 26, 2013 at 15:16
  • Awesome to hear! Just select my answer as correct with the checkmark please! You're welcome! :) Apr 26, 2013 at 15:21
  • 1
    @KranthiSama Like a Tomcat or Glassfish server? So you just want this number to be persistent for how many times it is called throughout all of time? You would have to save the number off into a file somewhere or into a database and have it load in on application launch. Jun 23, 2014 at 13:18
0

Two ways:

1) is to have a variable that exists outside of the method. (If the method lives on an object, you can make it a variable of the object, for example.) At the start of the method, ++variable. Then you can see what value it ends at.

2) is to make the return value, or part of the return value, the number of calls of all of your children. As in:

-If you do not call yourself, return 1.

-If you do call yourself, return the sum of all the return values of all your self-calls (+1 if you also want to count calls of the method part-way through the tree)

1
  • I have tried this in past versions of this code but i could never get it to work
    – zbfarris
    Apr 26, 2013 at 0:25
0

Well since you only call Months() once per execution of the application, then counter increments only once! If you want to persist counter between executions of your application, you will have to save the value at the end of the application, and load it in again when you run the application again.

0

Month() only gets called once in your code, so it sees that counter == 0, then increments that giving counter == 1. This is the value being printed.

So, your counter++ is in the wrong place. It should be inside at the top of the Sum() method. As this is the method being recursively called, this is where the counter should increment.

Now when Month() gets called it will have the proper value.

2
  • I tried this and to test it i put in a $500,000 initial investment and it tells me it should take 70 months to reach $1,000,000.
    – zbfarris
    Apr 25, 2013 at 23:56
  • if you look at the completed code that I put in as an edit to my original post, I took out the Month() method and made the whole program alot shorter and simpler but I did use what you suggested by putting my counter++ at the top of my Sum() method.
    – zbfarris
    Apr 26, 2013 at 15:19
0

I believe that this should give you what you are looking for.

public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    int months = 0;
    while(money < 10000){
        money += invest(money);
        months++;
    }

    StdOut.println("It should take about " + String.valueOf(months) + " month(s) to reach your goal of $1,000,000");
    StdOut.printf("you have reached an amount of $%8.2f", money);
    StdOut.println();
  }

  private static double invest(double investment){
    return (investment + (investement * 0.01));
  }
2
  • granted this works but it does not have recursion in it. I want to write this program using recursion.
    – zbfarris
    Apr 26, 2013 at 0:26
  • Guess i overlooked that important key word in the question. Glad you were able to get an answer though. cheers!
    – Aossey
    Apr 27, 2013 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.