| | |
Homework HELP!!!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2006
Posts: 2
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
int total = 0; for (int i:elements) { total += i; }
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Feb 2006
Posts: 2
Reputation:
Solved Threads: 0
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]);
}
}
}
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]);
}
}
}
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- Dynamic memory allocation homework (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Java open internet browser
- Next Thread: Weblogic licence exception
Views: 1771 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree webservices windows






