資料中のソースコード 7-4
class Ball {
double x;
double y;
double r;
String color;
public Ball(double x, double y, double r, String color) {
this.x = x;
this.y = y;
this.r = r;
this.color = color;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.r);
System.out.println(this.color);
}
public double dist() {
return this.x + this.y;
}
public void move(double xx, double yy) {
this.x = this.x + xx;
this.y = this.y + yy;
}
public void right(double xx) {
this.move(xx, 0);
}
public void left(double xx) {
this.move(-xx, 0);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Ball a = new Ball(8, 10, 1, "blue");
Ball b = new Ball(2, 4, 3, "green");
a.move(5, 5);
b.move(10, 10);
a.left(5);
b.right(10);
a.printout();
b.printout();
System.out.println(a.dist());
}
}
51