Vect.java
Práce s vektorem
java
import java.lang.Math;
public class Vect {
private double x,y;
// Testy na prvním místě :)
public static void main(String[] args) {
boolean assertsEnabled = false; // Je zbytečné pouštět test bez zapnutého assertu
assert assertsEnabled = true; // Tohle je finta, jak to detekovat
if (!assertsEnabled)
throw new RuntimeException("Assert se zapíná parametrem -ea");
double c;
Vect aa=new Vect(1,3);
assert aa.getX() == 1 : "Chyba getX";
assert aa.getY() == 3 : "Chyba getY";
assert aa.setX(8).getX() == 8 : "Chyba setX";
assert aa.setY(9).getY() == 9 : "Chyba setY";
assert aa.addX(3).getX() == 11 : "Chyba addX";
assert aa.addY(5).getY() == 14 : "Chyba addY";
Vect bb=new Vect(6,2);
assert diff(aa,bb) == 13 : "Chyba výpočtu vzdálenosti";
assert bb.setXY(7,9).getX() == 7 : "Chyba setXY v X";
assert bb.setXY(7,9).getX() == 7 : "Chyba setXY v X";
assert bb.setX(5).setY(6).getX() == 5 : "Chyba řetězeného setX";
assert bb.setX(5).setY(6).getY() == 6 : "Chyba řetězeného setY";
assert bb.setY(8).setX(6).getY() == 8 : "Chyba řetězeného setY";
}
public Vect(double x, double y) {
this.x = x;
this.y = y;
}
public Vect addXY(double x, double y) {
this.x += x;
this.y += y;
return this;
}
public Vect setXY(double x, double y) {
this.x = x;
this.y = y;
return this;
}
public Vect setX(double x) {
this.x = x;
return this;
}
public Vect setY(double y) {
this.y = y;
return this;
}
public Vect addX(double x) {
this.x += x;
return this;
}
public Vect addY(double y) {
this.y += y;
return this;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public static double diff(Vect a, Vect b) {
double x = a.getX() - b.getX();
double y = a.getY() - b.getY();
return Math.sqrt(x*x + y*y);
}
}
Neformátovaný
Přidáno: 29.12.2012
Expirace: Neuvedeno