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

Recommended Answers

All 4 Replies

int total = 0;
for (int i:elements) {
  total += i;
}

Thank you so much. I didn't think it would be much but I was just confused on how to do this. Thanks again!!!

here is a lewis loftus progamming project soluiton from chapter 6 i noticed the post had hw help and hope maybe this could help a student in need...email me for any other solutions at [email snipped]

  int[]numbers;
  int temp;
  int amount;

  System.out.println ("Enter 1-50.");
  amount = Keyboard.readInt();
  numbers = new int [amount];


  for(int i=0; i<number.length; i++)
  {
   System.out.println ("enter number"+(i+1));
   numbers[i] = Keyboard.readInt();
  }
  for (inti=0; i<number.length/2; i++)
  {
   temp = number[i];
    number[i]number[number.length-i-1];
    number[number.length -i-1] = temp;
}


for(int i = 0; i<number.length; i++)
{
 System.out.println(i+1+":"+number[i]);
}
}
}

:D

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.