URGENT! Java code seems to reset value

Discussion in 'Mac Programming' started by sammich, May 31, 2007.

  1. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #1
    Okay, it may be a very simple error, but this one's been driving me nuts for the last hour or so. This is for an assisngment that's due tonight (in 4hours). So if any one can check this out, it would awesome.

    It's a little long cos i'm not sure where the problem is, the value 'startingWeight' that is passed into the methods is being passed into them as the initial value, not the value as the result of all the previous code, as is evident in say: "startingWeight -= 554*9.81;" It will subtract the value, but the value passed back into the method "calculateFuelUsage()" is the original 151315*9.81.

    Thanks in advance!

    startingWeight is basically a variable that stores the current weight of the aircraft.

    Code:
    double timeStep = 0.01; //seconds
    double counter = 0;
    double totalTime = 0;
    double distanceStep = 0;
    double distanceCovered = 0;
    double desiredDistance; //metres; 18042.9m to first turn, to first buoy 610165.433m, to next two buoys 400km,
    double startingWeight = (151315-291.76)*9.81; //after first climb to 1500m 291.76, 
    double cruiseVelocity ; //SL v = 155, 1500m = 170, 8400m v = 214, intermediate = 170, TURNS = 132.2 @ SL, 140.47 @ 1500m
    double fuelWeightUsed = 0;
    double curFuelWeightUsed = 0;
    	
    double curThrust;
    double cruiseHeight; //dropping buoys 33m, intermediate = 1524m, cruise = 8400m, final turn in to approach is 272.72m
    
    public fuelUsage()
    {
    	calcTotalFuelUsage();
    }
    
    private void calcTotalFuelUsage()
    {
    	//calculateFuelUsage(h, dD, startingWeight, cV, n, "");	   	
    	//calculateFuelUsage(8400, 10000000, startingWeight, 214   , 1  , "Cruise to First Turn");
    	calculateFuelUsage(1524, 18042.9, startingWeight, 170   , 1  , "Cruise to First Turn");
    	calculateFuelUsage(1524, 2159.52, startingWeight, 140.47, 1.3, "Turn 1");
    	startingWeight -= 554*9.81;
    	calculateFuelUsage(8400, 610224.012, startingWeight, 214, 1, "Departing Cruise");  //Cruise		
    	calculateFuelUsageDescent(4216.5, (8400-33)*11, startingWeight, 184.5, 1, "First Descent");  //Cruise
    	startingWeight -= 200*9.81; //Drop Buoy 1
    	calculateFuelUsage(33, 141421.3, startingWeight, 155, 1, "Buoy 1-2 Cruise");	//Cruise 200km @ Sea Level
    	startingWeight -= 200*9.81; //Drop Buoy 2
    	calculateFuelUsage(33, 141421.3, startingWeight, 155, 1, "Buoy 2-3 Cruise");	//Cruise 200km @ Sea Level
    	startingWeight -= 200*9.81; //Drop Buoy 3
    	calculateFuelUsage(33, 5944.1588, startingWeight, 1132.2, 1.3, "Turn 2"); //Turn 2
    	calculateFuelUsage(33, 196402.241, startingWeight, 155, 1, "Buoy 3-4 Cruise");	//Cruise km @ Sea Level
    	calculateFuelUsage(33, 5944.1588, startingWeight, 1132.2, 1.3, "Turn 3"); //Turn 3
    	startingWeight -= 200*9.81; //Drop Buoy 4
    	calculateFuelUsage(33, 400000, startingWeight, 155, 1, "Buoy 4-5 Cruise");	//Cruise 400km @ Sea Level
    	startingWeight -= 200*9.81; //Drop Buoy 5
    	calculateFuelUsage(33, 1763.682, startingWeight, 155, 1.3, "Turn 4");	//Turn 4
    	startingWeight -= (544.006+291.76)*9.81;  //climb to 8400m
    	calculateFuelUsage(8400, 977869.77, startingWeight, 214, 1, "Final Cruise");	//Final Cruise
    	calculateFuelUsageDescent(4275, (8400-150)*11, startingWeight, 160, 1, "Final Descent");  //Final Descent
    	calculateFuelUsage(8400, 2492.386, startingWeight, 132.2, 1.3, "Turn 5");	//turn 5
    	calculateFuelUsageDescent(80, 1760, startingWeight, 96.51, 1, "Approach Descent");  //Final Descent
    }
    
    private void calculateFuelUsage(double cruiseHeight, double desiredDistance, double startingWeight, double cruiseVelocity, double n_1, String title)
    {
    	double fuelWeightUsed = 0;
    	double distanceCovered = 0;
    	double totalTime = 0;
    	
    	while (distanceCovered < desiredDistance)
    	{
    		totalTime += timeStep;
    		distanceStep = cruiseVelocity*timeStep;
    		curThrust = calcCoeffDrag(cruiseHeight, cruiseVelocity, (startingWeight-fuelWeightUsed), n_1)*0.5*rho(cruiseHeight)*Math.pow(cruiseVelocity, 2)*wingArea;
    		curFuelWeightUsed = ((tsfc*curThrust)/cruiseVelocity)*(distanceStep/g); //this gives the fuel used per timeStep in kilograms
    		fuelWeightUsed += (curFuelWeightUsed*9.81/1.4);
    		distanceCovered += distanceStep;			
    	}
    	startingWeight -= fuelWeightUsed;
    	System.out.println("\n---"+title+"----");
    	System.out.println("Total Fuel Used: "+fuelWeightUsed/9.81+" kg");
    	System.out.println("End Weight: "+startingWeight/9.81+" kg");
    	System.out.println("Distance Covered: "+distanceCovered+" m");
    	System.out.println("Total time taken: "+totalTime+" s");	
    }
    
    The Output. You'll see here that the "End Weight" should go down as the flight progresses, but it simply subtracts the "Total Fuel Used" from the original value of startingWeight.

    Code:
    ---Cruise to First Turn----
    Total Fuel Used: 145.7385063647447 kg
    End Weight: 150877.50149363524 kg
    Distance Covered: 18043.800000003597 m
    Total time taken: 106.1400000000174 s
    
    ---Turn 1----
    Total Fuel Used: 22.769672350170147 kg
    End Weight: 151000.47032764982 kg
    Distance Covered: 2160.428600000054 m
    Total time taken: 15.379999999999717 s
    
    ---Departing Cruise----
    Total Fuel Used: 3965.0585781985224 kg
    End Weight: 146504.18142180145 kg
    Distance Covered: 610225.2800030949 m
    Total time taken: 2851.5200000158216 s
    
    ---First Descent----
    Total Fuel Used: 33.612201426565 kg
    Distance Covered: 92037.8250000452 m
    Total time taken: 498.8499999996913 s
    
    ---Buoy 1-2 Cruise----
    Total Fuel Used: 1190.1855930666181 kg
    End Weight: 149079.05440693337 kg
    Distance Covered: 141422.00000009598 m
    Total time taken: 912.3999999993152 s
    
    ---Buoy 2-3 Cruise----
    Total Fuel Used: 1189.3788325053533 kg
    End Weight: 148879.86116749464 kg
    Distance Covered: 141422.00000009598 m
    Total time taken: 912.3999999993152 s
    
    ---Turn 2----
    Total Fuel Used: 482.30331477859977 kg
    End Weight: 149386.9366852214 kg
    Distance Covered: 5955.37200000003 m
    Total time taken: 5.259999999999932 s
    
    ---Buoy 3-4 Cruise----
    Total Fuel Used: 1649.3731141955334 kg
    End Weight: 148219.86688580445 kg
    Distance Covered: 196403.59999968304 m
    Total time taken: 1267.1199999989926 s
    
    ---Turn 3----
    Total Fuel Used: 482.30331477859977 kg
    End Weight: 149386.9366852214 kg
    Distance Covered: 5955.37200000003 m
    Total time taken: 5.259999999999932 s
    
    ---Buoy 4-5 Cruise----
    Total Fuel Used: 3347.2495083701515 kg
    End Weight: 146321.99049162984 kg
    Distance Covered: 400000.7499981539 m
    Total time taken: 2580.650000009909 s
    
    ---Turn 4----
    Total Fuel Used: 17.431497595335166 kg
    End Weight: 149451.80850240466 kg
    Distance Covered: 1763.899999999966 m
    Total time taken: 11.379999999999802 s
    
    ---Final Cruise----
    Total Fuel Used: 6277.745320605392 kg
    End Weight: 142355.72867939458 kg
    Distance Covered: 977870.8600054949 m
    Total time taken: 4569.490000053321 s
    
    ---Final Descent----
    Total Fuel Used: 33.03168774364626 kg
    Distance Covered: 90750.40000004965 m
    Total time taken: 567.1899999996292 s
    
    ---Turn 5----
    Total Fuel Used: 40.345675293154684 kg
    End Weight: 148593.1283247068 kg
    Distance Covered: 2493.2919999999554 m
    Total time taken: 18.86000000000015 s
    
    ---Approach Descent----
    Total Fuel Used: 1.08927779957211 kg
    Distance Covered: 1760.342399999925 m
    Total time taken: 18.24000000000005 s
    
     
  2. janey macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #2
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    I'm not sure it's dpoing what you think it is.

    For example in turn 3:-

    total fuel used + end weight = 149,868

    which isn't the starting weight of 151,024.

    Perhaps I don't understand the problem you are having but it looks like it might be working to me.

    b e n
     
  4. janey macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #4
    if you notice the output, it should be decreasing the end weight as the flight progresses, but it's doing no such thing, so it's not working..

    the first couple numbers check out though..
     
  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    What I meant was it's not simply subtracting the weight of the total fuel used from the initial starting weight and ignoring all the subtractions of bouys etc, which is what the OP suspects

    For turn 3 if you add the following:-

    Total fuel used: 482
    End weight: 149386.9
    3 Buoys at 200 each: 600
    something else: 554

    You get about 151023, ie the starting weight.


    So the passing by value is working. The problem lies elsewhere.

    b e n

    EDIT: I think the end weight is sometimes going up because the fuel used at each stage isn't being subtracted from the global starting weight, and some parts of the flight use less fuel then others.
     
  6. janey macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #6
    That's his problem, that's what I'm trying to say.

    edit: basically, startingWeight starts out at whatever value it was assigned, then with calculateFuelUsage he subtracts the fuel used from the startingWeight, but the amount of fuel used is being subtracted from startingWeight minus the weight of the buoys as applicable but NOT any previous fuel usage. What the OP is trying to do is subtract the fuel used every time for an end weight that takes into account the entire fuel usage and the weight of the buoys, which is what it's not doing.
     
  7. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #7
    You're overloading variables. For example, "startingWeight" is not only a class variable (I think - you don't post your entire class but it appears to be a class variable) but also the name of a parameter passed into the functions.

    Please differentiate class variables from parameters (typical naming conventions include things like adding a "_" to the front or end of a class variable so you can see what is local and what isn't, so "startingWeight_", and something like a "p" in front of parameters, so "pStartingWeight"). It doesn't much matter which convention you choose - just choose one.

    Barring that, please at least use this.startingWeight when setting the value.

    But... you'll be much, much better off learning to use naming conventions to ensure that, just by looking at the code, you can tell what's a class variable, what's a parameter that was passed in, and what's a local variable. It will make so many things so much easier to debug. In this case, it would have shown you the problem immediately.


    Edit: or, at least, I think that's the problem... the posted code is incomplete, so I'm not positive which variables are in scope where.
     
  8. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #8
    You are passing the bloody variable by value. All Java primitive types are passed by value on the stack. Didn't your programming teacher mention that?

    To fix your code in a really crap kludgy way, remove from your method calls the double representing weight and just use the variable defined in the class used at instance level. If you don't understand that, you are in serious trouble.
     
  9. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #9
    I don't think that's kludgy at all - it's part of the point of having class variables. It's just that it shouldn't have been passed in in the first place.
     
  10. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #10
    …or return the fuel used in calculateFuelUsage() which is what it's name implies it does, ie

    Code:
    private double calculateFuelUsage(double cruiseHeight, double desiredDistance, double startingWeight, double cruiseVelocity, double n_1, String title)
    {
       …
       return fuelWeightUsed ;
    }
    
    and then use the returned value to update the weight of the aircraft after each stage. Also how about renaming the parameter 'startingWeight' to aircraftWeight or something like that?

    b e n
     
  11. sammich thread starter macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #11
    thanks

    uncanny! that is EXACTLY my workaround. I figured that one out about two hours after I posted.

    jsw, I really should've noticed that one, it seems logical for me to pass the same parameter back and forwards, hence why I didn't pick it up.

    Thanks for all the help people!
    Already emailed it to my lectuerer.

    Night
     

Share This Page