The task is to create an ArrayVector class that implements work with vectors (a set of real numbers, coordinates) and basic vector arithmetic operations. The class must meet the following requirements.

The instance must correspond to a vector of fixed length (it is specified as a parameter of the constructor). The following methods should be implemented:

  • access to vector elements (get value and change value), ( getElement(), setElement() );
  • get the "length" of the vector (the number of its elements), ( getSize() );
  • search for the minimum and maximum values ​​of the elements of the vector, ( min(), max() );
  • vector sorting (ascending or descending - your choice), ( sort() );
  • finding the Euclidean norm, ( getNorm() );
  • multiplying a vector by a number, ( mult() );
  • addition of two vectors, ( sum() );
  • finding the scalar product of two vectors ( scalarMult() ).

In the process of performing the task, it is NOT possible to use Java utility classes (except for the Math.sqrt() method). The entry point of the program can be implemented in class, for debugging purposes, but is not required to write.

I am quite a beginner, so please do not laugh out loud :) I understand it is necessary to write a class that encapsulates a vector (based on an array) and supports a number of operations with the vector and on vectors. The question is: have I correctly imagined the task and how is it most logical to implement it? Code:

 public class ArrayVector { double [] a={2,23,32}; public double[] getA() { return a; } public void setA(double[] a) { this.a = a; } public void dlina(double a){ double dlina; //как обратиться к массиву и выполнять векторные операции? } } 
  • one
    I did not see setA () and getA () in the statement of work, but write the constructor right away. In it, initialize the vector with zeros and remember the length (this is the argument of the constructor), which you will return with the "dlina" method ( well, with a name ) - avp
  • avp, and can, of course, if it is not difficult, to roughly describe your answer in the code?) - FusionF
  • But what if I do something like this: public class ArrayVector {double [] a = {2.23,32}; public double [] getA () {return a; } public void setA (double [] a) {this.a = a; } double dlina; public double getDlina () {return dlina; } public void setDlina (double dlina) {this.dlina = dlina; } ArrayVector vector = new ArrayVector (); public void getSize () {dlina = Math.sqrt (vector.a [0] + vector.a [1] + vector.a [2]); } public static void main (String [] args) {vector.getSize (); System.out.println (dlina); }} swears they say is not a static vector and dlina - FusionF
  • And rightly so, and if so? :) Read more about static methods and fields. public class ArrayVector {double [] a = {2, 23, 32}; double dlina; public void getSize () {for (double value: a) {dlina + = value; }} // ... other public static void methods main (String [] args) {ArrayVector vector = new ArrayVector (); vector.getSize (); System.out.println (vector.getDlina ()); }} - masterMind 2:28 pm
  • @FusionF Bring your class to a separate file, see hashcode.ru/questions/154106/… - Costantino Rupert

2 answers 2

I added a code that will hopefully help you in learning a language. The example consists of 3 classes:

1) IArrayVector - sets the base methods for your 'ArrayVector' (it guarantees that all implementations of this interface will have the methods you need)
2) AArrayVector - implements some common functionality for all valid implementations of the ArrayVector (this functionality is common, and is unlikely to change for individual implementations)
3) IntegerArrayVector - an example of a simple implementation (of course, there is still a lot of things not working)

one)

 public interface IArrayVector<E extends Comparable<E>> { int getSize(); E getElement(int index); void setElement(int index, E element); E min(); E max(); E getNorm(); IArrayVector<E> sort(); IArrayVector<E> mult(E number); IArrayVector<E> sum(IArrayVector<E> vector); IArrayVector<E> scalarMult(IArrayVector<E> vector); } 

2)

 public abstract class AArrayVector<E extends Comparable<E>> implements IArrayVector<E>, Cloneable { private E[] elements = null; public AArrayVector(int size) { elements = createArray(size); } @Override public final E getElement(int index) { if (null == elements || index > getSize()) { return null; } return (E) elements[index]; } @Override public final void setElement(int index, E element) { if (0 <= index && index <= getSize()) { elements[index] = element; } } protected abstract E[] createArray(int size); @Override public int getSize() { if (null == elements) { return 0; } return elements.length; } @Override public final E max() { E max = null; for (int i = 0; i < getSize(); i++) { // if max element is not found use the first one // else use greater one if (null == max || (1 > max.compareTo(elements[i]))) { max = elements[i]; } } return max; } @Override public final E min() { E min = null; for (int i = 0; i < elements.length; i++) { // if max element is not found use the first one // else use greater one if (null == min || (-1 < min.compareTo(elements[i]))) { min = elements[i]; } } return min; } @Override public AArrayVector<E> sort() { AArrayVector<E> cloned = (AArrayVector<E>) clone(); if (2 > getSize()) { return cloned; } return cloned; } @Override protected AArrayVector<E> clone() { AArrayVector<E> instance = createInstance(); for (int i = 0; i < getSize(); i++) { instance.setElement(i, getElement(i)); } return instance; } protected abstract AArrayVector<E> createInstance(); } 

3)

 public class IntegerArrayVector extends AArrayVector<Integer> { public IntegerArrayVector(int size) { super(size); } @Override protected Integer[] createArray(int size) { return new Integer[size]; } @Override protected AArrayVector<Integer> createInstance() { return new IntegerArrayVector(getSize()); } @Override public Integer getNorm() { // TODO - implement return null; } @Override public IArrayVector<Integer> mult(Integer number) { // create clone of this object AArrayVector<Integer> copy = createInstance(); // TODO: multiply each element by number value // return result return copy; } @Override public IArrayVector<Integer> scalarMult(IArrayVector<Integer> vector) { // TODO implement return null; } @Override public IArrayVector<Integer> sum(IArrayVector<Integer> vector) { // TODO implement return null; } // usage public static void main(String[] args) { IArrayVector<Integer> vector = new IntegerArrayVector(3); vector.setElement(0, 7); vector.setElement(0, 3); vector.setElement(0, 5); vector.sort(); } } 

ps I have already forgotten a course of mathematics, therefore I apologize in advance for the mistakes made

  • jmu, you, sir, of course, burn)) Is this really such a colossus needed to implement this shushary? And public static void main (String [] args) swears, "I’m in Eclipse sculpt :)) - FusionF
  • @jmu, as long as this creation can only confuse, frighten and ward off TC from Java. - avp
  • @avp, then tell me something more concise .. - FusionF
  • 2 @FusionF: I'm not lazy, besides, I love java because of its pro-OOP approach to solving problems. On the eclipse account, I didn’t write in notepad, did you scatter the classes on individual files or did you just remove the modifier from 2x? 2 @avp: quite likely, you think it was worth not adding generics? - jmu 5:58 pm
  • @jmu, think for yourself. Apparently one of the first tasks. In the text of the job requires work with a double array. Why is it now generics, interface, etc.? - I think one class with one constructor and an array initialized (by default) will be zeros just right. Then no null checks inside the methods are needed. You can leave ArrayIndexOutOfBoundsException, which is standard for arrays, and not check indices, etc. - But I think it would be more useful for TC to write it myself. Let him write options, consult, and if he fails, ask specific questions. - avp

Not a bad start ..

 //как обратиться к массиву и выполнять векторные операции? ArrayVector vector = new ArrayVector(); vector.sort(); 
  • Gorets, and what does vector.sort () do? In the task You can not load anything?) - FusionF
  • vector sorting (ascending or descending - your choice), (sort ()); - you realize - it will work)) - Gorets