2

Alright I am still pretty new to this. So the idea is I would like to take an array from one class, send it through some equations and then output an updated version. I also need it to iterate a few times. My best guess at this was to setup a while loop in my main class with the class method inside of it that corresponds to the array updater. Its just spitting out the same values (not updated) I would really appreciate any suggestions or tips on what I might be doing wrong.

**If you are going to tell me to read a book or take a class, please move on. I'm learning on my own and this site is extremely helpful only when it is used correctly. Thanks

The code is really long so I will try to post what snippets I feel will be most helpful.

** This portion contains the original arrays (rad[ ] , vol[ ]...) I would like to update

public class InitialGrids {


PlanMat pm = new PlanMat();

int iic,ioc,iman ;
int nic, noc;
int iccomp, occomp, mcomp;
double masIC, masOC, masMAN;
double volp;
double radIC, radOC, radp;


public int iMat[ ];
public double rad[ ];
public double vol[ ];
public double rho0[ ];
public double mas[ ];
public double dmas[ ];

double pi = Math.PI;

public InitialGrids(int icMat, int ocMat, int manMat, int shells, double massP, double ficore, double focore, double fman) 

{}

**This class is supposed to update the array. sorry for the length

public class Iterator 

  {
final double G;
final double PI;
double Pave;
double x, z, dy, dz, dydx; // this is for the iteration that will update the density
double delta;

public double grav[ ];
public double P[ ];
public double rho0n[ ];

int mix;



public Iterator(int icMat, int ocMat, int manMat, int shells, double massP, double ficore, 

      double focore, double fman) 
{
    InitialGrids ig = new InitialGrids(icMat, ocMat, manMat, shells, massP, ficore, 
       focore, fman);
    Constants c = new Constants();
    PlanMat pm = new PlanMat();

    G = c.GC;
    PI = c.PI;

    // calculation of gravity force at each shell

    grav = new double [ shells + 1 ];

    for(int k = 0; k <= shells; k++)
    {
        grav[ k ] = ( (G*ig.mas[k]) / (Math.pow(ig.rad[k], 2)) );   
    }

    // calculation of pressure at zone boundaries

    P = new double [ shells + 1 ];

    for(int k = shells - 1; k >= 0; k-- )
    {
        P[shells] = 0;

        P[ k ] = 0.5 * (grav[ k + 1 ] + grav[ k ]) * (ig.rad[ k + 1] - ig.rad[ k ]) *
            ig.rho0[ k + 1 ];

    }

    // this will calculate for the average pressure grid

    rho0n = new double[ shells + 1 ];

    for(int k = 1; k <= shells; k++)
    {
        rho0n[ 0 ] = 0;
        Pave = 0.5 * (P[ k ] + P[ k - 1 ]);

        if(pm.eos[ ig.iMat[ k ] ] == 1)
        {
            z = 0.75*(pm.Ksp[ ig.iMat[ k ] ] - 4.0);
            x = 1.0;
            for(int j = 1; j <= 20; j++)
            {
                dy = 1.5 * (pm.Ks0[ ig.iMat[k] ]) * Math.pow(x, 5) *
                                     (z * Math.pow(x,4) + (1.0 - 2.0 * z) * Math.pow(x, 2) + (z - 1.0)) - Pave ;
                dydx = 1.5 * (pm.Ks0[ ig.iMat[ k ] ]) * Math.pow(x, 4) *
                                     (9.0 * z * 

Math.pow(x, 4) + 7.0 * (1.0 - 2*z) * Math.pow(x, 2) + 5 * (z - 1.8)) ;
x = x - ( dy / dydx ); }

            rho0n[ k ] = ig.rho0[ k ] * Math.pow(x, 3);             
        }

        else
        {
            rho0n[ k ] = pm.c[ ig.iMat[k] ] * Math.pow(Pave , pm.nn[ ig.iMat[k] ]);
            rho0n[ k ] = rho0n[ k ] + pm.rho0[ ig.iMat[k] ] ;
        }

    }

    // The following will: define the change in density after iterations, mix the old and new densities and then update the radial grids

    delta = (Math.abs( rho0n[1] / ig.rho0[1] ) - 1);

    mix = 1;

    for(int k = 0; k <= shells; k++)
    {
        rho0n[ k ] = rho0n[ k ] + ig.rho0[ k ] * (1 - mix);             //notice that rho0 = rho in desch's code. dont worry
    }

    // radius update using density dependent volume and then calculating radius from volume

    for(int k = 1; k <= shells; k++)
    {
        ig.rad[ 0 ] = 0;
        ig.vol[ 0 ] = 0;

        ig.vol[ k ] = ( ig.dmas[ k ] / ig.rho0[ k ] );
        ig.rad[ k ] = Math.pow( ( 0.75 * ig.dmas[ k ] / ig.rad[ k ] / PI + Math.pow(ig.rad[ k - 1 ], 3) ) , 1.0 / 3 );
    }
3
  • 1
    For starters, it looks like it might be very helpful to refactor your data structure. You have what looks like parallel arrays, where the same spot in each array corresponds to one composite item. If that's the case, pull the fields into one class (maybe InitialGrid), and have an array of InitialGrid instead. Regarding your problem, you haven't posted any of the code that actually deals with changing anything. Commented Feb 11, 2014 at 9:19
  • How are the arrays going to be updated? Will there be some mathematical function through which all the values are processed, or will the actual size of the array also be affected? Commented Feb 11, 2014 at 9:23
  • I just posted the class that updates the arrays. It wasnt formatting so it too me a bit Commented Feb 11, 2014 at 9:24

3 Answers 3

3

One thing to remember in Java is that primitive types are passed by value whereas with Objects the reference to the object is passed by value.

If you pass a primitive into a method, and change it inside that method when you return from that method the initial value will not have changed as you have only changed a copy of it inside the method.

To get a new value out of the method you must return the value from the method and set the old value to the return of the method.

If you have multiple values that need to be returned you can't do this in Java, as a method may only have one return value.

One thing you could do is rather than use [] arrays you could use Array objects and pass these in as arguments. THen any changes you do inside the method will be reflected when you return from the method as you were editing the same underlying data object. (Note though that if you reassign this array objects, like inputArrayObject = new ArrayList(); , inside the method you will lose the reference to the external object and any later changes will no longer be reflected when you return from the method.

Edit: as chrylis said, it looks like those arrays of your are probably closely related and should maybe be pulled into a class, which you could then pass an object of that class into a method which would properly change the values, or call a class method on the object depending on what exactly needs to be done.

Edit2: Not high enough Rep to comment on question so I'll put this here. Now that you have posted the code I see the first thing you do with all the arguments is pass them into an InitialGrip constructor, why not construct that object externally and pass that into the Iterator constructor, it would be cleaner. Also you should try and break some of those mathematical operations you are doing into methods. So rather than have localVar = (a ton of math) have localVar = calucalteDensity(arguments). It will make it a little easier to read and debug and will let you reuse anything you need to (I didn't get right into the code to know exactly what it's doing so maybe nothing can be reused, everything might be different)

Sign up to request clarification or add additional context in comments.

6 Comments

would you be able to provide a brief example of this? Sounds like a good idea, im just not quite sure if I understand it correctly
An example of what particularily?
constructing the object externally and passing it through the method. Do you mean like what the answer below meant?
For instance you have this in your code public Iterator(int icMat, int ocMat, int manMat, int shells, double massP, double ficore, double focore, double fman) { InitialGrids ig = new InitialGrids(icMat, ocMat, manMat, shells, massP, ficore, focore, fman); ... } which means somewhere you are calling Iterator it = new Iterator(array1, array2, array3 ...) Try doing this though IntialGrid initGrid = new InitialGrid(aaray1,array2,array3 ...); Iterator it = new Iterator(initGrid);
I see. My problem also seems to be that I have one method and no return types.
|
0

I don't know if I've got your question exactly, however if you want to pass an array to a method you can simply pass it and set an array of the same type as the output of your method;like this:

int[] methodNae(int iMat[ ]) {
    int[] result;// you can even change the iMat and send it as the result
    //Do whatever you want with iMat
    return result;
}

Comments

0

Try defining your array as a static variable so that it acts globally. That way, any change made to that method in a function will affect the overall value of that variable. In this case:

public class mySample{
public static double[] rad = {};
}

Can be accessed by calling mySample.rad and the changes made to it in any function will also come with the variable call in another class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.