Purpose:
This is my first program from my first Data Structures class. Using two arrays (data structure) as a "Vector" (a set of n real numbers that could grow) I had to perform addition, subtraction, dot-product, check for equivalence, scaler-product, and absolute value. The definitions for the methods that our instructor gave us is as follows:
A
vector x consists of n real components [x1, x2,
…, xn]. The operations
between two vectors x = [x1, x2, …, xn]
and y = [y1, y2,
…, yn] are defined as follows:
addition
[x1 + y1, x2
+ y2, …, xn + yn]
subtraction
[x1 - y1, x2
- y2, …, xn - yn]
dot-product x · y = x1 * y1 + x2 * y2
+ … + xn * yn
equivalence x == y if (x1 = y1 and x2 = y2 and … and xn
= yn)
scalar-product s * x = [s * x1, s * x2, …, s * xn],
where s is a real number.
absolute-value |
x | = Ö (x · x)
The requirements were two classes a "MyVector" Class that held the mathmatical methods and a "Driver" class with the hard coded values of the two arrays as well as the main method.
Classes:
MyVector class housed the all of the mathematical methods.
package project1;
import java.util.*;
public class MyVector {
//Feilds
ArrayList vector = new ArrayList();
ArrayList copyVect = new ArrayList();
double[] initCopy;
/**
* Constructor : constructs the object
* @param <code>double</code> values inside an array
*
*/
public MyVector(double[] initValues) {
initCopy = new double[initValues.length];
for (int i = 0; i < initValues.length; i++) {
vector.add(i, initValues[i]);
initCopy[i] = initValues[i];
}
}
/**
* Copy Constructor: Copies the original values of the object used when
* invoking this method.
*
* @param original - object to hold original values.
*/
public MyVector(MyVector original) {
copyVect = new ArrayList(original.vector);
}
/**
* Returns the value of a given index in the
* <code>ArrayList vector</code> or the <code>Object</code> from the
* copy constructor
*
* @param the index of the value to return.
* @return value at the index given.
*/
public double getValue(int index) {
double tempVal = 0.0;
if (!(this.vector.isEmpty())) {
return tempVal = (Double) this.vector.get(index);
} else {
return tempVal = (Double) this.copyVect.get(index);
}
}
/**
* Addition method which adds the element of the same index of
* two ArrayLists
*
*
* @param myVector object
* @return object that contains the results
*/
public MyVector plus(MyVector a) {
MyVector c = new MyVector(a);
double temp = 0.0;
double temp2 = 0.0;
for (int i = 0; i < a.copyVect.size(); i++) {
temp = (Double) a.copyVect.get(i);
temp2 = (Double) this.vector.get(i);
c.vector.add(i, temp + temp2);
}
return c;
}
/**
* Subtraction method which subtracts the element of the same index of
* two ArrayLists
*
*
* @param myVector object
* @return object that contains the results
*/
public MyVector minus(MyVector a) {
MyVector c = new MyVector(a);
double temp = 0.0;
double temp2 = 0.0;
for (int i = 0; i < a.copyVect.size(); i++) {
temp = (Double) a.copyVect.get(i);
temp2 = (Double) this.vector.get(i);
c.vector.add(i, temp2 - temp);
}
return c;
}
/**
* Multiplication by a constant
*
*
* @param the constant or scaler
* @return object that contains the results
*/
public MyVector scaledBy(Double a) {
double[] initC = {0.0, 0.0, 0.0, 0.0, 0.0};
MyVector c = new MyVector(initC);
double temp = 0.0;
if (!this.vector.isEmpty()) {
for (int i = 0; i < this.vector.size(); i++) {
temp = (Double) this.vector.get(i) * a;
c.vector.add(i, temp);
}
} else {
for (int i = 0; i < this.copyVect.size(); i++) {
temp = (Double) this.copyVect.get(i) * a;
c.vector.add(i, temp);
}
}
return c;
}
/**
* This returns a textual representation of the object placed before
* invoking the object, i.e. vector.toString(); @Override ,
* <code>Object</code> class toString method
*
* @return returns a String representation of the Object.
*/
@Override
public String toString() {
String str = "";
double temp = 0.0;
if (!(this.vector.isEmpty())) {
for (int i = 0; i < vector.size(); i++) {
str += vector.get(i);
if (i != (vector.size() - 1)) {
str += ", ";
}
}
} else {
for (int i = 0; i < copyVect.size(); i++) {
str += copyVect.get(i);
if (i != (copyVect.size() - 1)) {
str += ", ";
}
}
}
return "[" + str + "]";
}
/**
* Returns a
* <code>boolean</code> value depending on if one object is equal to
* another.
*
* @Override equals() Method in Object Class
*
* @return boolean value
*/
@Override
public boolean equals(Object ob) {
if (this == ob) {
return true;
}
if (ob == null) {
return false;
}
if (!(ob instanceof MyVector)) {
return false;
}
MyVector compareVect = (MyVector) ob;
return this.vector.equals(compareVect.vector);
}
/**
* Absolute value method which finds the absolute value of a given
* element in a single ArrayList
*
* @return object that contains the results
*/
public MyVector abs() {
MyVector c = new MyVector(this);
double temp = 0.0;
if (!(this.vector.isEmpty())) {
for (int i = 0; i < this.vector.size(); i++) {
temp = Math.abs((Double) this.vector.get(i));
c.vector.add(i, temp);
}
} else {
for (int i = 0; i < this.copyVect.size(); i++) {
temp = Math.abs((Double) this.copyVect.get(i));
c.vector.add(i, temp);
}
}
return c;
}
/**
* Dot product method multiplies each element of the same index
* and then adds all the separate elements up for one number.
*
* @return <code>double</code> value
*/
public double dot(MyVector a) {
double temp, temp2, result = 0.0;
double[] temp3 = {0.0, 0.0, 0.0, 0.0, 0.0};
for (int i = 0; i < a.copyVect.size(); i++) {
temp = (Double) a.copyVect.get(i);
temp2 = (Double) this.vector.get(i);
temp3[i] = temp * temp2;
}
for (int i = 0; i < temp3.length; i++) {
result += temp3[i];
}
return result;
}
}
MyVectorDriver class housed the main method and the two arrays that were the "vectors."
package project1;
public class MyVectorDriver {
public static void main(String[] args) {
//Local variables
double[] a = {1.5, 2.4, -7.0, 3.3, 5.5};
double[] b = {2.5, 2.7, 10.1, -3.4, 5.6};
double getValue, dot;
Double scaler = 2.0;
//MyVector object instantiation
MyVector v1 = new MyVector(a);
MyVector v2 = new MyVector(v1);
v1 = new MyVector(b);
MyVector vectContainer;
//My Vector v1 printout
System.out.println("MyVector v1\n");
for (int i = 0; i < v2.copyVect.size(); i++) {
getValue = v1.getValue(i);
System.out.println("Position " + i + " of MyVector " + "is "
+ getValue);
}
System.out.println("v1 = " + v1.toString()
+ " and is built from array (" + b[0] + " , " + b[1] + " , "
+ b[2] + " , " + b[3] + " , " + b[4] + ")\n");
//My Vector v2 printout
System.out.println("MyVector v2\n");
for (int i = 0; i < v1.vector.size(); i++) {
getValue = v2.getValue(i);
System.out.println("Position " + i + " of MyVector " + "is "
+ getValue);
}
System.out.println("v2 = " + v2.toString()
+ " and is built from array (" + a[0] + " , " + a[1] + " , "
+ a[2]+ " , " + a[3] + " , " + a[4] + ")\n");
//Equals Method demonstration
if (v1.equals(v2)) {
System.out.println(v1 + " == " + v2);
}
if (!(v1.equals(v2))) {
System.out.println(v1 + " != " + v2);
}
if (v1.equals(v1)) {
System.out.println(v1 + " == " + v1);
}
if (v2.equals(v2)) {
System.out.println(v2 + " == " + v2);
}
System.out.println("\nComputations\n");
//Calling the addition method
vectContainer = v1.plus(v2);
System.out.println(v1 + " + " + v2 + " = " + vectContainer);
//Calling the Subtraction method
vectContainer = v1.minus(v2);
System.out.println(v1 + " - " + v2 + " = " + vectContainer);
//Calling the scaler method
vectContainer = v1.scaledBy(scaler);
System.out.println(scaler + " * " + v1 + " = " + vectContainer);
vectContainer = v2.scaledBy(scaler);
System.out.println(scaler + " * " + v2 + " = " + vectContainer);
//Calling absolute value method
vectContainer = v1.abs();
System.out.println("|" + v1 + "|" + " = " + vectContainer);
vectContainer = v2.abs();
System.out.println("|" + v2 + "|" + " = " + vectContainer);
//Calling dot product Mehtod
dot = v1.dot(v2);
System.out.println(v1 + " . " + v2 + " = " + dot);
}
}
My grade on this particular project was 96/100.
No comments:
Post a Comment