Search code examples
javamathscopenaming-conventionsinstance-variables

Java Method not getting correct Value,


I was experimenting with a program idea and therefore have way to many classes, which very well could have been methods, however in my CF method (Don't worry about the naming conventions they were changed to post online) however, when I try to set cF to cF=getTi-getTe the values from the getters always print 0 but this is only in the calculate cF method. If I print the getters and setters in the main method alone they print out with the correct numbers.

public class CF {

    private double cF;

    Income rI= new Income();

    Expenses e = new Expenses();


    public double getCF() {
        return cF;
    }

        public void setCF(double cF) {
        this.cF = cF;
    }

    public CF(){
    //cF=0;
    }
    public double calculateCF(){



       cF= rI.getTi()-e.getTe();// this line will return 0 no matter what

        return cF;
    }
}

public class Expenses {

    private double tExpenses,exp1,exp2;
    public Expenses(){

    tExpenses=exp1=exp2=0;



    }
    public double calculateTotalExpenses(){

        tExpenses=exp1+exp2;

        return tExpenses;
    }




    /**
     * @return the tExpenses
     */
    public double getTe() {// again don't worry about naming conventions //they are correct in the IDE
        return tExpenses;
    }

    /**
     * @param tExpenses the tExpenses to set
     */
    public void setTe(double tExpenses) {
        this.tExpenses = tExpenses;
    }
}

public class RI {

    private double rI;

    private double incomeMisc;

    private double tIncome;

    public RI(){
    rI=0;
    incomeMisc=0;
    tIncome=0;
    }

    public double calculateRI(){

        tIncome= rI+incomeMisc;

    return tIncome;
    }


    /**
     * @return the incomeMisc
     */
    public double getIncomeMisc() {
        return incomeMisc;
    }

    /**
     * @param incomeMisc the incomeMisc to set
     */
    public void setIncomeMisc(double incomeMisc) {
        this.incomeMisc = incomeMisc;
    }

    /**
     * @return the tIncome
     */
    public double gettIncome() {
        return tIncome;
    }

    /**
     * @param tIncome the tIncome to set
     */
    public void settIncome(double tIncome) {
        this.tIncome = tIncome;
    }


}
public class FSAProgram {


    public static void main(String[] args) {
        RI rI = new RI();
        Expenses e = new Expenses();
        CF cF= new CF();

        System.out.println("Enter RI");
        Scanner k = new Scanner(System.in);
        rI.I(k.nextDouble());
        System.out.println("Enter Misc Income");
        rI.setIncomeMisc(k.nextDouble());
        rI.calculateRI();
        System.out.println("Total Income is: "+ rI.getTi());

        System.out.println("Enter expense");
        e.setExp1(k.nextDouble());// I know this isn't correct its because // I changed the name to post this question
        e.calculateTotalExpenses();
        System.out.println("Total Expenses :"+ e.gettExpenses());
        //cF.calculateCF();
        System.out.println(""+(e.gettExpenses() +" "+  rI.gettIncome()));

// Prints fine when last statement is executed
//The next statement is what returns 0 unfortunately

        System.out.println("CF: "+ cF.calculateCF());


    }

}

Sample output **Enter RI

2000

Enter Misc Income

0

Total Income is: 2000.0

Enter Exp1// these are a combination of other things in the actual program 900

Total Expenses :900.0

900 2000

CF 0.0


Solution

  • You just make two new objects without setting its value, sure it returns 0.

    Income rI= new Income();
    
    Expenses e = new Expenses();
    

    To make it workable, here are hints:

    //in your main
    CF cF= new CF(rI, e);
    
    //in your cf
    private double cF;
    
    private Income income;
    
    private Expenses expense;
    
    public CF(Income rI, Expense e){
        this.income = rI;
        this.expense = e;
    }