Homework HELP!!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2006
Posts: 2
Reputation: startingover is an unknown quantity at this point 
Solved Threads: 0
startingover startingover is offline Offline
Newbie Poster

Homework HELP!!!

 
0
  #1
Jan 28th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Homework HELP!!!

 
0
  #2
Jan 28th, 2006
  1. int total = 0;
  2. for (int i:elements) {
  3. total += i;
  4. }
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 2
Reputation: startingover is an unknown quantity at this point 
Solved Threads: 0
startingover startingover is offline Offline
Newbie Poster

Re: Homework HELP!!!

 
0
  #3
Jan 28th, 2006
Thank you so much. I didn't think it would be much but I was just confused on how to do this. Thanks again!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2
Reputation: Java4u is an unknown quantity at this point 
Solved Threads: 0
Java4u Java4u is offline Offline
Newbie Poster

Re: Homework HELP!!!

 
0
  #4
Feb 10th, 2006
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]);
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2
Reputation: Java4u is an unknown quantity at this point 
Solved Threads: 0
Java4u Java4u is offline Offline
Newbie Poster

Re: Homework HELP!!!

 
0
  #5
Feb 10th, 2006
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1771 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC