I'm taking an online Computer Science Course for school and I have hit a bump in the road that after two days I still can not figure out, I have turned to the community.
I have written a piece of code that I need to print the catapult distances. I have put these objects in an ArrayList and have developed a class file for the catapult instance. I want my code to print a list of the distances and the MPH at the start of the row (You can see this with the source files below with an example of what I want it to look like). Here is the code I have developed up to this point:
Catapult.java
public class Catapult {
static double speed, angle, MPS, Rad, R;
Catapult(double launchSpeed, double launchAngle) {
angle = launchAngle;
speed = launchSpeed;
public void convMPHtoMPS() {
MPS = speed * 0.44704;
}
public void convDegToRad() {
Rad = Math.toRadians(angle);
}
public void calcDistance() {
R = (Math.pow(MPS, 2)*Math.sin(2*Rad))/(Math.pow(9.8, 2));
}
public void convMtoFt() {
R = R * 3.28084;
}
public double getMPH() {
return speed;
}
public double getAngle() {
return angle;
}
public double getMetersPerSecond() {
return MPS;
}
public double getRadians() {
return Rad;
}
public double getDistance() {
return R;
}
}
CatapultTester.java
public static void calcData() {
int mph = 25;
for (int i = 0; i < 7; i++) {
int deg = 25;
for (int j = 0; j < 6; j++) {
CP.add(new Catapult(mph, deg));
//System.out.println(mph);
//System.out.println(deg);
deg += 5;
}
mph += 5;
}
System.out.println(CP.size());
Catapult data;
for (int index = 0; index < CP.size(); index++) {
data = CP.get(index);
data.convMPHtoMPS();
data.convDegToRad();
data.calcDistance();
data.convMtoFt();
//System.out.println(data.getMPH());
//System.out.println(data.getDistance());
}
presentData();
}
public static void presentData() {
Catapult data;
for (int index = 0; index < CP.size(); index++) {
data = CP.get(index);
System.out.printf("%1f", data.getMPH());
for (int j = 0; j < 6; j++) {
System.out.printf("%4.2f", data.getDistance());
}
}
}
Expected Output
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg
25 distance distance distance distance distance distance
30 distance distance distance distance distance distance
...
Distance is the formula declared in Catapult.java
R = (Math.pow(MPS, 2)*Math.sin(2*Rad))/Math.pow(9.8)/2));
Actual Output:
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg
55 20.34 20.34 20.34 20.34 20.34 20.34
55 20.34 20.34 20.34 20.34 20.34 20.34
55 20.34 20.34 20.34 20.34 20.34 20.34
...
Thank you all for your help and I apologize for any grammatical errors or confusion with code. I am new to the Java programming language!
您不应该在您的情况下使用静态变量,因为它将在该类的所有对象(弹射器)中通用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.