Home > Software engineering >  Add setters and getters to an array , create a for-loop method and assign values
Add setters and getters to an array , create a for-loop method and assign values

Time:02-23

I'm new to Java. I created an array in a class and created a setter and getter to it.

    public class Calculation{
        private int[] data;
        private int size;
    
    
    public int[] getData() {
        return data;
    }
    
    public void setData(int[] data) {
        this.data = data;
    }

    public int getSize() {
        return size;
    }
    
    public void setSize(int size) {
        this.size = size;
    }
}

And I created a loop method using array. But its output is 1. Method,

public int ProductOfArray(int data[], int size){
    int product = 1;
    for (int i =1; i <= getSize(); i  ) {
        product = product * this.data[i];
    }
    return product;
}

Main,

System.out.println("Enter size: ");
int size = num.nextInt();
                
cal1.setSize(size);
System.out.println("Enter elements of array: ");
[] myArray = new int[size];
                
for(int i=0; i<size; i  ) {
                     
    myArray[i] = num.nextInt();
}
                
System.out.println(cal2.ProductOfArray(myArray, size));

When I run this program it shows the last output as 1. It does not calculate user inputs.

CodePudding user response:

As Thomas mentions, you need to set the data in your Calculation instance (I do it for cal1 in this case) to be the data that you read in from the for loop. Here is one way you could do it:

import java.util.Scanner;

public class TestCalc {

    public static void main(String[] args) {
    Scanner num = new Scanner(System.in);
    System.out.println("Enter size: ");
    int size = num.nextInt();
    Calculation cal1 = new Calculation();
                    
    cal1.setSize(size);
    System.out.println("Enter elements of array: ");
    int [] myArray = new int[size];
            
    for(int i=0; i<size; i  ) {
                         
        myArray[i] = num.nextInt();
    }
    
    cal1.setData(myArray); //Here is where you set the int array field of the object.
                    
    System.out.println(cal1.ProductOfArray(myArray, size));

    }

}

class Calculation {
    private int[] data;
    private int size;

    public int[] getData() {
    return data;
    }

    public void setData(int[] data) {
    this.data = data;
    }

    public int getSize() {
    return size;
    }

    public void setSize(int size) {
    this.size = size;
    }

    public int ProductOfArray(int data[], int size) {
    int product = 1;
    for (int i = 1; i < getSize(); i  ) {
        product = product * this.data[i];
    }
    return product;
    }
}

Note: I have put the class in the same file as the main method. You may have a separate file that you can copy and paste this class content into for it to work. I have also changed the bounds of your for loop (from <=getSize() to <getSize()) so you do not get an out of bounds error.

Output:

Enter size: 
5
Enter elements of array: 
1
2
3
4
5
120
  • Related