Here is my problem:
Create Two classes
ProcessInt Class knows
An array of integers
ProcessInt class knows how to
One constructor that will set the size of the array of integers
Method to set the value of a particular array element
Method to get the value of a particular array element
Calculate and return the sum of all array elements
ProcessIntApp class
ProcessIntApp class does the following in its main method
Get from a user the number of integers the user wants to enter and uses the user’s answer to create an object of the ProcessInt class
Do not allow user to provide 0 or negative input
Gets from user an integer and stores that integer in one of the ProcessInt integer array elements
Repeat above step until the user has entered the number of integers the user said they would and all array elements are filled
Display the sum of the integers stored in ProcessInt integer array instance variable
Here is what I have. I am having trouble with the last part. I can not seem to get my array to sum the total. Please look over and make suggestions. If there are other problems, please point out. It would be appreciated. Thanks, Lea
public class ProcessInt {
//declare instance variables
public int [] process;
public double sum;
//constructor
public ProcessInt(int sizeOfArray)
{
process = new int [sizeOfArray];
}
// end of constructor
public void setProcessElement (int element, int elementValue)
{
if ( (element < process.length) && (element >= 0))
process[element] = elementValue;
}//end if
public int getProcessElement (int element)
{
if ((element < process.length) && (element >= 0))
return process[element];
else
{
return -9999999;
}
}//end if
public double getSum()
{
for (int i = 0; i < process.length; i++)
{
sum += process;
} // end for loop
return sum;
}//end getSum
public String toString()
{
String tempString;
tempString = "Process Element\t Process Value\n";
for(int index = 0; index < process.length; index++)
{
tempString = tempString + "\t" + index + "\t\t" + process[index] + "\n";
}//end for loop
tempString = tempString + "\nThe sum of the Process is: " + getSum();
return tempString;
}//end String
}//end class
import java.util.Scanner;
public class ProcessIntApp {
public static void main(String[] args) {
String userAnswer;
int numberOfElements;
int process;
Scanner in = new Scanner(System.in);
System.out.println("This program will allow you to enter one or more numbers and display the sum of the numbers."
);
System.out.print("Enter Y to begin or Q to quit: ");
userAnswer = in.next();
while (userAnswer.equalsIgnoreCase("y"))
{
do
{
System.out.println("How many numbers would you like to process?");
System.out.print("Enter a positive number: ");
numberOfElements = in.nextInt();
System.out.println("\n");
}
while (numberOfElements <= 0);
ProcessInt pi = new ProcessInt(numberOfElements);
for (int i = 0; i < numberOfElements; i++)
{
System.out.print("Enter number: ");
process = in.nextInt();
pi.getSum();
} //end for loop
System.out.println("The sum of the numbers is: " + numberOfElements);
userAnswer = in.next();
}
}
}//end class