// Universe.java import java.util.*; public class Universe { static int xg = 0, yg = 4; static int height = 25, width = 25; static class Particle { String name; int pos[] = {0, 0}; int dot[] = {0, 0}; int acc[] = {0, 0}; Particle(String n, int px, int py, int vx, int vy) { name = n; pos[0] = px; pos[1] = py; dot[0] = vx; dot[1] =vy; } Particle(String n, int px, int py) { this(n,px,py,0,0); } int[] pos() { return pos; } int[] pos(int x, int y) { pos[0] = x; pos[1] = y; return pos; } int[] vel() { return dot; } public int[] vel(int x, int y) { dot[0] = x; dot[1] = y; return dot; } int[] acc() { return acc; } int[] acc(int x, int y) { acc[0] = x; acc[1] = y; return acc; } void step() { pos[0] += dot[0]; pos[1] += dot[1]; dot[0] += acc[0]; dot[1] += acc[1]; } void status1() { System.out.print(name + "'s P:(" + pos[0] + ", " + pos[1] + ") V:(" + dot[0] + ", " + dot[1] + ") A:(" + acc[0] + ", " + acc[1] + ") "); if (iscrashed()) { System.out.println(";"); System.out.print(" " + name + " crashed "); } } void status() { status1(); System.out.println(); } boolean iscrashed() { return( (pos[0] < width) && (pos[1] > height)); } } static class Rocket extends Particle { int motor[] ={0, 0}; public Rocket(String n, int x0, int y0, int vx, int vy, int mx, int my) { super(n, x0, y0, vx, vy); motor(mx, my); } int[] motor() { return motor; } int[] motor(int x, int y) { motor[0] = x; motor[1] = y; acc[0] = xg + x; acc[1] = yg + y; return motor; } void status() { super.status1(); System.out.println(" M:(" + motor[0] + ", " + motor[1] + ") "); } } static void main(String argv[]) { Particle[] ps = new Particle[6]; ps[0] = new Particle(new String("p1"),4,3,2,5); ps[1] = new Particle("p2",0,0,0,1); ps[2] = new Particle("p3",1,1,9,4); ps[3] = new Rocket("r1",2,3,4,3,2,1); ps[4] = new Rocket("r2",7,5,2,1,7,6); ps[5] = new Rocket("r3",2,4,4,2,2,3); for (int t=0; t<5; t++) { for(int i=0; i<6; i++) { ps[i].status(); ps[i].step(); } } } }