import java.util.Scanner; public class Vector { private double x = 0; private double y = 0; public Vector() { } public Vector(double x, double y) { this.x = x; this.y = y; } public boolean leer() { Scanner teclado = new Scanner(System.in); x = teclado.nextDouble(); y = teclado.nextDouble(); return x != 0 && y != 0; } public String toString() { return String.format("(x, y) = (%s, %s); (r, θ) = (%s, %s)" , format(x), format(y), format(magnitud()), format(angulo())); } public static String format(double valor) { return (long)valor == valor ? String.format("%,d", (long)valor) : String.format("%.2f", valor); } public double magnitud() { return Math.sqrt(x*x + y*y); } public double angulo() { return Math.atan2(y, x); } public Vector sumar(Vector otro) { return new Vector(x + otro.x, y + otro.y); } public Vector restar(Vector otro) { return new Vector(x - otro.x, y - otro.y); } public static void main(String[] args) { Vector vector1 = null; Vector vector2 = new Vector(); System.out.println("Operaciones con vectores en el plano"); for (long i = 1; ; ++i) { System.out.print("\nVector " + i + ": "); if ( ! vector2.leer() ) return; System.out.println("V" + i + ": " + vector2); if ( vector1 != null ) { System.out.printf("\nV%d + V%d: %s\n", i - 1, i, vector1.sumar(vector2) ); System.out.printf("V%d - V%d: %s\n", i - 1, i, vector1.restar(vector2) ); System.out.printf("V%d - V%d: %s\n", i, i - 1, vector2.restar(vector1) ); } vector1 = vector2; vector2 = new Vector(); } } }