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.

Results 1 to 8 of 8

Thread: Whats wrong with this for loop?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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.


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with this for loop?

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

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Whats wrong with this for loop?

    That's why self-education is better

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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().

Similar Threads

  1. Whats wrong here??
    By cbplayer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2013, 02:19 PM
  2. Whats going wrong here?
    By choll4c in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2012, 03:39 PM
  3. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  4. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  5. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM