vectors and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ILtech
    New Member
    • Oct 2008
    • 6

    vectors and arrays

    "3. (5 points) Write a method for a class that uses an array

    For the Vector class given below, write a method that computes the dot product of the Vector "this" object with the Vector object passed as an argument, "b". The dot-product is the sum of the products of the corresponding elements of the vectors. The Vectors must be the same length for the dot-product to be defined. For example, if a = {1,2,3,4} and b = {2,4,6,8}, the dot product would be 1*2 + 2*4 + 3*6 + 4*8 = 60.
    [code=java]
    public class Vector {
    private double [] a;
    public Vector(double newData[]) {
    a=new double[newData.length];
    for (int i=0; i<newData.lengt h; i++)
    a[i]=newData[i];
    }
    public int getLength() {
    return a.length;
    }
    public double getEntry(int index) {
    return a[index];
    }
    public double dotProduct(Vect or b) {
    // YOUR CODE GOES HERE
    }
    }[/code]
    "

    So this is my assignment. The title suggests only using arrays and the constructor takes an array but the dotProduct method takes a vector as an argument. So, I'm assuming I'm supposed to convert vector b to an array or convert array a to a vector.

    So, my question is this: How do I convert to one or the other? I've looked around for examples but they don't seem to make sense or translate very well into my situation. Help, please?
    Last edited by Nepomuk; Oct 15 '08, 09:13 PM. Reason: Added [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by ILtech
    ...So, my question is this: How do I convert to one or the other?
    Simple: You don't have to. You have a method called getEntry in there, so if you want to get an array from your vector, you can just create a new one with those entries. But you can just as well use the given Vector and use a.getEntry(i) instead of [i]a.

    Greetings,
    Nepomuk

    Comment

    • ILtech
      New Member
      • Oct 2008
      • 6

      #3
      Uhm...kay?

      The thing that's throwing me off is that my class hasn't said a word about vectors so I don't really know how to work with them.

      If I wanted to make an array out of the vector in the dotProduct, how would I do that?

      Something like...

      Code:
      double [] bArray = new double[b.size()] //the b.size() gives me an error...and i've imported java.util.* so what do i do here?
      
      for (int i = 0; i < b.size(); i++) {
      bArray[t] = b.getEntry(t);
      }
      :s Thank you for your help.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by ILtech
        Uhm...kay?

        The thing that's throwing me off is that my class hasn't said a word about vectors so I don't really know how to work with them.

        If I wanted to make an array out of the vector in the dotProduct, how would I do that?

        Something like...

        Code:
        double [] bArray = new double[b.size()] //the b.size() gives me an error...and i've imported java.util.* so what do i do here?
        
        for (int i = 0; i < b.size(); i++) {
        bArray[t] = b.getEntry(t);
        }
        :s Thank you for your help.
        OK, you seem to be confused - there IS a class Vector in the package java.util.*, but you have one defined yourself (see, it says public class Vector in your code), so you have to work with that. It doesn't have a method called size(), instead it's called getLength() or something like that.
        Other than that, I think your code should be correct so far.

        Greetings,
        Nepomuk

        Comment

        • ILtech
          New Member
          • Oct 2008
          • 6

          #5
          Thank you very much. I've got it all working now. :]

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by ILtech
            Thank you very much. I've got it all working now. :]
            You're welcome of course and I'm glad it's working now. :-)

            Greetings,
            Nepomuk

            Comment

            Working...