0

I was wondering how I could call getJD() within getToD and keep the parameters intact,(or temporarily set the parameters as variables in main and call the variables to the method). The parameters are going to be input using the scanner class later in the main method. import static java.lang.Math.*;

public class jdMethods
{
    public static double getJD(double y, double m, double d){
        if (m<=2.0){
            y--;
            m += 12.0;
        }
        double a=floor(y/100.0);
        return (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
    }

    public static double getToD(int h, int m, int s)
    {
        double a = getJD(a, a, a) + ((h-12)/24) + (m/1440) + (s/86400);
        return a;
    }
}

Edited for clarity.

Quesofat
  • 1,493
  • 2
  • 21
  • 49

2 Answers2

1

All those parameters will be intact since you are using double and int, those are not Object s so it's value is copied when passed to a function, unlike Object s that a reference to it is passed to the function.

About your code, undefined variable a won't let it compile:

double a = getJD( a, a, a ) + ((h-12)/24) + (m/1440) + (s/86400);

I don't get what you are trying to do there, remember that a from getJD method is not the same a into getToD.

HericDenis
  • 1,364
  • 12
  • 28
1

It's not perfectly clear on what you are trying to do, but I assumed that you just want to save the result of your first getJD() and to use the result within your getToD(), so I made a private _jd and created a setter and getter for it.

import static java.lang.Math.*;

public class jdMethods
{

    private double _jd;
    public double getJD(){
        return _jd;
    }

    public void setJD(double y, double m, double d){
        if (m<=2.0){
            y--;
            m += 12.0;
        }
        double a=floor(y/100.0);
        _jd = (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
    }

    public double getToD(int h, int m, int s)
    {
        double a = getJD() + ((h-12)/24) + (m/1440) + (s/86400);
        return a;
    }
}

So here is how you call it:

jdMethods testRun = new jdMethods();
testRun.setJD(1,2,3);
System.out.println(testRun.getToD(3, 2, 1));
16dots
  • 2,941
  • 1
  • 13
  • 15
  • Thanks! I understand some of it. Why get rid of `static`? And when `getJD()` is declared as `pubic` it means other classes can use it; correct? PS in line 18 it says the "local variable might not have been initialized" – Quesofat Nov 05 '12 at 17:01
  • @IrishEnigma static or not it depending on how you are going to use it, see this : http://stackoverflow.com/a/2671636/1342525 – 16dots Nov 05 '12 at 17:06
  • @IrishEnigma Yes, if it's public, it means other classes can access it after initializing the object. – 16dots Nov 05 '12 at 17:08
  • @IrishEnigma You should be calling setJD(x, y, z) first inorder to save the result as _jd, and then call your getToD(). – 16dots Nov 05 '12 at 17:10
  • Got it so static methods should only be used if you do not plan on changing the return value. – Quesofat Nov 05 '12 at 17:13
  • @IrishEnigma No, it has little or nothing to do with the return value, you should see static methods as independent contractor of a company(Class), the company will have a lot of different projects(Instances of the Class) to do, but it doesn't need to get a new contractor every single time, it can just use the same contractor over and over(Produces different results for different projects), and everybody else(Other classes) can also use this contractor, meaning this contractor isn't locked into this company's projects(instance of the Class), he can work independently on it self. – 16dots Nov 05 '12 at 17:44