Define a class of vectors. Let us start with an implementation in which each ins
ID: 3567350 • Letter: D
Question
Define a class of vectors. Let us start with an implementation in which each instance of this class has three real-valued fields: the three coordinates x1, x2, and x3 of the vector. Define a constructor, appropriate set-methods (modifiers) and get-methods, operations on vectors (see below), and a method that prints the vector as a triple (x1, x2, x3). Use the main method to test your class. For example, show how you can use your methods to normalize a given vector, i.e., to divide it by its length. Motivation: to practice classes and objects. Background. While computers have been originally designed for a serious task of processing data, nowadays a large amount of computer resources is spent on playing computer games. One of the many things that attracts people to computer games is their ability to compute realistic images of 3-D scenes. Many graphics algorithms behind these games use 3-D vectors. Let us therefore practice simple operations with vectors. Assignment for Lab 9. Define a class of vectors. Let us start with an implementation in which each instance of this class has three real-valued fields: the three coordinates x1, x2, and x3 of the vector. Define a constructor, appropriate set-methods (modifiers) and get-methods, operations on vectors (see below), and a method that prints the vector as a triple (x1, x2, x3). Use the main method to test your class. For example, show how you can use your methods to normalize a given vector, i.e., to divide it by its length. The following operations should be defined for vectors: . multiplication of a vector by a real number a: a * (x1, x2, x3) is defined as (a * x1, a * x2, a * x3); . division of a vector by a real number a: (x1, x2, x3) /a is defined as (x1 / a, X2 / a, x3 / a); . the length of a vector; the length is defined as |x|= root((x1)2 + (x2)2 + (x3)2); . a method for checking whether the vector is a zero vector, i.e., whether all three of its components are equal to 0; . the sum of two vectors; it is defined component-wise: (x1, x2, x3) + (y1, y2 y3) = (x1 + y1, x2 + y2, x3 + y3); . the difference between two vectors; it is also defined component-wise: (x1, x2, x3) - (y1, y2, y3) = (x1 - y1, X2 - y2, X3 - y3); . the dot product between the two vectors: (x, y) = x1 * y + x * y + X3 * y3. For extra credit: . write down an alternative implementation, in which a vector is represented by an array; by calling the corresponding methods in the main program, show that the results do not change if you replace the original number-based implementation with the array-based one; . implement vector product -- and any other additional operations with vectors.Explanation / Answer
//Vector class and its metods implmenations and testing the vector class
//Vector.java
public class Vector
{
//fields of the vector class
//to store x1,x2,x3
private double x1;
private double x2;
private double x3;
//constructor to set the arguments of the vector
public Vector(double x1,double x2,double x3)
{
//call setter methods to set the values of fieds
setX1(x1);
setX2(x2);
setX3(x3);
}
//setter methods
public void setX3(double x3)
{
this.x3=x3;
}
public void setX2(double x2)
{
this.x2=x2;
}
public void setX1(double x1)
{
this.x1=x1;
}
//getter methods
public double getX3()
{
return x3;
}
public double getX2()
{
return x2;
}
public double getX1()
{
return x1;
}
//Method to multiply by a real number
public void multiplyBy(double a)
{
x1=a*x1;
x2=a*x2;
x3=a*x3;
}
//Method to division by a real number
public void divisionBy(double a)
{
x1=x1/a;
x2=x2/a;
x3=x3/a;
}
//Method to division by a real number
public double length()
{
return Math.sqrt(x1*x1+x2*x2+x3*x3);
}
//Checking whether the vector is zero vector
public boolean isZeroVector()
{
return x1==0 &&x2==0 &&x3==0;
}
//Method for sum of a vectors
public Vector sum(Vector otherVector)
{
x1=x1+otherVector.getX1();
x2=x2+otherVector.getX2();
x3=x3+otherVector.getX3();
return new Vector(x1, x2, x3);
}
//Method for difference of a vectors
public Vector difference(Vector otherVector)
{
x1=x1-otherVector.getX1();
x2=x2-otherVector.getX2();
x3=x3-otherVector.getX3();
return new Vector(x1, x2, x3);
}
//Method to product two vectors
public Vector product(Vector otherVector)
{
x1=x1*otherVector.getX1();
x2=x2*otherVector.getX2();
x3=x3*otherVector.getX3();
return new Vector(x1, x2, x3);
}
@Override
public String toString() {
return "X1 : "+x1+" X2 : "+x2+" X3 : "+x3+" ";
}
}//end of vector class
---------------------------------------------------------------------------------------
//Test class to test the methods of vector class
//TestVector.java
public class TestVector
{
public static void main(String[] args)
{
Vector vector=new Vector(1, 2, 3);
System.out.println("---Vector operations---");
System.out.println(vector.toString());
System.out.println("Multiplication By 5");
vector.multiplyBy(5);
System.out.println(vector.toString());
System.out.println("Division By 5");
vector.divisionBy(5);
System.out.println(vector.toString());
System.out.println("Lenght of the vector ");
System.out.println(vector.length());
System.out.println("Checking if the vector has all zero components");
if(vector.isZeroVector())
System.out.println("Vector has zero arguments");
else
System.out.println("Vector has no zero arguments");
System.out.println("Sum of the two vectors");
Vector otherVector=new Vector(4, 5, 6);
vector.sum(otherVector);
System.out.println(vector.toString());
System.out.println("Difference of two vectors");
vector.difference(otherVector);
System.out.println(vector.toString());
System.out.println("Product of two vectors");
vector.product(otherVector);
System.out.println(vector.toString());
}
}//end of test class
------------------------------------------------------------------------------------------------
Sample output:
---Vector operations---
X1 : 1.0
X2 : 2.0
X3 : 3.0
Multiplication By 5
X1 : 5.0
X2 : 10.0
X3 : 15.0
Division By 5
X1 : 1.0
X2 : 2.0
X3 : 3.0
Lenght of the vector
3.7416573867739413
Checking if the vector has all zero components
Vector has no zero arguments
Sum of the two vectors
X1 : 5.0
X2 : 7.0
X3 : 9.0
Difference of two vectors
X1 : 1.0
X2 : 2.0
X3 : 3.0
Product of two vectors
X1 : 4.0
X2 : 10.0
X3 : 18.0
Hope this would be helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.