java - How does the syntax look like to create and test a vector? [level: newbie] -


i'm studying java bit , want create vector class understand exceptions , arrays. in case go double arrays first.

i implemented 2 constructors class. 1 length , 1 array.

the variable length contains length of vector.

get() , set() implemented read or change values.

minimum(), maximum() , average() implemented wanted values of array.

tostring() should print vector string.

my code far

public class myvector {      //lenght of vector     public final int l;      //array representing vector     private double[] arr;       // constructs vector.     public myvector(int l) {         this.l = l;         arr = new double[l];     }      // constructs  vector  existing array     public myvector(double[] array) {         l = array.length;          // copy]         arr = new double[l];         (int = 0; < l; i++)             arr[i] = array[i];     }       //gets value @ given index.     public double get(int index) {         return arr[index];      }      // sets  value @ given index.     public double set(int index, double value) {      }       // returns minimal value in vector.     public double minimum() {         double minimum = get(0);          (int i=1; i<arr.l; i++)             if (get(i)<minimum)                 minimum = get(i);          return minimum;    }      // returns maximal value in vector.     public double maximum() {         double maximum = get(0);          (int i=1; i<arr.l; i++)             if (get(i)>maximum)                 maximum = get(i);          return maximum;    }      // average of values in vector     public double average() {         double sum = 0;          (int i=0; i<arr.l; i++)             sum += get(i);           return sum/arr.l;     }       @override     public string tostring() {         return "[arr=" + arr.l                 + " min=" + min()                 + " max=" + maximum()                 + " avg=" + average();     } } 

first of want test vectors. guess need testerclass vector public static void main(string[] args) { } , 2 testermethods. 1 test class , 1 test methods of class. how syntax create , test vector?

i appreciate feedback myvector class well.

thank you.

the statements wrote in comments responding mureinik work it's not clear they'd you, other array of vectors of varying lengths.

re goal stated in question, started testing in rudimentary way creating test class bunch of test methods , main method runs them all. e.g. 1 might like:

public boolean testaverage(vector v, double expected) {   double computed = v.average();   boolean passed = expected == computed;   if (!passed) {     system.err.printf("incorrect average %s; expected %f, got %f\n",       v, expected, computed);   }   return passed; } 

at point you'll want learn junit, there numerous tutorials you'll find via searching. it's whole framework writing tests java classes. self-professed newbie it's lot take on until you've become more comfortable language. kudos being motivated write tests in learning curve.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -