473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

vectors and arrays

ILtech
6
"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.
Expand|Select|Wrap|Line Numbers
  1.       public class Vector {
  2.          private double [] a;
  3.          public Vector(double newData[]) {
  4.             a=new double[newData.length];
  5.             for (int i=0; i<newData.length; i++)
  6.                a[i]=newData[i];
  7.          }
  8.          public int getLength() {
  9.             return a.length;
  10.          }
  11.          public double getEntry(int index) {
  12.             return a[index];
  13.          }
  14.          public double dotProduct(Vector b) {
  15.          // YOUR CODE GOES HERE
  16.          }
  17.       }
"

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?
Oct 15 '08 #1
5 1944
Nepomuk
3,112 Expert 2GB
...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
Oct 15 '08 #2
ILtech
6
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...

Expand|Select|Wrap|Line Numbers
  1. 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?
  2.  
  3. for (int i = 0; i < b.size(); i++) {
  4. bArray[t] = b.getEntry(t);
  5. }
  6.  
:s Thank you for your help.
Oct 15 '08 #3
Nepomuk
3,112 Expert 2GB
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...

Expand|Select|Wrap|Line Numbers
  1. 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?
  2.  
  3. for (int i = 0; i < b.size(); i++) {
  4. bArray[t] = b.getEntry(t);
  5. }
  6.  
: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
Oct 15 '08 #4
ILtech
6
Thank you very much. I've got it all working now. :]
Oct 15 '08 #5
Nepomuk
3,112 Expert 2GB
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
Oct 16 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: FLChamp | last post by:
SOrry if this message is a little confused, it most probably reflects the state of the author! I have made a small program that plots the orbit of a planet in visual python using visual.vector...
10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
2
by: J. Campbell | last post by:
I have a class that contains an array of integers that hold the state of my 'system'. The system must be updated periodically. I need to update the whole system at once, at which point the system...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
3
by: John Bend | last post by:
Hello. Can anyone please suggest some good tutorial links where Java Vectors and Arrays are explained and compared? I'm a proficient programmer but fairly new to Java. Whilst I understand...
7
by: Mack | last post by:
Hi Guys, In C++ , i was just wondering what is the advantage of Vectors over arrays? I saw one sample program doing pretty much same way traditional vectors do then what the use of VECTORS??or...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.