-3

alright so i have already made a working compound interest rate program. but now for this program i have to re write the program using more methods. using

private static double FVCALC(..) 

and

private static double validate(........)

i dont quite understand how i need to do this. the current code i have only lets me input the 3 values of interest rate and it stops. is it because of the private mehtods? im not sure what to do and i have searched for 3 days now.

bottom line is. my code is not working the way i want it to.

public class interest_rate 
{

    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
            double i;
            double n;
            double FVCalc;
            double PV;
            System.out.print("please enter value for n (years): ");
            n = input.nextInt();
            System.out.print("please enter interest rate: ");
            i=input.nextDouble();
            System.out.print("please enter Present Value: ");
            PV = input.nextDouble();        


    }
    private static double validate (double upLimit, double lowLimit, double PV)
    {

        upLimit=100000.00;
        lowLimit=0.00;
        while(PV>upLimit|| PV<lowLimit)
        {
            System.out.print("please enter value between "+upLimit+" and "+lowLimit);
            System.out.print("please enter PV");
            PV=input.nextDouble();
        }



        return PV;


    }




    private static double FVCalc(double PV, double i, double n, double FV)
    {


        FV = PV*Math.pow(1+(i/100), n);

        return(FV);
    }

}
Rogue
  • 11,105
  • 5
  • 45
  • 71
Kyle
  • 9
  • 2
  • the program needs to validate PV between 0-100,000 – Kyle Jul 31 '16 at 13:50
  • also i am a noob at this – Kyle Jul 31 '16 at 13:51
  • 1
    Please edit your post instead of adding comments. Also, don't give unnecessary information, solely the problem – Dacaspex Jul 31 '16 at 13:53
  • Another thing: please try to format your code as best as possible. You want us to help; so you please spend some time making your code as readable as possible! – GhostCat Jul 31 '16 at 13:57
  • thanks for all the help ill make sure to make it pretty. ] – Kyle Jul 31 '16 at 14:24
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – GhostCat Jul 31 '16 at 14:44
  • Interestingly enough, your way of coding ... did hide the problem that is actually pretty obvious: Java does "pass by value". And therefore, when you use some PV or FV as argument to a method call ... the caller does **not** see when the called method changes its parameters! – GhostCat Jul 31 '16 at 14:45
  • 1
    You never call the two methods you wrote... – OneCricketeer Jul 31 '16 at 14:45

2 Answers2

0

First, you need to call the methods in main.

Secondly, you cant pass in PV, then reassign it and expect the value to update.

For example..

private static double validate (double upLimit, double lowLimit, double PV)

You need to call this in main like so

PV = 0.0; // some double value 
double validated = validate(0,100000); // return the value, don't pass it in

And remove these lines in that method because overriding parameters is typically bad.

upLimit=100000.00;
lowLimit=0.00;

Next, add a field for your values that you want to use throughout the class.

public class InterestRate 
{

    static double pv , fvCalc;
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {

    } 

And remove these lines in main and use those two class variables instead

double FVCalc;
double PV;

Additionally, I don't see a reason to store fvCalc as a variable. You can just calculate it

private static double fvCalc(double pV, double i, double n)
{
    return pV*Math.pow(1+(i/100), n);   
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

i believe i have figured it out. and i made it pretty. excuse the notes. i wanted to keep them there to see what i did wrong. thanks all.

public class interest_rate 
{
    static double pV , fvCalc, i, n;
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        double validated;
        double calculated;
        double i;
        double n;
        //double fV; 
        //double pV1;
        System.out.print("please enter value for n (years): ");
        n = input.nextInt();
        System.out.print("please enter interest rate: ");
        i=input.nextDouble();
        validated = validate(0,100000); 
        System.out.println("pV is validated and equal to: "+validated);
        calculated= fvCalc(validated,i,n);
        System.out.println("your calulation for interest is: "+calculated);
    }
    private static double validate(double upLimit, double lowLimit) {
    double pV;
    System.out.print("please enter pV");
    pV=input.nextDouble();
    while(pV<upLimit|| pV>lowLimit)
    {
        System.out.print("please enter value between "+upLimit+" and "+lowLimit);
        System.out.println("please enter pV");
        pV=input.nextDouble();
    }
    return pV;
    }
    private static double fvCalc(double pV, double i, double n)
    {
        double fV;
        fV=pV*Math.pow(1+(i/100), n);   
        return fV;
    }

}
Kyle
  • 9
  • 2