0

How do I give input directly, when that function is invoked in another method, especially when that input is a double[] array?

public double dotPro1(double[] vectorA, double[] vectorB) {
    double[] vecPro;
    vecPro = new double[2];
    vecPro[0] = vectorA[0]*vectorB[0];
    vecPro[1] = vectorA[1]*vectorB[1];
    return vecPro[0] + vecPro[1];
}
public double dotPro2(double[] length) {
    double[] lenPro;
    lenPro = new double[1];
    lenPro[0] = length[0];
    return lenPro[0];
}
public static double cosine(double a) {
    double x = Math.cos(Math.toRadians(a));
    /*Class c = Class.forName("NaiveStrategy");
        Class methodTypes[] = new Class[3];
        methodTypes[0] = Double.TYPE;
        methodTypes[1] = Double.TYPE;
        methodTypes[2] = Double.TYPE;
        Method[] m = c.getMethods();*/
    NaiveStrategy ns = new NaiveStrategy();
  problem-->ns.dotPro1(vectorA[], vectorB[]);
  problem-->ns.dotPro2(length[]);
    return 0;  
}

As you can also see my old coding I tried in another way to solve it, but it didn't worked. It's commented out above.

4
  • At which place are you facing the problem..can u pin point it Commented Sep 17, 2012 at 5:58
  • 3
    Why isn't dotPro2 just { return length[0]; }? What's with all the useless double[] manipulation? Commented Sep 17, 2012 at 6:00
  • @KumarVivekMitra, i have pinpointed my problem. Commented Sep 17, 2012 at 6:01
  • @basiljames, actually its not useful, i need an array of values to getValues for the length that is magnitude of the vectors in double Commented Sep 17, 2012 at 6:04

4 Answers 4

3

It's not at all clear what you mean - but certainly the syntax you've got at the moment won't work. If you're just looking to create an array at execution time, that's easy:

NaiveStrategy ns = new NaiveStrategy();
// Either using separate variables...
double[] first = { 0.5, 0.1, 10 };
double[] second { 5, 20.3, 1 };
double result1 = ns.dotPro1(first, second);

// Or inline...
double results2 = ns.dotPro2(new double[] { 50.2, 0.3 });

I can't tell why you've got commented-out reflection calls in your code though...

Also, as noted in comments, your methods seem to be creating arrays for no particular purpose. They could be rewritten as:

public double dotPro1(double[] vectorA, double[] vectorB) {
    return vectorA[0] * vectorB[0] + vectorA[1] * vectorB[1];
}

public double dotPro2(double[] length) {
    return length[0];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You said in one comment that you want random values. So use the Random class to generate them.

double[] list1 = new double[5];
double[] list2 = new double[5];
Random rand = new Random();
for (int i = 0; i < list1.length; i++) {
    list1[i] = rand.nextDouble();
}

for (int i = 0; i < list2.length; i++) {
    list2[i] = rand.nextDouble();
}

The new double[5]; will create an array of length 5. Change this number to the length you want.

Note: rand.nextdouble only gives a random number between 0 and 1. If you wish it to , say, be between 0 and 100 then use rand.nextDouble()*100

You can then just use

double result1 = ns.dotPro1(list1, list2);

Comments

0

When passing the array parameter do not put the square braces.

= ns.dotPro2(length);
= ns.dotPro1(vectorA, vectorB);

1 Comment

i know i am just showing my problem here. how do i directly assign values for my array here.
0

Sorry for the delay, as my net went off..... Ok back to the answer...

Here is the simplest way to do it...

double[] first = { 0.10, 0.15, 0.30 };
double[] second { 65, 44.3, 10.11 };
double result1 = ns.dotPro1(first, second);

4 Comments

thank you for the answer, but i need runtime input values for arrays
@so whats the probs in it..you can always input double variables into array during runtime... but i would suggest you to use Collection than going for array..its more flexible..
actually i am new to java programming! it would be nice if i get answers exactly what i need from you?
actually the answer u suggested me might help me generating just that declared value inside the braces, but i need random values to be generated.

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.