Create a class Dir2d simulating 2-dimentional vectors, using the following UML d
ID: 3689295 • Letter: C
Question
Create a class Dir2d simulating 2-dimentional vectors, using the following UML diagram: - xCoord : double - yCoord: double + Dir2d () Constructs the vector object (0, 0) + Dir2d (x: double, y: double) Constructs the vector object (x, y) + getX(): double Returns the x-coordinate of the vector object + getY(): double Returns the y-coordinate of the vector object + sum (other: Dir2d): Dir2d Returns the sum of the current object and another one + dif (other: Dir2d): Dir2d Returns the difference between the current object and another one + scalar (other: Dir2D): double Returns the scalar product of the current object and another one + rotate (angle: double): Dir2D Returns a new vector object by rotating the original by the given angle in radians Create a driver program in Java that tests the class creation.Explanation / Answer
Java Program:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.twod;
import java.text.NumberFormat;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.geometry.Point;
import org.apache.commons.math3.geometry.Space;
import org.apache.commons.math3.geometry.Vector;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.util.MathUtils;
/** This class represents a 2D vector.
* <p>Instances of this class are guaranteed to be immutable.</p>
* @since 3.0
*/
public class Dir2D implements Vector<Euclidean2D> {
/** Origin (coordinates: 0, 0). */
public static final Dir2D ZERO = new Dir2D(0, 0);
// CHECKSTYLE: stop ConstantName
/** A vector with all coordinates set to NaN. */
public static final Dir2D NaN = new Dir2D(Double.NaN, Double.NaN);
// CHECKSTYLE: resume ConstantName
/** A vector with all coordinates set to positive infinity. */
public static final Dir2D POSITIVE_INFINITY =
new Dir2D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
/** A vector with all coordinates set to negative infinity. */
public static final Dir2D NEGATIVE_INFINITY =
new Dir2D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
/** Serializable UID. */
private static final long serialVersionUID = 266938651998679754L;
/** Abscissa. */
private final double x;
/** Ordinate. */
private final double y;
/** Simple constructor.
* Build a vector from its coordinates
* @param x abscissa
* @param y ordinate
* @see #getX()
* @see #getY()
*/
public Dir2D(double x, double y) {
this.x = x;
this.y = y;
}
/** Simple constructor.
* Build a vector from its coordinates
* @param v coordinates array
* @exception DimensionMismatchException if array does not have 2 elements
* @see #toArray()
*/
public Dir2D(double[] v) throws DimensionMismatchException {
if (v.length != 2) {
throw new DimensionMismatchException(v.length, 2);
}
this.x = v[0];
this.y = v[1];
}
/** Multiplicative constructor
* Build a vector from another one and a scale factor.
* The vector built will be a * u
* @param a scale factor
* @param u base (unscaled) vector
*/
public Dir2D(double a, Dir2D u) {
this.x = a * u.x;
this.y = a * u.y;
}
/** Linear constructor
* Build a vector from two other ones and corresponding scale factors.
* The vector built will be a1 * u1 + a2 * u2
* @param a1 first scale factor
* @param u1 first base (unscaled) vector
* @param a2 second scale factor
* @param u2 second base (unscaled) vector
*/
public Dir2D(double a1, Dir2D u1, double a2, Dir2D u2) {
this.x = a1 * u1.x + a2 * u2.x;
this.y = a1 * u1.y + a2 * u2.y;
}
/** Linear constructor
* Build a vector from three other ones and corresponding scale factors.
* The vector built will be a1 * u1 + a2 * u2 + a3 * u3
* @param a1 first scale factor
* @param u1 first base (unscaled) vector
* @param a2 second scale factor
* @param u2 second base (unscaled) vector
* @param a3 third scale factor
* @param u3 third base (unscaled) vector
*/
public Dir2D(double a1,Dir2D u1, double a2, Dir2D u2,
double a3, Dir2D u3) {
this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x;
this.y = a1 * u1.y + a2 * u2.y + a3 * u3.y;
}
/** Get the abscissa of the vector.
* @return abscissa of the vector
* @see #Vector2D(double, double)
*/
public double getX() {
return x;
}
/** Get the ordinate of the vector.
* @return ordinate of the vector
* @see #Vector2D(double, double)
*/
public double getY() {
return y;
}
/** {@inheritDoc} */
public Dir2D add(double factor, Vector<Euclidean2D> v) {
Dir2D v2 = (Dir2D) v;
return new Dir2D(x + factor * v2.getX(), y + factor * v2.getY());
}
/** {@inheritDoc} */
public Dir2D subtract(Vector<Euclidean2D> p) {
Dir2D p3 = (Dir2D) p;
return new Dir2D(x - p3.x, y - p3.y);
}
/** {@inheritDoc} */
public Dir2D subtract(double factor, Vector<Euclidean2D> v) {
Dir2D v2 = (Dir2D) v;
return new Dir2D(x - factor * v2.getX(), y - factor * v2.getY());
}
/** Compute the angular separation between two vectors.
* <p>This method computes the angular separation between two
* vectors using the dot product for well separated vectors and the
* cross product for almost aligned vectors. This allows to have a
* good accuracy in all cases, even for vectors very close to each
* other.</p>
* @param v1 first vector
* @param v2 second vector
* @return angular separation between v1 and v2
* @exception MathArithmeticException if either vector has a null norm
*/
public static double angle(Dir2D v1, Dir2D v2) throws MathArithmeticException {
double normProduct = v1.getNorm() * v2.getNorm();
if (normProduct == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
}
double dot = v1.dotProduct(v2);
double threshold = normProduct * 0.9999;
if ((dot < -threshold) || (dot > threshold)) {
// the vectors are almost aligned, compute using the sine
final double n = FastMath.abs(MathArrays.linearCombination(v1.x, v2.y, -v1.y, v2.x));
if (dot >= 0) {
return FastMath.asin(n / normProduct);
}
return FastMath.PI - FastMath.asin(n / normProduct);
}
// the vectors are sufficiently separated to use the cosine
return FastMath.acos(dot / normProduct);
}
public double dotProduct(final Vector<Euclidean2D> v) {
final Dir2D v2 = (Dir2D) v;
return MathArrays.linearCombination(x, v2.x, y, v2.y);
}
/** {@inheritDoc} */
public double getNorm() {
return FastMath.sqrt (x * x + y * y);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.