I am writing a little program to create an array and then to sum the integers in the array. I am also suffering a stomach flu, and I don't have the brainpower to figure out how to call the two methods I wrote to create the array and then sum the array. I would really appreciate some help -- even just a few pointed hints.

LGK

class SumIntegers { //define class
   // private variables to be used by this class
      private int n;    // number of integers in the array
      private int array[];  // the array to hold the integers to be summed
      private int x; // the lower bound of the integer array
      private int y; // the upper bound of the integer array
      int sum; // sum of the array's integers
   // constructor
   public SumIntegers(){
      x = 12;
      y = 23;
      n = y - x + 1;
   }
   //main method
   public void main(String[] args){
       System.out.println("This program sums a range of integers.");
   //todo call createArray method

   //todo call sumArray method
       System.out.println("The sum of the elements in the array is: " + sum);
   }

    //method to create the array
   public void createArray(){
     int i;
     array[0] = x;
       for (i = 1; i < n; i++){
       array[i] = array[i-1] + 1;
       }
    //print elements of the array
     for (int j = 0; j < array.length; j++) {
       System.out.println("The array includes these integers: " + array[i]);
     } //end method to populate array
    }

   //sum the elements of the array
   public int sumArray(){
    int k;
    int sum = 0;
     for(k = 0; k < n; k++){
      sum = sum + array[k];
     }
    return sum;
   }
  }

I am writing a little program to create an array and then to sum the integers in the array. I am also suffering a stomach flu, and I don't have the brainpower to figure out how to call the two methods I wrote to create the array and then sum the array. I would really appreciate some help -- even just a few pointed hints.

LGK

class SumIntegers { //define class
   // private variables to be used by this class
      private int n;    // number of integers in the array
      private int array[];  // the array to hold the integers to be summed
      private int x; // the lower bound of the integer array
      private int y; // the upper bound of the integer array
      int sum; // sum of the array's integers
   // constructor
   public SumIntegers(){
      x = 12;
      y = 23;
      n = y - x + 1;
   }
   //main method
   public void main(String[] args){
       System.out.println("This program sums a range of integers.");
   //todo call createArray method

   //todo call sumArray method
       System.out.println("The sum of the elements in the array is: " + sum);
   }

    //method to create the array
   public void createArray(){
     int i;
     array[0] = x;
       for (i = 1; i < n; i++){
       array[i] = array[i-1] + 1;
       }
    //print elements of the array
     for (int j = 0; j < array.length; j++) {
       System.out.println("The array includes these integers: " + array[i]);
     } //end method to populate array
    }

   //sum the elements of the array
   public int sumArray(){
    int k;
    int sum = 0;
     for(k = 0; k < n; k++){
      sum = sum + array[k];
     }
    return sum;
   }
  } 

end quote.

 //main method
   public void main(String[] args){
       System.out.println("This program sums a range of integers.");
   //todo call createArray method
                   int sum = 0;
       SumIntegers obj = new SumIntegers();//Create an object and Initialize the variables
                   obj.createArray();//Now the Array is created
                   sum = obj.sumArray();//Sum all the Values of the array and assign to variable
   //todo call sumArray method
       System.out.println("The sum of the elements in the array is: " + sum);
   }

there are some errors in your code
I've changed a bit in it, so it'll run

public class SumIntegers {

//	 private variables to be used by this class
	private int n; // number of integers in the array
	private int array[]; // the array to hold the integers to be summed
	private int x; // the lower bound of the integer array
	private int y; // the upper bound of the integer array
	int sum; // sum of the array's integers
//	 constructor
	public SumIntegers(){
	x = 12;
	y = 23;
	n = y - x + 1;
	}

//	method to create the array
	public void createArray(){
	int i;
	
	// without this step, you will not have (completely) declared your array
	// the app needs to know the size your array needs to have
	int lengthArr = (y - x) + 3;
	array = new int[lengthArr];
	
	array[0] = x;
	for (i = 1; i <= n; i++){
		array[i] = x + i;
		// array[i] = array[i-1] + 1;
	}
//	print elements of the array
//	for (int j = 0; j < array.length; j++) {
//	System.out.println("The array includes these integers: " + array[i]);
//	} //end method to populate array
	}

//	sum the elements of the array
	public int sumArray(){
	int k;
	int sum = 0;
	for(k = 0; k < n; k++){
		System.out.println("The array includes these integers: " + array[k]);
	sum = sum + array[k];
	}
	return sum;
	}

	
	public static void main(String[] args){
		System.out.println("This program sums a range of integers.");
//		todo call createArray method
		int sum = 0;
		SumIntegers obj = new SumIntegers();//Create an object and Initialize the variables
		System.out.println("bleeee");
		obj.createArray();//Now the Array is created
		System.out.println("bleeee");
		sum = obj.sumArray();//Sum all the Values of the array and assign to variable
//		todo call sumArray method
		System.out.println("The sum of the elements in the array is: " + sum);
		} 
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.