教育サーバーのページ
オンラインテキスト目次
ソフトウエア入門演習
public void back(int t) {
forward(-t);
}
public void forward(int t) {
position += t;
if (position > 60*60) {
position = 60*60;
}
}
public void back(int t) {
position -= t;
if (position < 0) {
position = 0;
}
}
class Point {
double x = 0, y = 0;
public void move(double mx, double my) {
x += mx; y += my;
}
}
課題7
class Point {
double x = 0, y = 0;
public void move(double mx, double my) {
x += mx; y += my;
}
public void swap_xy() {
int t = x;
x = y;
y = t;
}
}
public class PointTest {
public static void main(String[] args) {
Point p = new Point();
p.x = 100;
p.y = 60;
System.out.println("before swap: x=" + p.x + " y=" + p.y);
p.swap_xy();
System.out.println("after swap: x=" + p.x + " y=" + p.y);
}
}
課題8
class Point {
private double x = 0, y = 0;
public double getx() {
return x;
}
public double gety() {
return y;
}
public void set(double x, double y) {
this.x = x;
this.y = y;
}
/* あるいは、
public void set(double sx, double sy) {
x = sx;
y = sy;
}
*/
public void move(double mx, double my) {
x += mx; y += my;
}
}
public class PointTest {
public static void main(String[] args) {
Point p = new Point();
p.set(100, 60);
System.out.println("x=" + p.getx() + " y=" + p.gety());
p.move(20, -30);
System.out.println("x=" + p.getx() + " y=" + p.gety());
}
}
ソフトウエア入門演習
教育サーバーのページ
ohmi@rsch.tuis.ac.jp