Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi how am I doing this wrong. I how to properly order this public class Vector2D { private double xComp; private double yComp; public Vector2D(){
Hi how am I doing this wrong. I how to properly order this
public class Vector2D {
private double xComp;
private double yComp;
public Vector2D(){
xComp = 0;
yComp = 0;
}
public Vector2D(double xComp, double yComp) {
super();
this.xComp = xComp;
this.yComp = yComp;
}
public Vector2D(Vector2D v){
this.xComp = v.xComp;
this.yComp = v.yComp;
}
public double getX() {
return xComp;
}
public double getY() {
return yComp;
}
public void setVector2D(double xComp, double yComp) {
this.xComp = xComp;
this.yComp = yComp;
}
public double norm(){
return Math.sqrt(xComp*xComp + yComp*yComp);
}
public Vector2D unit(){
if(xComp == 0 || yComp == 0)
throw new IllegalArgumentException("Zero Vector can not have Unit Vector");
else{
xComp = xComp/this.norm();
yComp = yComp/this.norm();
return this;
}
}
public Vector2D add(Vector2D v){
Vector2D temp = new Vector2D();
temp.xComp = this.xComp + v.xComp;
temp.yComp = this.yComp + v.yComp;
return temp;
}
public Vector2D sub(Vector2D v){
Vector2D temp = new Vector2D();
temp.xComp = this.xComp - v.xComp;
temp.yComp = this.yComp - v.yComp;
return temp;
}
public double dot(Vector2D v){
return this.xComp*v.xComp + this.yComp*v.yComp;
}
public Vector2D sMult(double scalar){
this.xComp = this.xComp*scalar;
this.yComp = this.yComp*scalar;
return this;
}
public boolean equals(Vector2D v){
if(this.xComp == v.xComp && this.yComp == v.yComp)
return true;
else
return false;
}
@Override
public String toString() {
return "";
}
}
Vector2D demo
package first;
import java.util.Scanner;
public class Vector2DDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter x and y components of the first vector->");
double x1 = scan.nextDouble();
double y1 = scan.nextDouble();
Vector2D v1 = new Vector2D(x1, y1);
System.out.println("Enter x and y components of the second vector->");
double x2 = scan.nextDouble();
double y2 = scan.nextDouble();
Vector2D v2 = new Vector2D(x2, y2);
System.out.println("Enter x and y components of the third vector->");
double x3 = scan.nextDouble();
double y3 = scan.nextDouble();
Vector2D v3 = new Vector2D(x3, y3);
System.out.println();
System.out.println(v1.toString());
System.out.println(v2.toString());
System.out.println(v3.toString());
System.out.println();
System.out.print("(v1 + v2) - (v1 -v3) = ");
Vector2D v4 = v1.add(v2).sub((v1.sub(v3)));
System.out.println(v4.toString());
System.out.print("||(v1 + v2) - (v1 -v3)|| = ");
v4 = v4.unit();
System.out.println(v4.toString());
System.out.println();
System.out.print("unit(v1) x sqrt(v1.v1) = ");
double squareRoot = Math.sqrt(v1.dot(v1));
System.out.println(v1.unit().sMult(squareRoot).toString());
v4 = v2.sub(v3);
v4 = v4.unit();
System.out.print("The x-component of (v2 - v3) / ||v2 - v3|| = ");
System.out.println(v4.getX());
System.out.print("The y-component of (v2 - v3) / ||v2 - v3|| = ");
System.out.println(v4.getY());
System.out.println();
//Swap x and y compnents of v3
double tempX = v3.getX();
double tempY = v3.getY();
double temp = tempX;
tempX = tempY;
tempY = temp;
v3.setVector2D(tempX, tempY);
System.out.println("v3 = " + v3.toString());
v4 = new Vector2D(v3); //create v4 from v3
System.out.println("v4 = " + v4.toString());
System.out.print("Is unit(4 x v4) equal to unit(v4)? ");
Vector2D tempVector = new Vector2D(v4);
tempVector = tempVector.sMult(4).unit();
System.out.println(tempVector.equals(v4.unit()));
System.out.println();
System.out.print("Are ||v2 - v3|| and ||v2 + v3|| equal? ");
System.out.println(v2.sub(v3).norm() == v2.add(v3).norm());
}
}
To get this, these are what has come closest to working for me
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started