Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /**
  2. * Beschreiben Sie hier die Klasse TwoDimVector.
  3. *
  4. * @author (Ihr Name)
  5. * @version (eine Versionsnummer oder ein Datum)
  6. */
  7. public class TwoDimVector extends ATwoDimVector
  8. {
  9. //Attribute
  10.  
  11. public TwoDimVector(double x, double y) {
  12. super(x, y);
  13. }
  14.  
  15. //Methoden
  16. /**
  17. * Return the x-coordinate of the vector
  18. * @return x-coordinate of the vector as int-value, obtained by rounding
  19. * the internal double representation to the closest int (using Math.round)
  20. */
  21. public int getX(){
  22. return (int)Math.round(x);
  23. }
  24.  
  25. /**
  26. * Return the y-coordinate of the vector
  27. * @return y-coordinate of the vector as int-value, obtained by rounding
  28. * the internal double representation to the closest int (using Math.round)
  29. */
  30. public int getY(){
  31. return (int)Math.round(y);
  32. }
  33.  
  34. /**
  35. * Vector addition
  36. * @param v Other vector, so that the vector sum of this and v shall be calculated
  37. * @return New vector z that is the sum of this and v. If this has coordinates (x,y)
  38. * and v has coordinates (w,z), then z has coordinates (x+w,y+z).
  39. */
  40. public ATwoDimVector add(ATwoDimVector v){
  41. return new TwoDimVector(this.x + v.x, (this.y + v.y));
  42.  
  43. }
  44.  
  45. public ATwoDimVector sub(ATwoDimVector v){
  46. return new TwoDimVector(this.x - v.x, (this.y - v.x));
  47. }
  48.  
  49. /**
  50. * Return the scalar product of this multiplied by a factor
  51. * @param factor The factor to be applied by the scalar product
  52. * @return If this has coordinates (x,y), then a new vector with
  53. * coordinates (factor*x,factor*y) is returned.
  54. */
  55. public ATwoDimVector scalarProduct(double factor){
  56. return new TwoDimVector(factor * x, factor * y);
  57. }
  58.  
  59. /**
  60. * Return the result of the inner product of this and v
  61. * @param v Other vector, so that the inner product <this,v> shall
  62. * be calculated.
  63. * @return The value of <this, v>. If this has coordinates (x,y) and
  64. * v has coordinates (w,z), then <this,v> = x*w + y*z.
  65. */
  66. public double innerProduct(ATwoDimVector v){
  67. return ((this.x * v.x) + (this.y * y));
  68. }
  69.  
  70. /**
  71. * Return the squared length of this
  72. * @return Squared length of this. If this has coordinates (x,y),
  73. * then the squared length is x*x + y*y
  74. */
  75. public double squaredLength(){
  76. return ((this.x * this.x) + (this.y * this.y));
  77. }
  78.  
  79. /**
  80. * Output the internal coordinated representation.
  81. * @return String in format (ix,iy), where ix is the rounded int-value
  82. * of the x-ccordinate, and iy the rounded int-value of the y-coordinate.
  83. * (use Math.round)
  84. */
  85. public String toString(){
  86. return "(" + Math.round(x) + ", " + Math.round(y) + ")";
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement