-3

So, this is my class which contains my constructor and accessor methods:

public class sac
{
// Initializing instance variables.
private double bal;
private double[] dep;
private double[] wit;

/**
 * Constructor for objects of class sac
 */
public sac()
{
    //Declaring instance variables
    bal = 500;

    //Deposits:
    double[] dep = new double[5];
    dep[0] = 100.00;
    dep[1] = 124.00;
    dep[2] = 78.92;
    dep[3] = 37.55;
    dep[4] = 83.47;

    //Withdrawals:
    double[] wit = new double[7];
    wit[0] = 29.88;
    wit[1] = 110.00;
    wit[2] = 27.52;
    wit[3] = 50.00;
    wit[4] = 12.90;
    wit[5] = 15.20;
    wit[6] = 11.09;
}

/**
 * Returns and sets the value of balance.
 *
 * 
 * @return bal
 */
public double setBal(double b)
{
    //Declaring instance variables
    b = bal;
    return bal;
}

/**
 * Returns and sets the values of deposits.
 *
 * 
 * @return deposits
 */
public double[] getDep()
{
    double[] d = new double[5];
    //Deposits:
    d[0] = dep[0];
    d[1] = dep[1];
    d[2] = dep[2];
    d[3] = dep[3];
    d[4] = dep[4];
    return d;
}

/**
 * Returns and sets the values of withdrawals.
 *
 * 
 * @return withdrawals
 */
public double[] getWit()
{
    double[] w = new double[7];
    //Withdrawals:
    w[0] = wit[0];
    w[1] = wit[1];
    w[2] = wit[2];
    w[3] = wit[3];
    w[4] = wit[4];
    w[5] = wit[5];
    w[6] = wit[6];
    return w;
    }

}

The code may be sloppy as I have been messing around trying to find out the solution to this error, however this is my driver which I am trying to run. The error occurs while trying to call my values from my accessor methods in the other class.

import java.util.*;
/**
  */
public class sacDemo
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in); //Creates new scanner object.
    sac sac = new sac(); //Creates accessor object to retrieve variables       from sac class.

    int bal;
    double air;
    double[] dep = new double[5];;
    double[] wit = new double[7];

    double[] d = dep;
    double[] w = wit;

    w = sac.getWit();
    d = sac.getDep();

    System.out.println("What is your annual interest rate?");
    air = input.nextInt();
    air = air/10;

    System.out.println("Your deposits for the month were: ");
    for(int i=0;i<5;i++){

        System.out.println(dep[i]);
    }
    System.out.println("Your withdrawals for the month were: ");
    for(int i=0;i<6;i++){

        System.out.println(wit[i]);
    }

}
}
Lokesh Pandey
  • 1,739
  • 23
  • 50

2 Answers2

0

your setBal function is wrong mate.

public double setBal(double b)
{
    // Declaring instance variables
    // b = bal; <--- wrong
    bal = b; // <--- right
    return bal;
}
LonelyCpp
  • 2,533
  • 1
  • 17
  • 36
0

In your sacDemo class you are initializing new array objects and assign them to variables d and w.

double[] dep = new double[5];;// {0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
double[] wit = new double[7];// {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
double[] d = dep; // {0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
double[] w = wit; // {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }

So after this line 'd' and 'w' will have elements with 0.0 value initialized to each. In java , = (assignment ) operator works from right to left. What you have in 'dep' will be assigned to 'd', so iterating d and w will result in elements which has 0.0 value.

w = sac.getWit();
d = sac.getDep();

After this, you can access the values using 'w' and 'd' not 'dep' and 'wit' because they are still referring to previou arrays.

So when you iterate, use below code fragments instead,

   for(int i=0;i<5;i++){

        System.out.println(d[i]);
    }
   for(int i=0;i<6;i++){

        System.out.println(w[i]);
    }

I hope this will help you!

Hisat
  • 43
  • 5