944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1853
  • Java RSS
Jan 28th, 2006
0

Homework HELP!!!

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
startingover is offline Offline
2 posts
since Jan 2006
Jan 28th, 2006
0

Re: Homework HELP!!!

Java Syntax (Toggle Plain Text)
  1. int total = 0;
  2. for (int i:elements) {
  3. total += i;
  4. }
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 28th, 2006
0

Re: Homework HELP!!!

Thank you so much. I didn't think it would be much but I was just confused on how to do this. Thanks again!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
startingover is offline Offline
2 posts
since Jan 2006
Feb 10th, 2006
0

Re: Homework HELP!!!

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 bobgan1234@yahoo.com

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]);
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java4u is offline Offline
2 posts
since Feb 2006
Feb 10th, 2006
0

Re: Homework HELP!!!

Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java4u is offline Offline
2 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java open internet browser
Next Thread in Java Forum Timeline: Weblogic licence exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC