Having trouble with this one:

The formula y = nekt can be used for estimating growth where

n is the initial amount
k is a constant
t is the time
y is the final amount

This formula will be used for estimating the bacteria growth in a lab experiment. The output should look similar to:

Enter intial bacteria amount:
5.0
Enter a constant value for k:
0.8

Time(hours) Bacteria

1 11.13
2 24.77
3 55.12
4 122.66
5 272.99
6 607.55
7 1352.13
8 3009.23


In the class Mymath , write two different methods:

a. Prompts the user initial bacteria and the constant k in the method.

b. Get the user initial bacteria and the constant k from the main method.

Write a main method to test the two methods above.

My coding:

import java.util.Scanner;
public class bacteriagrowth 
{
	public static void main(String []args)
	{
		Scanner input = new Scanner (System.in);
		double n;
		System.out.println("Enter initial bacteria amount: ");
		n = input.nextDouble();
		double k;
		System.out.println("Enter a constant value for k: ");
		k = input.nextDouble();
		double t = 1;
		double y = 0;
		while (t<=8)
		{
		y = Math.pow(n*Math.E, (k*t));
		t = t +1;
		}
		System.out.printf("time" + t, y);
		
	}

}

Okay? And your question is?

i dont know what im doing wrong. You think you can help me?

i dont know what im doing wrong. You think you can help me?

Are you getting an error message? All you've said is the question, and your code. Is there a problem, if so, what is it? :)

im not getting the output my teacher wants.

I think the problem here is that you are calculating the formula in the wrong way your calculation is:

y = Math.pow(n*Math.E, (k*t));

what this does is multiply E by n, then raise this product by (k*t). Instead what you want to do to get the correct answer is raise E by the product (k*t) then multiply this answer by n to get the correct number. Be careful of the order in which you apply calculations.
Since this is homework I won't give you the correct code but you should be able to do it by yourself anyway. Let us know if this works.

thanks alot! i got it now.

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.