Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Whats wrong with this for loop?

1. ## Whats wrong with this for loop?

I have this code here:
``` for(C0 = 280.0; C0 <= 400.0; C0 = C0 += 5)
{
Carbondep = A + B*Math.log(C0/PRE_CARBON);

//formula to calculate emissivity
earthEmissivityep = 1.0 - (H2O_EMISSIVITY + Carbondep/2);

//write calculation to find surface temperature
eSurfacetemp =
Math.pow((SOLAR_CONSTANT/4)*(1.0 - EARTH_ALBEDO)
/(STEFAN_B_CON*Carbondep*RADIUS*RADIUS), 0.25);

//convert answer from kelvin to celcius
surfaceTempCel = eSurfacetemp - KELVIN_TEMP;

//enable answer to be truncated to 2 decimal places
DecimalFormat df = new DecimalFormat("####0.00");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.print("For a carbon level of " + C0 +
" the surface temperature is: "
+ df.format(surfaceTempCel)
+ "C");
System.out.print("\n");
}```

It compiles fine, but when I run it, all the printed out answers are the same. I've tried everything to get the code to print out properly, but all it does is repeat the same answer over again. What is wrong with this. Im guessing its a simple fix, but I cant even find out what is wrong.  Reply With Quote

3. ## Re: Whats wrong with this for loop?

One guy posted similar question with similar assignment yesterday.

Now remember: int/int=int, not double. For example C0/PRE_CARBON is always an integer, not double.  Reply With Quote

4. ## Re: Whats wrong with this for loop?

I figured it out now. I guess our lecturers dont make themselves very clear :/  Reply With Quote

5. ## Re: Whats wrong with this for loop?

That's why self-education is better   Reply With Quote

6. ## Re: Whats wrong with this for loop?

Okay now I have this code:
```public class climate
{
public static void main(String[] args)
{
//constants for calculating emissivity
final double SOLAR_CONSTANT = 1367;
final double ALBEDO = 0.3;
final double STEFAN_B_CONSTANT = 5.67E-8;
final double RADIUS = 1.0;
final double KELVIN_TEMP = 273.15;
final double H2O_EMISSIVITY = 0.65;
final double A = 0.1;
final double B = 0.06;
final double PRE_CARBON = 280;

double carbondiep = 0.0;
double emissivity;
double earthsurfaceTemp;
double surfaceTempCel;

for(double C0 = 280.0; C0 <= 400.0; C0 = C0 += 5)
{
carbondiep = A + B * Math.log(C0/PRE_CARBON);

//Calculate emissivity
emissivity = 1.0 - (H2O_EMISSIVITY + carbondiep)/2;

//Surface temperature formula
earthsurfaceTemp =
Math.pow((SOLAR_CONSTANT/4)*(1.0 - ALBEDO)/(STEFAN_B_CONSTANT*emissivity*RADIUS*RADIUS), 0.25) - KELVIN_TEMP;

//enable answer to be truncated to 2 decimal places
DecimalFormat df = new DecimalFormat("####0.00");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.print("For a carbon level of " + C0 +
" the surface temperature is: " + df.format(earthsurfaceTemp) + "C");
System.out.print("\n");
}
}
}```

I need to put a helper method in here... basically I want to, to learn more skills. But I dont know where to put one. Please only answers, im due to hand this in a few hours. Then explain.  Reply With Quote

7. ## Re: Whats wrong with this for loop?

You can, for instance, make a method with the whole formula. So, that your loop would look like as follows:
```for(double c = 280; c <= 400; c += 5) {
String earthsurfTemp = computeWithAhelper(c);
System.out.println("C = " + c + "; temp = " + earthsurfTemp);
}```
Here, computeWithAhelper() is a helper method to simplify the code.  Reply With Quote

8. ## Re: Whats wrong with this for loop?

Sorry, I still dont understand. how does it simplify the code? and how is any calculations associated with the helper?

--- Update ---

Please help someone, im sick and I have to had in this crap just someone help insert at helper method in my code please  Reply With Quote

9. ## Re: Whats wrong with this for loop?

Helper methods are used in order to encapsulate different parts of computation so that your code becomes more readable. In my example, you encapsulate the entire computation within computeWithAhelper() method. The advantage is that you can completely forget about computation details during writing your output loop. You can just concentrate on your loop details and have a guarantee, that the helper method would compute whatever you want correctly. And when the loop is done, you can, in turn, totally forget about it and concentrate on the computation details.

So that your main method would look like follows:

```public static void main(String[] args)
{
for(double c = 280; c <= 400; c += 5) {
String earthsurfTemp = computeWithAhelper(c);
System.out.println("C = " + c + "; temp = " + earthsurfTemp);
}
}```

All the computations, formulas, constants etc. are in the computeWithAhelper().  Reply With Quote