felipe342
felipe342

Reputation: 29

Dividing using class methods always zero

The result of one of my class methods is always coming up 0. I know that can happen because of integer division, and I've tried casting whatever numbers needed to double ;yet it stays 0. The problem is with the variance method at the bottom. It's strange to me because the Average method seems to divide just fine.

public class MyArray {

    private int[] array;
    private int count = 0;
    public int length;
    public double sum;
    public double average;
    public double sqSum;
    public double variance;

   //Constructor to instantiate a new array        
   public MyArray(int length) {
       array = new int[length];
   }

   public int length(){
       return array.length;
   }


   //getter for count
   public int getCount(){
       return count;
   }

   //setter for count
   public void setCount(int Count){
       this.count = Count;
   }

   //getter for integers
   public  int[] getInts(){
       return array;
   }

   //setter for integers
   public void setInts(int counter, int input) {
       array[counter] = input;
   }

   public int getArray(int position) {
    return array[position];
   }

   public void setArray(int counter, int element){
       array[counter] = element;
   }
   public int intAt(int index) {
       return array[index]; 

   }
   //Method to print Arrays
   public void printArray() {   
       System.out.println("\nThe length of the array is "        
      +    array.length);

        for(int i=0; i<array.length; i++)
            System.out.println("The array is:" + array[i]);
   }

//Method to check if array is sorted
   public void isSortedArray() {   
         for(int i=0; i<array.length; i++)
             System.out.println("The array is:" + array[i]);
   }

//method to calc sum
   public double sum() {
         for(int i=0; i<array.length;i++)
             sum+= array[i];

        return (double)sum;
   }


    //method to calc Average
    public double average() {
        average =sum/array.length;
            return (double)average;
    }

    //method to calc sqSum
    public double sqSum() {
        double sqSum = 0.0;
        for(int i=0; i<array.length; i++)

           sqSum = sqSum +array[i] * array[i];

       return (double)sqSum;
    }    

     //method to calculate variance
     public double variance() {
            variance = (double)sqSum/array.length;

           return (double)variance;
     }
 }

Upvotes: 0

Views: 77

Answers (1)

Wilfredo Pomier
Wilfredo Pomier

Reputation: 1121

I review your class. Probably the problem was in the order of calling methods: calling average() before call sum() will return zero (because sum was never calculated). To avoid this, it's better avoid store the results in instance fields, instead call the original methods whenever is necessary. Ex. average = sum() / length.

To cache the results(in order to improve performance), it's a bit more complex than this.

Here is your class with a few changes:

public class MyArray {

    private int[] array;

    // Constructor to instantiate a new array
    public MyArray(int length) {
        array = new int[length];
    }

    public int length() {
        return array.length;
    }

    // getter for integers
    public int getInts(int counter) {
        return array[counter];
    }

    // setter for integers
    public void setInts(int counter, int input) {
        array[counter] = input;
    }

    // getter for entire array
    public int[] getArray() {
        return array;
    }

    // setter for entire array
    public void setArray(int[] newArray) {
        this.array = newArray;
    }

    public int intAt(int index) {
        return array[index];
    }

    // Method to print Arrays
    public void printArray() {
        System.out.println("\nThe length of the array is " + array.length);

        for (int i = 0; i < array.length; i++)
            System.out.println("array[" + i + "] = " + array[i]);
    }

    // Method to check if array is sorted
    public boolean isSortedArray() {
        boolean isSorted = true;
        for (int i = 1; i < array.length; i++)
            if (array[i] > array[i-1]) {
                isSorted = false;
                break;
            } 
        System.out.println("The array is " + (isSorted ? "sorted" : "not sorted"));
        return isSorted;
    }

    // method to calc sum
    public long sum() {
        long sum = 0L;
        for (int i = 0; i < array.length; i++)
            sum += array[i];
        return sum;
    }

    // method to calc Average
    public double average() {
        return (double) sum() / array.length;
    }

    // method to calc sqSum
    public long sqSum() {
        long sqSum = 0L;
        for (int i = 0; i < array.length; i++)
            sqSum += array[i] * array[i];
        return sqSum;
    }

    // method to calculate variance
    public double variance() {
        return (double) sqSum() / array.length;
    }
}

Disclaimer: I didn't run it, so test it first :-)

Upvotes: 1

Related Questions