Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/527186591987421184
Rollback to Revision 4
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141



Here's my re-factored code, based on input from as many answers as possible. Thanks for the feedback!

import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x * x + y * y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx * dx + dy * dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 



Here's my re-factored code, based on input from as many answers as possible. Thanks for the feedback!

import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x * x + y * y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx * dx + dy * dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 
added 12 characters in body
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146
import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x*x+y*y;x * x + y * y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx*dx+dy*dy;dx * dx + dy * dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 
import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x*x+y*y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx*dx+dy*dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 
import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x * x + y * y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx * dx + dy * dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 
added 1143 characters in body
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146



Here's my re-factored code, based on input from as many answers as possible. Thanks for the feedback!

import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x*x+y*y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx*dx+dy*dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 



Here's my re-factored code, based on input from as many answers as possible. Thanks for the feedback!

import java.util.*; class Vector { private final double x; private final double y; public Vector(double x, double y) { this.x = x; this.y = y; } public Vector add(Vector other) { return new Vector(x + other.x, y + other.y); } public Vector sub(Vector other) { return new Vector(x - other.x, y - other.y); } public double scalar(Vector other) { return x * other.x + y * other.y; } public double lengthSq() { return x*x+y*y; } public double length() { return Math.sqrt(lengthSq()); } public double distSq(Vector other) { double dx = x - other.x; double dy = y - other.y; return dx*dx+dy*dy; } public double distance(Vector other) { return Math.sqrt(distSq(other)); } } 
Added [physics] tag as vectors are a fundamental part of physics.
Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146
Loading
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
no need for Main class stub, take Java from the title.
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419
Loading
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146
Loading