中级农民
- 积分
- 220
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-10-28
- 最后登录
- 1970-1-1
|
/*
在discussion2 & HW1上付出远超出要求的时间,对这一周的习题训练感想很多,稍微总结:
1. 写好一个robust的test对于初学者是多么的不容易(尤其对初学者),写好一个user-friendly的class同理不易。这点在自己写test测试discussion2时感受最深刻,linked-list的经典NPE处理折磨我好久,好多时候是test本身的问题,反而让我误以为源程序有问题 ,可见坏test是多么的坑己……在这一点上有了初步认知后再去听SList和sentinel node,理解深入很多
2. 然后,HW1.个人认为官方的作业要求写的不好,有的时候需要参考test代码里的变量名,浪费了一些时间,感觉不是很值。
*/
Planet.java
public class Planet {
double x;
double y;
double xVelocity;
double yVelocity;
double mass;
String img;
double G = 6.67e-11;
double xNetForce;
double yNetForce;
double xAccel;
double yAccel;
public Planet (double xPos, double yPos, double xVel, double yVel, double m, String image) {
this.x = xPos;
this.y = yPos;
this.xVelocity = xVel;
this.yVelocity = yVel;
this.mass = m;
this.img = image;
} // close constructor
// Planet planet = new Planet(x, y, xVelocity, yVelocity, mass, img);
// Planet planet2 = new Planet(x2, y2, xVel2, yVel2, m2, im2);
public double calcDistance (Planet planetX) {
double dis;
dis = Math.sqrt((x - planetX.x) * (x - planetX.x) + (y - planetX.y) * (y - planetX.y));
// dis = Math.sqrt(dis);
return dis;
}
public double calcPairwiseForce (Planet planetX) {
double f = G * mass * planetX.mass / Math.pow(calcDistance (planetX), 2);
return f;
}
public double calcPairwiseForceX (Planet planetX) {
double fX = calcPairwiseForce (planetX) * (planetX.x - x) / calcDistance(planetX);
return fX;
}
public double calcPairwiseForceY (Planet planetX) {
double fY = calcPairwiseForce (planetX) * (planetX.y - y) / calcDistance(planetX);
return fY;
}
public void setNetForce (Planet[] planetG) {
// add the force one by one
this.xNetForce = 0;
this.yNetForce = 0;
for (int i = 0; i < planetG.length; i++) {
if (this != planetG[i]) {
this.xNetForce += calcPairwiseForceX (planetG[i]);
this.yNetForce += calcPairwiseForceY (planetG[i]);
} else {
this.xNetForce += 0;
this.yNetForce += 0;
}
}
} // close setNetForce method
/**
** Draw the planet itself at its appropriate position
** to the specific x, y coordinates.
**/
public void draw() {
StdDraw.picture(this.x, this.y, "images/" + this.img);
} // close draw method
/** Update the new positions and velocities of the planets **/
public void update (double dt) {
xAccel = this.xNetForce / mass;
yAccel = this.yNetForce / mass;
xVelocity = xVelocity + dt * xAccel;
yVelocity = yVelocity + dt * yAccel;
x = x + dt * xVelocity;
y = y + dt * yVelocity;
} // close update method
}
NBody.java
public class NBody {
/*
takes an In object as an argument, and reads from it the information for the next Planet object.
It will then instantiate that Planet and return it.
Note that getPlanet will NOT work on an In unless the In has already had its number of planets and universe radius read.
*/
public static Planet getPlanet (In i) {
double x = i.readDouble();
double y = i.readDouble();
double xvel = i.readDouble();
double yvel = i.readDouble();
double m = i.readDouble();
String img = i.readString();
Planet p = new Planet(x, y, xvel, yvel, m, img);
return p;
} // close getPlanet()
public static void main(String[] args) {
// step 1
double T;
double time = 0.0;
T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
// step 2
String filename = args[2];
// step 3
In file = new In(filename);
// step 4
int planetNum = file.readInt();
double radius = file.readDouble();
/***************************************************
****** Drawing the Initial Universe State **********
*****************************************************/
// read all planets into one array
// set the universe scale
StdDraw.setScale(-radius, radius);
// draw the initial state of the universe
StdDraw.picture(0, 0, "images/starfield.jpg");
// build the array with the size of planet #
Planet[] uni = new Planet[planetNum];
// read all planets into array
for (int j = 0; j < planetNum; j++) {
uni[j] = getPlanet(file);
}
/*******************************************
************* Amimation **********************
*******************************************/
StdAudio.play("audio/2001.mid"); // add background audio
while (time < T) {
StdDraw.picture(0, 0, "images/starfield.jpg");
for (int k = 0; k < planetNum; k++) {
uni[k].setNetForce(uni);
uni[k].update(dt);
uni[k].draw();
}
StdDraw.show(10);
time += dt;
}
/**********************************************
************** Printing the Universe **********
***********************************************/
StdOut.printf("%d\n", planetNum);
StdOut.printf("%.2e\n", radius);
for (int l = 0; l < planetNum; l++) {
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
uni[l].x, uni[l].y, uni[l].xVelocity, uni[l].yVelocity, uni[l].mass, uni[l].img);
} // close for loop
} // close main method
} // close NBody class |
|