Help with Java Code

Oct 25, 2006
11,036
11
91
Hello

I'm working on a piece of java code that should be able to add, subtract, multiply by a constant, and divide vectors.

I've been reading up on resources, but they aren't very helpful.

can anyone point me in the right direction in terms of how you go about adding 2 vecotrs in Java?
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
The first three are rather easy. You can use the straightforward algorithms you learned in elementary school. I'll give an example with addition assuming that the vectors have the lowest order digits in the smaller indexes (e.g. the number 1234 would be stored in an array like [4 3 2 1], though resizable since it's a vector).

First, get the length of the vectors (i.e. the number of digits in each number you want to add). Let's say I'm computing 1234 + 50. The lengths of these numbers are 4 and 2. The length of the output can be at most 5 digits, so for generality I should loop 5 times (or rather, max(length(n1), length(n2)) + 1 in pseudocode).

Heck, I'll just write it in pseudocode from here:

public Vector add (Vector n1, Vector n2) {

int carry = 0;
Vector sum = new Vector();

for (int k = 0; k < max(n1.length, n2.length) + 1; k++) {
....int s = n1[k] + n2[k] + carry;
....sum[k] = s % 10;
....carry = s / 10;
}

return sum;

}

(I'm using the dots for indentation only, since I think the attach code feature is still broken.)

Basically, you just keep track of the carry and keep adding things through. Depending on your vector implementation, this may need to change. I'm assuming that indexing a higher order bit for a number that isn't large enough to have that bit non-zero will return zero (it should if these vectors are just being used for math). If it doesn't, you'll need an extra check for that.

EDIT: Changed index from i to k to avoid italics.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
It's probably a school project. We did this in my C class for a small project (implement vectors in C, implement big int calculations with vectors).
 

KCfromNC

Senior member
Mar 17, 2007
208
0
76
Could the original problem also be vectors in the physics sense, i.e. tuples of <x,y,z,...> coordinates? Granted dividing vectors doesn't make sense, but maybe he meant dividing by a scalar?

 

agibby5

Senior member
Jun 23, 2004
990
0
76
Originally posted by: Daishiki2
Originally posted by: Codewiz
fyi vectors are deprecated....

Aren't arraylists more the norm now?

Do you mean obsolete? I guess they are equivalent terms in this situation...

Anyway, when I was in college (just graduated) we only learned ArrayLists. Our teacher didn't much like Vectors... maybe that's the reason. I've never much used them.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Originally posted by: KCfromNC
Could the original problem also be vectors in the physics sense, i.e. tuples of <x,y,z,...> coordinates? Granted dividing vectors doesn't make sense, but maybe he meant dividing by a scalar?

That is how I read it, as well.
 

Codewiz

Diamond Member
Jan 23, 2002
5,758
0
76
Originally posted by: agibby5
Originally posted by: Daishiki2
Originally posted by: Codewiz
fyi vectors are deprecated....

Aren't arraylists more the norm now?

Do you mean obsolete? I guess they are equivalent terms in this situation...

Anyway, when I was in college (just graduated) we only learned ArrayLists. Our teacher didn't much like Vectors... maybe that's the reason. I've never much used them.

When I was in school java was the new kid on the block. Vectors were the data type to use.

But Vectors were deprecated in 1999 or 2000 I believe.

That means, they will work but you shouldn't implement new code using them. Once deprecated, they could be removed from the baseline at some point in the future.
 

agibby5

Senior member
Jun 23, 2004
990
0
76
Originally posted by: Codewiz
Originally posted by: agibby5
Originally posted by: Daishiki2
Originally posted by: Codewiz
fyi vectors are deprecated....

Aren't arraylists more the norm now?

Do you mean obsolete? I guess they are equivalent terms in this situation...

Anyway, when I was in college (just graduated) we only learned ArrayLists. Our teacher didn't much like Vectors... maybe that's the reason. I've never much used them.

When I was in school java was the new kid on the block. Vectors were the data type to use.

But Vectors were deprecated in 1999 or 2000 I believe.

That means, they will work but you shouldn't implement new code using them. Once deprecated, they could be removed from the baseline at some point in the future.

I see... I graduated college in 2006, so that's after the approximate depreciation date.
 
Oct 25, 2006
11,036
11
91
Yeah. The Vectors are in a physics sense. Basically, what I'm working on now is a program that simulates a galaxy. Its my first attempt at simulations, and my first task is coding the manipulation of vectors.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Codewiz
Originally posted by: agibby5
Originally posted by: Daishiki2
Originally posted by: Codewiz
fyi vectors are deprecated....

Aren't arraylists more the norm now?

Do you mean obsolete? I guess they are equivalent terms in this situation...

Anyway, when I was in college (just graduated) we only learned ArrayLists. Our teacher didn't much like Vectors... maybe that's the reason. I've never much used them.

When I was in school java was the new kid on the block. Vectors were the data type to use.

But Vectors were deprecated in 1999 or 2000 I believe.

That means, they will work but you shouldn't implement new code using them. Once deprecated, they could be removed from the baseline at some point in the future.
Vectors are not deprecated as of 1.6 (at least that's what the javadocs say). The only difference between Vectors and ArrayLists is that Vectors are thread-safe, which has some (unspecified) performance hit in single thread usage. Vectors don't quite fit in the regular api trends but I don't see any reason not to use them (unless you benchmark with both and notice a non-negligible difference). Everything should be hidden behind a List anyways
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Well if these are mathematical vectors, it depends on their representation. If they're given in tuples (e.g. <x, y, z>), then you should be able to add/subtract/multiply/divide them very easily.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: agibby5
Originally posted by: Daishiki2
Originally posted by: Codewiz
fyi vectors are deprecated....

Aren't arraylists more the norm now?

Do you mean obsolete? I guess they are equivalent terms in this situation...

Anyway, when I was in college (just graduated) we only learned ArrayLists. Our teacher didn't much like Vectors... maybe that's the reason. I've never much used them.

Vectors are "supposed" to be slower than ArrayLists since Vectors introduce snchronization overhead. The same applies to HashMap vs HashTable..

However, I believe starting with 1.5 JVM, the compiler/runtime detects if the object is being used in a single-threaded mode and bypasses synchronization. This should make the performance difference moot.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |