• Post Reply Bookmark Topic Watch Topic
  • New Topic

do while loop in Poisson example  RSS feed

 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im working on a project to calculate the probability of people entering a shop using Poissons probability function, altho im struggling with the do while method,my factorial class file compiles successfully altho could sum1 help me with the code for the Poisson Class??
as follows:
Poisson(X) = (AX * e-A)/X!
Where:
X = the number of customers arriving
A = the user supplied average arrival rate
e = Euler�s number (2.7182818)
X! = Factorial X

my code:

class PoissonClass {

//--------------FIELDS----------------

private int arrivals;
private double average;
private long numTerms;

//------------CONSTRUCTOR-------------

/* Constructor*/

public PoissonClass(int newarrivals, double newaverage, long X){
arrivals = newarrivals;
average = newaverage;
numTerms = X;
}

//--------------METHOD----------------

/* Calculate Poisson probability */

// Call Factorial Class instance

Factorial newFactorial= new Factorial(numTerms);

// Call Factorial method

newFactorial.getProduct();

// Main method

public long getProduct(){
do {(Math.pow(newaverageX)*(Math.E))/newFactorial.getProduct()



loopCounter ++
}
}
}
-----------------------------------------------------------------

code for factorial class:

class Factorial {

// ------------------ FIELDS ----------------------

private long numberOfTerms;
private long product;

// ----------------- CONSTRUCTORS -----------------

/* Factorial constructor */

public Factorial(long numTerms) {
numberOfTerms = numTerms;
}

// ------------------ METHODS ---------------------

/* Get product */

public long getProduct() {
return(product);
}



/* Calculate factorial N */

public void factorialN() {
long loopParameter;
final long END_CONDITION = 0;

// Initial value for product

product = 1;

// Loop

for(loopParameter=numberOfTerms;loopParameter>END_CONDITION;
loopParameter--)
product = product*loopParameter;
}
}


thanks in advance
Wayne
 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Do while syntex is not correct. There are 3 loops.
for( ; ; ){}
while( ){}
and
do{}
while()

follow this link for more details

Link
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the problem? As Jimmy says, it doesn't look like your code even compiles. What compiler errors do you get (READ: copy and paste them here) and which lines do each of the errors refer to? These kinds of details will greatly increase the amount of help we can provide for you.

Keep Coding!

Layne
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i posted that wrong, in the equation, it should be that A is to the power of X and that e is to the power of minusA.
also, at the moment the only error in compiling is in the newFactorial.getProduct line altho im sure once i get past it ther'll be a few more.
after some more work on my method it now reads:

//--------------METHOD----------------

/* Calculate Poisson probability */

// Call Factorial Class instance

Factorial newFactorial= new Factorial(numTerms);

// Call Factorial method

newFactorial.getProduct();

// Main method

public long calcPoisson(){
do {
if (numTerms >= 1 && numTerms <= 10)
return (Math.pow(newaverageX)*(Math.E)/newFactorial.getProduct());
else {
System.out.println ("Error: Input not within range. Please try again");
} loopCounter ++;

}while (true);

any help with the code is greatly apreciated as its really bugging me
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the exact error message. It will save a lot of time for all of us in the long run. Copy and paste is your friend!

Also, it will help if you use UBB CODE tags. They will preserve your formatting so we can more easily read your code. There are buttons just bellow the message editing text area that will help.

Layne
[ November 30, 2004: Message edited by: Layne Lund ]
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Poisson
// Chris Lavery
// 28th November 2004

class PoissonClass {

//--------------FIELDS----------------

private int arrivals;
private int average;
private long numTerms;

//------------CONSTRUCTOR-------------

/* ConstructUBBor*/

public PoissonClass(int newarrivals, int newaverage, long X){
arrivals = newarrivals;
average = newaverage;
numTerms = X;
}

//--------------METHOD----------------

/* Calculate Poisson probability */

// Call Factorial Class instance

Factorial newFactorial= new Factorial(numTerms);

// Call Factorial method

newFactorial.getProduct();

// Main method

public long calcPoisson(){
do {
if (numTerms >= 1 && numTerms <= 10)
return (Math.pow(newaverageX)*(Math.E)/newFactorial.getProduct());
else {
System.out.println ("Error: Input not within range. Please try again");
} loopCounter ++;

}while (true);

}
}


(Math.pow(newaverageX) - error under the n in new average saying that it cannot resolve symbol

(Math.E)/new - error under the backslash saying possible loss of precision: found - double
required - long

} loopCounter++; - error under l in loopCounter saying cannot resolve symbol
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!