Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with completing the rest of this code on vector methods. /** * Vector class * * The class Vector encapsulates an n-dimensional vector containing

Help with completing the rest of this code on vector methods.

/** * Vector class * * The class Vector encapsulates an n-dimensional vector containing n double numbers. It offers * the operations (methods) described below. * Note: created all methods as class members to allow easy chaining of commands, such as * * "a.add(b).subtr(c).isZero()" * * To see an example, look for the implemented add methods below; model other methods * after this one. * * CONSTRUCTORS * ------------ * Vector(double[] data) creates a new vector with the input array as values * Vector(int n) creates a new vector with n zeros as values * * METHODS (OPERATIONS) * -------------------- * Note: when appropriate, each method below checks whether the input vectors * are compatible, i.e. whether they have the same dimension. If not, it throws an * Error("incompatible vectors"). Recall that functions throwing an Error instead of an * Exception do not have to be embedded in a try-catch block. * * public int getDim() returns dimension of current vector * public double get(int i) returns i-th value of current vector if possible, * otherwise generates an error. * public Vector add(Vector b) returns sum of a and current vector if possible * public Vector multBy(double c) returns scalar multiple c * current vector * public Vector subtr(Vector b) returns difference of current vector and b * public double dot(Vector b) returns the dot product of current vector and b if possible * public boolean equals(Vector b) returns true if current vector equals b, else false * public boolean isPerpTo(Vector b) returns true if current vector is perpendicular to b, else false * public boolean isParallelTo(Vector b) returns true if current vector is parallel to b, else false * public double abs() returns length of the current vector * public boolean isZero() returns true if current vectors has all zero entries, else false * public String toString() returns a string representation of current vect */ public class Vector { // field to store the values of the current vector. private double[] data = null;

// Sample implementation of the first constructor of a Vector. You can then create a new // Vector v = < 1,0, -1> by saying, for example: // // Vector v = new Vector(new double[] {1.0, 0.0, -1.0}) // public Vector(double[] data) { this.data = data; } // Sample implementation of the second constructor of a Vector. You can then create a new // (empty) vector v by saying, for example: // // Vector v = new Vector(3); // public Vector(int dim) { data = new double[dim]; // all values are set to zero automatically } // getDim -- returns dimension of current vector public int getDim() { return data.length; } //get i-th -- returns i-th value of current vector if possible,otherwise generates an error. public double get(int i) { return data[i]; } // add -- returns sum of a and current vector if possible public Vector add(Vector b) { if (getDim() != b.getDim()) throw new Error("Incompatible Vector1s for addition");

Vector1 sum = new Vector1(getDim()); for (int i = 0; i < getDim(); i++) sum.set(i, get(i) + b.get(i));

return sum; }

// multiply -- returns scalar multiple c * current vector public Vector multBy(double c) { Vector ans = new Vector(getDim()); for(int i = 0; i < getDim(); i++) ans.set(i, get(i) * c);

return ans; }

// subtract -- returns difference of current vector and b public Vector subtr(Vector b) { if (getDim() != b.getDim()) throw new Error("Incompatible Vector1s for subtraction"); Vector1 ans = new Vector1(getDim()); for (int i = 0; i < getDim(); i++) ans.set(i, get(i) - b.get(i)); return ans; }

// dot -- returns the dot product of current vector and b if possible public double dot(Vector b) { }

// equals -- returns true if current vector equals b, else false public boolean equals(Vector b) { }

// perpendicular -- returns true if current vector is perpendicular to b, else false public boolean isPerpTo(Vector b) { }

// parallel -- returns true if current vector is parallel to b, else false public boolean isParallelTo(Vector b) { }

// length or abs -- returns length of the current vector public double abs() { }

// isZero? -- returns true if current vectors has all zero entries, else false public boolean isZero() { }

// toString -- returns a string representation of current vect public String toString() { }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions