Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please make changes to the following java code following the instruciton of tasks posted with this question. Please make use of the resources, the code

Please make changes to the following java code following the instruciton of tasks posted with this question. Please make use of the resources, the code should not be too complicated. The code is correct if it runs with the test code below correctly. Thank you
The code to be edited:
class MathVector {
final int dim;
final double[] values;
public MathVector(int N){
dim = N;
values = new double[N];
}
public MathVector(double[] arr){
dim = arr.length;
values = new double[arr.length];
}
public MathVector(MathVector v){
dim =0;
values = null;
}
public static MathVector vec2DfromPolar(double r, double theta){
return new MathVector(0);
}
public static MathVector vec3DfromPolar(double r, double theta, double phi){
return new MathVector(0);
}
public int getDimension(){ return 0; }
public double getValue(int idx){ return 0; }
public void setValue(int idx, double val){}
public double dotWith(MathVector other){ return 0; }
public double getMagnitude(){ return 0; }
public void scaleBy(double scalar){}
public static MathVector multiply(double scalar, MathVector v){
return new MathVector(0);
}
public void shiftBy(MathVector other){}
public static MathVector add(MathVector v1, MathVector v2){
return new MathVector(0);
}
public static MathVector add(MathVector... vs){
return new MathVector(0);
}
public boolean equals(MathVector other){ return false; }
public String toString(){ return ""; }
public void print(){}
}
The test code:
class HW2_Tester {
public static void main(String[] args){
MathVector v1= new MathVector(1);
MathVector v2= new MathVector(1.0,2.0);
MathVector v3= new MathVector(v2);
System.out.println(v1.getDimension()+""+ v2.getDimension()); //12
System.out.println(v1.getValue(1)); //0.0
System.out.print('
');
v3.setValue(2,3.0);
System.out.println(v1.toString()); //[0.0]
System.out.println(v2); //[1.0,2.0]
v3.print(); //[1.0,3.0]
System.out.print('
');
MathVector.vec2DfromPolar(2, Math.PI /3).print();
// close to [1.0, Math.sqrt(3.0)]
MathVector.vec3DfromPolar(4, Math.PI /6, Math.PI /3).print();
// close to [1.0, Math.sqrt(3.0),2* Math.sqrt(3)]
System.out.print('
');
MathVector v = new MathVector(1,2,0,0);
MathVector w = new MathVector(0,0,3,4);
final MathVector e1= new MathVector(1,0,0,0);
final MathVector e2= new MathVector(0,1,0,0);
final MathVector e3= new MathVector(0,0,1,0);
final MathVector e4= new MathVector(0,0,0,1);
System.out.println(v.dotWith(e1)); //1.0
System.out.println(v.dotWith(e2)); //2.0
System.out.println(w.dotWith(w)); //25.0
System.out.println(w.getMagnitude()); //5.0
System.out.print('
');
System.out.println(MathVector.multiply(2, w)); //[0.0,0.0,6.0,8.0]
w.print(); // w unchanged by line before //[0.0,0.0,3.0,4.0]
w.scaleBy(2);
w.print(); // w changed by line before //[0.0,0.0,6.0,8.0]
System.out.print('
');
System.out.println(MathVector.add(v, w)); //[1.0,2.0,6.0,8.0]
v.print(); // v unchanged by line before //[1.0,2.0,0.0,0.0]
w.print(); // w unchanged by line before //[0.0,0.0,6.0,8.0]
System.out.print('
');
v.shiftBy(w);
v.print(); // v changed by line before //[1.0,2.0,6.0,8.0]
w.print(); // w unchanged by line before //[0.0,0.0,6.0,8.0]
System.out.print('
');
MathVector linearCombination = MathVector.add(
MathVector.multiply(8, e1), MathVector.multiply(7, e2),
MathVector.multiply(6, e3), MathVector.multiply(5, e4)
);
linearCombination.print(); //[8.0,7.0,6.0,5.0]
System.out.print('
');
System.out.println(!e1.equals(e2)); // true
System.out.println(!e2.equals(e3)); // true
System.out.println(!e3.equals(e4)); // true
System.out.println(!(new MathVector(4)).equals(new MathVector(5))); // true
System.out.println(v.equals(v)); // true
System.out.println(w.equals(w)); // true
System.out.println(w.equals(new MathVector(0,0
The task instructions are linked as as file
image text in transcribed

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

Recommended Textbook for

Expert Oracle Exadata

Authors: Martin Bach, Kristofferson Arao

2nd Edition

1430262427, 9781430262428

More Books

Students also viewed these Databases questions

Question

=+ e. What problem does your answer to part (d) illustrate?

Answered: 1 week ago