Question

In Java for [computing] please. Thanks. Implement a method public static double[] difference(double[] vec1, double[] vec2)...

In Java for [computing] please. Thanks.

Implement a method public static double[] difference(double[] vec1, double[] vec2) to compute the (component-wise) difference of vectors vec1 and vec2 as a new vector. Assume that vec1 and vec2 are of the same length. The difference of vectors (a1, a2, . . . , an) and (b1, b2, . . . , bn) is (a1 − b1, a2 − b2, . . . , an − bn). For example, the difference of (−2, −4, 9) and (3, −2, 4) is (−2 − 3, −4 − (−2), 9 − 4) = (−5, −2, 5). (7 marks)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.java

public class Main
{
   public static double[] difference(double[] vec1, double[] vec2){
   int l = vec1.length;
   double[] diff_vec = new double[l];
  
   for(int i=0; i<l; i++)
   diff_vec[i] = vec1[i] - vec2[i];
  
   return diff_vec;
   }
  
   public static void main(String[] args) {
   double[] vec1 = {-2, -4, 9};
   double[] vec2 = {3, -2, 4};
  
   double[] diff_vec = difference(vec1, vec2);
  
   System.out.print("Difference vector: ");
   for(int i=0; i<diff_vec.length; i++)
       System.out.print(diff_vec[i] + " ");
   }
}

Add a comment
Know the answer?
Add Answer to:
In Java for [computing] please. Thanks. Implement a method public static double[] difference(double[] vec1, double[] vec2)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a method public static double[] product(double[] v1, double[] v2) that, given two arrays, v1 and...

    Implement a method public static double[] product(double[] v1, double[] v2) that, given two arrays, v1 and v2, of floating-point numbers, returns a new array containing the component-wise product of vectors v1 and v2, that is, an array whose ith element is v1[i] * v2[i]. For example, if v1 is { 5, 2, 3 } and v2 is { -1, 2, 0 }, then the method should return { -5, 4, 0 }.

  • Solve it by JAVA please. DO NOT IMPORT PLEASE. THANKS Method 10: public static double nearestNeighbor(double...

    Solve it by JAVA please. DO NOT IMPORT PLEASE. THANKS Method 10: public static double nearestNeighbor(double v, double[] values) Return the value of the nearest neighbor to v. If v is some value, look through the list of values to find the one which is closest in value to v. For example, if v = 1.2 and the input array is {3.2, 1.7, 2.4, 0.9, 4.4}, then the method will return 0.9 because it is the value closest to 1.2...

  • implement a C++ class name Vector. The Vector class contains a private double array of length...

    implement a C++ class name Vector. The Vector class contains a private double array of length 2 named elements that represents a two-dimensional vector. We will also implement an overloaded multiplication operator (*) that accepts a single Vector variable by reference and returns the Dot Product between the current Vector object and the one pointed to by the function argument in form of a double. Please recall that the dot product of two vectors a =(21,92) and 5 = (b1,b2)...

  • IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is...

    IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is a palindrome or not. A palindrome is a phrase that reads the same forward​and backward; not including punctuation, spaces, or letter case. Some example palindromes include: Just the method please..

  • Problem 16)(Java) Write a complete definition for a public static method called closeEnough that takes a...

    Problem 16)(Java) Write a complete definition for a public static method called closeEnough that takes a double and an integer and returns true if the double is within one unit of the integer. For example closeEnough(3.9, 5) -> false closeEnough(4.1, 5) -> true closeEnough(6.0, 5) -> true

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which...

    public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which does the following: In the main method: Use a for loop which repeats three times. Within the loop, a. prompt the user to enter the base of the triangular prism and store the value in a double array called base b. prompt the user to enter the corresponding height and store the value in a double array called height c. prompt the user to...

  • C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can...

    C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can add two const references to 3-d vectors [vector<vector-vector<T>>>]. It should return a new 3-d vector that performed element-wise addition. You cannot use indexing on this problem, instead you must use iterators to access the values of end vector. INPUT OF THE TEST CASE 1 #include <vector> 2 using std: : vector; 3 const vector<double> a{1, 4, -6}; const vector<double> b{3, 6,...

  • Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double...

    Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double inches) { return 2.54 * inches; }public static double inTOmeter(double inches) { return 0.0254 * inches; }public static double inTOft(double inches) { return 0.083333 * inches; }public static double inTOyd(double inches) { return 0.0277778 * inches; }public static double inTOkm(double inches) { return 0.000254 * inches; }public static double ftTOmeter(double feet) { return 0.3048 * feet; }public static double ftTOyd(double feet) { return 0.0003048...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT